xfs_bmap.c 184 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511
  1. /*
  2. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_shared.h"
  21. #include "xfs_format.h"
  22. #include "xfs_log_format.h"
  23. #include "xfs_trans_resv.h"
  24. #include "xfs_bit.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_mount.h"
  27. #include "xfs_defer.h"
  28. #include "xfs_da_format.h"
  29. #include "xfs_da_btree.h"
  30. #include "xfs_dir2.h"
  31. #include "xfs_inode.h"
  32. #include "xfs_btree.h"
  33. #include "xfs_trans.h"
  34. #include "xfs_inode_item.h"
  35. #include "xfs_extfree_item.h"
  36. #include "xfs_alloc.h"
  37. #include "xfs_bmap.h"
  38. #include "xfs_bmap_util.h"
  39. #include "xfs_bmap_btree.h"
  40. #include "xfs_rtalloc.h"
  41. #include "xfs_error.h"
  42. #include "xfs_quota.h"
  43. #include "xfs_trans_space.h"
  44. #include "xfs_buf_item.h"
  45. #include "xfs_trace.h"
  46. #include "xfs_symlink.h"
  47. #include "xfs_attr_leaf.h"
  48. #include "xfs_filestream.h"
  49. #include "xfs_rmap.h"
  50. #include "xfs_ag_resv.h"
  51. #include "xfs_refcount.h"
  52. #include "xfs_rmap_btree.h"
  53. #include "xfs_icache.h"
  54. kmem_zone_t *xfs_bmap_free_item_zone;
  55. /*
  56. * Miscellaneous helper functions
  57. */
  58. /*
  59. * Compute and fill in the value of the maximum depth of a bmap btree
  60. * in this filesystem. Done once, during mount.
  61. */
  62. void
  63. xfs_bmap_compute_maxlevels(
  64. xfs_mount_t *mp, /* file system mount structure */
  65. int whichfork) /* data or attr fork */
  66. {
  67. int level; /* btree level */
  68. uint maxblocks; /* max blocks at this level */
  69. uint maxleafents; /* max leaf entries possible */
  70. int maxrootrecs; /* max records in root block */
  71. int minleafrecs; /* min records in leaf block */
  72. int minnoderecs; /* min records in node block */
  73. int sz; /* root block size */
  74. /*
  75. * The maximum number of extents in a file, hence the maximum
  76. * number of leaf entries, is controlled by the type of di_nextents
  77. * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
  78. * (a signed 16-bit number, xfs_aextnum_t).
  79. *
  80. * Note that we can no longer assume that if we are in ATTR1 that
  81. * the fork offset of all the inodes will be
  82. * (xfs_default_attroffset(ip) >> 3) because we could have mounted
  83. * with ATTR2 and then mounted back with ATTR1, keeping the
  84. * di_forkoff's fixed but probably at various positions. Therefore,
  85. * for both ATTR1 and ATTR2 we have to assume the worst case scenario
  86. * of a minimum size available.
  87. */
  88. if (whichfork == XFS_DATA_FORK) {
  89. maxleafents = MAXEXTNUM;
  90. sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
  91. } else {
  92. maxleafents = MAXAEXTNUM;
  93. sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
  94. }
  95. maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
  96. minleafrecs = mp->m_bmap_dmnr[0];
  97. minnoderecs = mp->m_bmap_dmnr[1];
  98. maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
  99. for (level = 1; maxblocks > 1; level++) {
  100. if (maxblocks <= maxrootrecs)
  101. maxblocks = 1;
  102. else
  103. maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
  104. }
  105. mp->m_bm_maxlevels[whichfork] = level;
  106. }
  107. STATIC int /* error */
  108. xfs_bmbt_lookup_eq(
  109. struct xfs_btree_cur *cur,
  110. xfs_fileoff_t off,
  111. xfs_fsblock_t bno,
  112. xfs_filblks_t len,
  113. int *stat) /* success/failure */
  114. {
  115. cur->bc_rec.b.br_startoff = off;
  116. cur->bc_rec.b.br_startblock = bno;
  117. cur->bc_rec.b.br_blockcount = len;
  118. return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
  119. }
  120. STATIC int /* error */
  121. xfs_bmbt_lookup_ge(
  122. struct xfs_btree_cur *cur,
  123. xfs_fileoff_t off,
  124. xfs_fsblock_t bno,
  125. xfs_filblks_t len,
  126. int *stat) /* success/failure */
  127. {
  128. cur->bc_rec.b.br_startoff = off;
  129. cur->bc_rec.b.br_startblock = bno;
  130. cur->bc_rec.b.br_blockcount = len;
  131. return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
  132. }
  133. /*
  134. * Check if the inode needs to be converted to btree format.
  135. */
  136. static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
  137. {
  138. return whichfork != XFS_COW_FORK &&
  139. XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
  140. XFS_IFORK_NEXTENTS(ip, whichfork) >
  141. XFS_IFORK_MAXEXT(ip, whichfork);
  142. }
  143. /*
  144. * Check if the inode should be converted to extent format.
  145. */
  146. static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
  147. {
  148. return whichfork != XFS_COW_FORK &&
  149. XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
  150. XFS_IFORK_NEXTENTS(ip, whichfork) <=
  151. XFS_IFORK_MAXEXT(ip, whichfork);
  152. }
  153. /*
  154. * Update the record referred to by cur to the value given
  155. * by [off, bno, len, state].
  156. * This either works (return 0) or gets an EFSCORRUPTED error.
  157. */
  158. STATIC int
  159. xfs_bmbt_update(
  160. struct xfs_btree_cur *cur,
  161. xfs_fileoff_t off,
  162. xfs_fsblock_t bno,
  163. xfs_filblks_t len,
  164. xfs_exntst_t state)
  165. {
  166. union xfs_btree_rec rec;
  167. xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
  168. return xfs_btree_update(cur, &rec);
  169. }
  170. /*
  171. * Compute the worst-case number of indirect blocks that will be used
  172. * for ip's delayed extent of length "len".
  173. */
  174. STATIC xfs_filblks_t
  175. xfs_bmap_worst_indlen(
  176. xfs_inode_t *ip, /* incore inode pointer */
  177. xfs_filblks_t len) /* delayed extent length */
  178. {
  179. int level; /* btree level number */
  180. int maxrecs; /* maximum record count at this level */
  181. xfs_mount_t *mp; /* mount structure */
  182. xfs_filblks_t rval; /* return value */
  183. xfs_filblks_t orig_len;
  184. mp = ip->i_mount;
  185. /* Calculate the worst-case size of the bmbt. */
  186. orig_len = len;
  187. maxrecs = mp->m_bmap_dmxr[0];
  188. for (level = 0, rval = 0;
  189. level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
  190. level++) {
  191. len += maxrecs - 1;
  192. do_div(len, maxrecs);
  193. rval += len;
  194. if (len == 1) {
  195. rval += XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
  196. level - 1;
  197. break;
  198. }
  199. if (level == 0)
  200. maxrecs = mp->m_bmap_dmxr[1];
  201. }
  202. /* Calculate the worst-case size of the rmapbt. */
  203. if (xfs_sb_version_hasrmapbt(&mp->m_sb))
  204. rval += 1 + xfs_rmapbt_calc_size(mp, orig_len) +
  205. mp->m_rmap_maxlevels;
  206. return rval;
  207. }
  208. /*
  209. * Calculate the default attribute fork offset for newly created inodes.
  210. */
  211. uint
  212. xfs_default_attroffset(
  213. struct xfs_inode *ip)
  214. {
  215. struct xfs_mount *mp = ip->i_mount;
  216. uint offset;
  217. if (mp->m_sb.sb_inodesize == 256) {
  218. offset = XFS_LITINO(mp, ip->i_d.di_version) -
  219. XFS_BMDR_SPACE_CALC(MINABTPTRS);
  220. } else {
  221. offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
  222. }
  223. ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version));
  224. return offset;
  225. }
  226. /*
  227. * Helper routine to reset inode di_forkoff field when switching
  228. * attribute fork from local to extent format - we reset it where
  229. * possible to make space available for inline data fork extents.
  230. */
  231. STATIC void
  232. xfs_bmap_forkoff_reset(
  233. xfs_inode_t *ip,
  234. int whichfork)
  235. {
  236. if (whichfork == XFS_ATTR_FORK &&
  237. ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
  238. ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
  239. ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
  240. uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
  241. if (dfl_forkoff > ip->i_d.di_forkoff)
  242. ip->i_d.di_forkoff = dfl_forkoff;
  243. }
  244. }
  245. #ifdef DEBUG
  246. STATIC struct xfs_buf *
  247. xfs_bmap_get_bp(
  248. struct xfs_btree_cur *cur,
  249. xfs_fsblock_t bno)
  250. {
  251. struct xfs_log_item_desc *lidp;
  252. int i;
  253. if (!cur)
  254. return NULL;
  255. for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
  256. if (!cur->bc_bufs[i])
  257. break;
  258. if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
  259. return cur->bc_bufs[i];
  260. }
  261. /* Chase down all the log items to see if the bp is there */
  262. list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
  263. struct xfs_buf_log_item *bip;
  264. bip = (struct xfs_buf_log_item *)lidp->lid_item;
  265. if (bip->bli_item.li_type == XFS_LI_BUF &&
  266. XFS_BUF_ADDR(bip->bli_buf) == bno)
  267. return bip->bli_buf;
  268. }
  269. return NULL;
  270. }
  271. STATIC void
  272. xfs_check_block(
  273. struct xfs_btree_block *block,
  274. xfs_mount_t *mp,
  275. int root,
  276. short sz)
  277. {
  278. int i, j, dmxr;
  279. __be64 *pp, *thispa; /* pointer to block address */
  280. xfs_bmbt_key_t *prevp, *keyp;
  281. ASSERT(be16_to_cpu(block->bb_level) > 0);
  282. prevp = NULL;
  283. for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
  284. dmxr = mp->m_bmap_dmxr[0];
  285. keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
  286. if (prevp) {
  287. ASSERT(be64_to_cpu(prevp->br_startoff) <
  288. be64_to_cpu(keyp->br_startoff));
  289. }
  290. prevp = keyp;
  291. /*
  292. * Compare the block numbers to see if there are dups.
  293. */
  294. if (root)
  295. pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
  296. else
  297. pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
  298. for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
  299. if (root)
  300. thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
  301. else
  302. thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
  303. if (*thispa == *pp) {
  304. xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
  305. __func__, j, i,
  306. (unsigned long long)be64_to_cpu(*thispa));
  307. panic("%s: ptrs are equal in node\n",
  308. __func__);
  309. }
  310. }
  311. }
  312. }
  313. /*
  314. * Check that the extents for the inode ip are in the right order in all
  315. * btree leaves. THis becomes prohibitively expensive for large extent count
  316. * files, so don't bother with inodes that have more than 10,000 extents in
  317. * them. The btree record ordering checks will still be done, so for such large
  318. * bmapbt constructs that is going to catch most corruptions.
  319. */
  320. STATIC void
  321. xfs_bmap_check_leaf_extents(
  322. xfs_btree_cur_t *cur, /* btree cursor or null */
  323. xfs_inode_t *ip, /* incore inode pointer */
  324. int whichfork) /* data or attr fork */
  325. {
  326. struct xfs_btree_block *block; /* current btree block */
  327. xfs_fsblock_t bno; /* block # of "block" */
  328. xfs_buf_t *bp; /* buffer for "block" */
  329. int error; /* error return value */
  330. xfs_extnum_t i=0, j; /* index into the extents list */
  331. xfs_ifork_t *ifp; /* fork structure */
  332. int level; /* btree level, for checking */
  333. xfs_mount_t *mp; /* file system mount structure */
  334. __be64 *pp; /* pointer to block address */
  335. xfs_bmbt_rec_t *ep; /* pointer to current extent */
  336. xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
  337. xfs_bmbt_rec_t *nextp; /* pointer to next extent */
  338. int bp_release = 0;
  339. if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
  340. return;
  341. }
  342. /* skip large extent count inodes */
  343. if (ip->i_d.di_nextents > 10000)
  344. return;
  345. bno = NULLFSBLOCK;
  346. mp = ip->i_mount;
  347. ifp = XFS_IFORK_PTR(ip, whichfork);
  348. block = ifp->if_broot;
  349. /*
  350. * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
  351. */
  352. level = be16_to_cpu(block->bb_level);
  353. ASSERT(level > 0);
  354. xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
  355. pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
  356. bno = be64_to_cpu(*pp);
  357. ASSERT(bno != NULLFSBLOCK);
  358. ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
  359. ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
  360. /*
  361. * Go down the tree until leaf level is reached, following the first
  362. * pointer (leftmost) at each level.
  363. */
  364. while (level-- > 0) {
  365. /* See if buf is in cur first */
  366. bp_release = 0;
  367. bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
  368. if (!bp) {
  369. bp_release = 1;
  370. error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
  371. XFS_BMAP_BTREE_REF,
  372. &xfs_bmbt_buf_ops);
  373. if (error)
  374. goto error_norelse;
  375. }
  376. block = XFS_BUF_TO_BLOCK(bp);
  377. if (level == 0)
  378. break;
  379. /*
  380. * Check this block for basic sanity (increasing keys and
  381. * no duplicate blocks).
  382. */
  383. xfs_check_block(block, mp, 0, 0);
  384. pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
  385. bno = be64_to_cpu(*pp);
  386. XFS_WANT_CORRUPTED_GOTO(mp,
  387. XFS_FSB_SANITY_CHECK(mp, bno), error0);
  388. if (bp_release) {
  389. bp_release = 0;
  390. xfs_trans_brelse(NULL, bp);
  391. }
  392. }
  393. /*
  394. * Here with bp and block set to the leftmost leaf node in the tree.
  395. */
  396. i = 0;
  397. /*
  398. * Loop over all leaf nodes checking that all extents are in the right order.
  399. */
  400. for (;;) {
  401. xfs_fsblock_t nextbno;
  402. xfs_extnum_t num_recs;
  403. num_recs = xfs_btree_get_numrecs(block);
  404. /*
  405. * Read-ahead the next leaf block, if any.
  406. */
  407. nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
  408. /*
  409. * Check all the extents to make sure they are OK.
  410. * If we had a previous block, the last entry should
  411. * conform with the first entry in this one.
  412. */
  413. ep = XFS_BMBT_REC_ADDR(mp, block, 1);
  414. if (i) {
  415. ASSERT(xfs_bmbt_disk_get_startoff(&last) +
  416. xfs_bmbt_disk_get_blockcount(&last) <=
  417. xfs_bmbt_disk_get_startoff(ep));
  418. }
  419. for (j = 1; j < num_recs; j++) {
  420. nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
  421. ASSERT(xfs_bmbt_disk_get_startoff(ep) +
  422. xfs_bmbt_disk_get_blockcount(ep) <=
  423. xfs_bmbt_disk_get_startoff(nextp));
  424. ep = nextp;
  425. }
  426. last = *ep;
  427. i += num_recs;
  428. if (bp_release) {
  429. bp_release = 0;
  430. xfs_trans_brelse(NULL, bp);
  431. }
  432. bno = nextbno;
  433. /*
  434. * If we've reached the end, stop.
  435. */
  436. if (bno == NULLFSBLOCK)
  437. break;
  438. bp_release = 0;
  439. bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
  440. if (!bp) {
  441. bp_release = 1;
  442. error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
  443. XFS_BMAP_BTREE_REF,
  444. &xfs_bmbt_buf_ops);
  445. if (error)
  446. goto error_norelse;
  447. }
  448. block = XFS_BUF_TO_BLOCK(bp);
  449. }
  450. return;
  451. error0:
  452. xfs_warn(mp, "%s: at error0", __func__);
  453. if (bp_release)
  454. xfs_trans_brelse(NULL, bp);
  455. error_norelse:
  456. xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
  457. __func__, i);
  458. panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
  459. return;
  460. }
  461. /*
  462. * Add bmap trace insert entries for all the contents of the extent records.
  463. */
  464. void
  465. xfs_bmap_trace_exlist(
  466. xfs_inode_t *ip, /* incore inode pointer */
  467. xfs_extnum_t cnt, /* count of entries in the list */
  468. int whichfork, /* data or attr or cow fork */
  469. unsigned long caller_ip)
  470. {
  471. xfs_extnum_t idx; /* extent record index */
  472. xfs_ifork_t *ifp; /* inode fork pointer */
  473. int state = 0;
  474. if (whichfork == XFS_ATTR_FORK)
  475. state |= BMAP_ATTRFORK;
  476. else if (whichfork == XFS_COW_FORK)
  477. state |= BMAP_COWFORK;
  478. ifp = XFS_IFORK_PTR(ip, whichfork);
  479. ASSERT(cnt == xfs_iext_count(ifp));
  480. for (idx = 0; idx < cnt; idx++)
  481. trace_xfs_extlist(ip, idx, state, caller_ip);
  482. }
  483. /*
  484. * Validate that the bmbt_irecs being returned from bmapi are valid
  485. * given the caller's original parameters. Specifically check the
  486. * ranges of the returned irecs to ensure that they only extend beyond
  487. * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
  488. */
  489. STATIC void
  490. xfs_bmap_validate_ret(
  491. xfs_fileoff_t bno,
  492. xfs_filblks_t len,
  493. int flags,
  494. xfs_bmbt_irec_t *mval,
  495. int nmap,
  496. int ret_nmap)
  497. {
  498. int i; /* index to map values */
  499. ASSERT(ret_nmap <= nmap);
  500. for (i = 0; i < ret_nmap; i++) {
  501. ASSERT(mval[i].br_blockcount > 0);
  502. if (!(flags & XFS_BMAPI_ENTIRE)) {
  503. ASSERT(mval[i].br_startoff >= bno);
  504. ASSERT(mval[i].br_blockcount <= len);
  505. ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
  506. bno + len);
  507. } else {
  508. ASSERT(mval[i].br_startoff < bno + len);
  509. ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
  510. bno);
  511. }
  512. ASSERT(i == 0 ||
  513. mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
  514. mval[i].br_startoff);
  515. ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
  516. mval[i].br_startblock != HOLESTARTBLOCK);
  517. ASSERT(mval[i].br_state == XFS_EXT_NORM ||
  518. mval[i].br_state == XFS_EXT_UNWRITTEN);
  519. }
  520. }
  521. #else
  522. #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
  523. #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
  524. #endif /* DEBUG */
  525. /*
  526. * bmap free list manipulation functions
  527. */
  528. /*
  529. * Add the extent to the list of extents to be free at transaction end.
  530. * The list is maintained sorted (by block number).
  531. */
  532. void
  533. xfs_bmap_add_free(
  534. struct xfs_mount *mp,
  535. struct xfs_defer_ops *dfops,
  536. xfs_fsblock_t bno,
  537. xfs_filblks_t len,
  538. struct xfs_owner_info *oinfo)
  539. {
  540. struct xfs_extent_free_item *new; /* new element */
  541. #ifdef DEBUG
  542. xfs_agnumber_t agno;
  543. xfs_agblock_t agbno;
  544. ASSERT(bno != NULLFSBLOCK);
  545. ASSERT(len > 0);
  546. ASSERT(len <= MAXEXTLEN);
  547. ASSERT(!isnullstartblock(bno));
  548. agno = XFS_FSB_TO_AGNO(mp, bno);
  549. agbno = XFS_FSB_TO_AGBNO(mp, bno);
  550. ASSERT(agno < mp->m_sb.sb_agcount);
  551. ASSERT(agbno < mp->m_sb.sb_agblocks);
  552. ASSERT(len < mp->m_sb.sb_agblocks);
  553. ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
  554. #endif
  555. ASSERT(xfs_bmap_free_item_zone != NULL);
  556. new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
  557. new->xefi_startblock = bno;
  558. new->xefi_blockcount = (xfs_extlen_t)len;
  559. if (oinfo)
  560. new->xefi_oinfo = *oinfo;
  561. else
  562. xfs_rmap_skip_owner_update(&new->xefi_oinfo);
  563. trace_xfs_bmap_free_defer(mp, XFS_FSB_TO_AGNO(mp, bno), 0,
  564. XFS_FSB_TO_AGBNO(mp, bno), len);
  565. xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list);
  566. }
  567. /*
  568. * Inode fork format manipulation functions
  569. */
  570. /*
  571. * Transform a btree format file with only one leaf node, where the
  572. * extents list will fit in the inode, into an extents format file.
  573. * Since the file extents are already in-core, all we have to do is
  574. * give up the space for the btree root and pitch the leaf block.
  575. */
  576. STATIC int /* error */
  577. xfs_bmap_btree_to_extents(
  578. xfs_trans_t *tp, /* transaction pointer */
  579. xfs_inode_t *ip, /* incore inode pointer */
  580. xfs_btree_cur_t *cur, /* btree cursor */
  581. int *logflagsp, /* inode logging flags */
  582. int whichfork) /* data or attr fork */
  583. {
  584. /* REFERENCED */
  585. struct xfs_btree_block *cblock;/* child btree block */
  586. xfs_fsblock_t cbno; /* child block number */
  587. xfs_buf_t *cbp; /* child block's buffer */
  588. int error; /* error return value */
  589. xfs_ifork_t *ifp; /* inode fork data */
  590. xfs_mount_t *mp; /* mount point structure */
  591. __be64 *pp; /* ptr to block address */
  592. struct xfs_btree_block *rblock;/* root btree block */
  593. struct xfs_owner_info oinfo;
  594. mp = ip->i_mount;
  595. ifp = XFS_IFORK_PTR(ip, whichfork);
  596. ASSERT(whichfork != XFS_COW_FORK);
  597. ASSERT(ifp->if_flags & XFS_IFEXTENTS);
  598. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
  599. rblock = ifp->if_broot;
  600. ASSERT(be16_to_cpu(rblock->bb_level) == 1);
  601. ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
  602. ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
  603. pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
  604. cbno = be64_to_cpu(*pp);
  605. *logflagsp = 0;
  606. #ifdef DEBUG
  607. if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
  608. return error;
  609. #endif
  610. error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
  611. &xfs_bmbt_buf_ops);
  612. if (error)
  613. return error;
  614. cblock = XFS_BUF_TO_BLOCK(cbp);
  615. if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
  616. return error;
  617. xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
  618. xfs_bmap_add_free(mp, cur->bc_private.b.dfops, cbno, 1, &oinfo);
  619. ip->i_d.di_nblocks--;
  620. xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
  621. xfs_trans_binval(tp, cbp);
  622. if (cur->bc_bufs[0] == cbp)
  623. cur->bc_bufs[0] = NULL;
  624. xfs_iroot_realloc(ip, -1, whichfork);
  625. ASSERT(ifp->if_broot == NULL);
  626. ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
  627. XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
  628. *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
  629. return 0;
  630. }
  631. /*
  632. * Convert an extents-format file into a btree-format file.
  633. * The new file will have a root block (in the inode) and a single child block.
  634. */
  635. STATIC int /* error */
  636. xfs_bmap_extents_to_btree(
  637. xfs_trans_t *tp, /* transaction pointer */
  638. xfs_inode_t *ip, /* incore inode pointer */
  639. xfs_fsblock_t *firstblock, /* first-block-allocated */
  640. struct xfs_defer_ops *dfops, /* blocks freed in xaction */
  641. xfs_btree_cur_t **curp, /* cursor returned to caller */
  642. int wasdel, /* converting a delayed alloc */
  643. int *logflagsp, /* inode logging flags */
  644. int whichfork) /* data or attr fork */
  645. {
  646. struct xfs_btree_block *ablock; /* allocated (child) bt block */
  647. xfs_buf_t *abp; /* buffer for ablock */
  648. xfs_alloc_arg_t args; /* allocation arguments */
  649. xfs_bmbt_rec_t *arp; /* child record pointer */
  650. struct xfs_btree_block *block; /* btree root block */
  651. xfs_btree_cur_t *cur; /* bmap btree cursor */
  652. xfs_bmbt_rec_host_t *ep; /* extent record pointer */
  653. int error; /* error return value */
  654. xfs_extnum_t i, cnt; /* extent record index */
  655. xfs_ifork_t *ifp; /* inode fork pointer */
  656. xfs_bmbt_key_t *kp; /* root block key pointer */
  657. xfs_mount_t *mp; /* mount structure */
  658. xfs_extnum_t nextents; /* number of file extents */
  659. xfs_bmbt_ptr_t *pp; /* root block address pointer */
  660. mp = ip->i_mount;
  661. ASSERT(whichfork != XFS_COW_FORK);
  662. ifp = XFS_IFORK_PTR(ip, whichfork);
  663. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
  664. /*
  665. * Make space in the inode incore.
  666. */
  667. xfs_iroot_realloc(ip, 1, whichfork);
  668. ifp->if_flags |= XFS_IFBROOT;
  669. /*
  670. * Fill in the root.
  671. */
  672. block = ifp->if_broot;
  673. xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
  674. XFS_BTNUM_BMAP, 1, 1, ip->i_ino,
  675. XFS_BTREE_LONG_PTRS);
  676. /*
  677. * Need a cursor. Can't allocate until bb_level is filled in.
  678. */
  679. cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
  680. cur->bc_private.b.firstblock = *firstblock;
  681. cur->bc_private.b.dfops = dfops;
  682. cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
  683. /*
  684. * Convert to a btree with two levels, one record in root.
  685. */
  686. XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
  687. memset(&args, 0, sizeof(args));
  688. args.tp = tp;
  689. args.mp = mp;
  690. xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
  691. args.firstblock = *firstblock;
  692. if (*firstblock == NULLFSBLOCK) {
  693. args.type = XFS_ALLOCTYPE_START_BNO;
  694. args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
  695. } else if (dfops->dop_low) {
  696. args.type = XFS_ALLOCTYPE_START_BNO;
  697. args.fsbno = *firstblock;
  698. } else {
  699. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  700. args.fsbno = *firstblock;
  701. }
  702. args.minlen = args.maxlen = args.prod = 1;
  703. args.wasdel = wasdel;
  704. *logflagsp = 0;
  705. if ((error = xfs_alloc_vextent(&args))) {
  706. xfs_iroot_realloc(ip, -1, whichfork);
  707. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  708. return error;
  709. }
  710. if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
  711. xfs_iroot_realloc(ip, -1, whichfork);
  712. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  713. return -ENOSPC;
  714. }
  715. /*
  716. * Allocation can't fail, the space was reserved.
  717. */
  718. ASSERT(*firstblock == NULLFSBLOCK ||
  719. args.agno >= XFS_FSB_TO_AGNO(mp, *firstblock));
  720. *firstblock = cur->bc_private.b.firstblock = args.fsbno;
  721. cur->bc_private.b.allocated++;
  722. ip->i_d.di_nblocks++;
  723. xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
  724. abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
  725. /*
  726. * Fill in the child block.
  727. */
  728. abp->b_ops = &xfs_bmbt_buf_ops;
  729. ablock = XFS_BUF_TO_BLOCK(abp);
  730. xfs_btree_init_block_int(mp, ablock, abp->b_bn,
  731. XFS_BTNUM_BMAP, 0, 0, ip->i_ino,
  732. XFS_BTREE_LONG_PTRS);
  733. arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
  734. nextents = xfs_iext_count(ifp);
  735. for (cnt = i = 0; i < nextents; i++) {
  736. ep = xfs_iext_get_ext(ifp, i);
  737. if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
  738. arp->l0 = cpu_to_be64(ep->l0);
  739. arp->l1 = cpu_to_be64(ep->l1);
  740. arp++; cnt++;
  741. }
  742. }
  743. ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
  744. xfs_btree_set_numrecs(ablock, cnt);
  745. /*
  746. * Fill in the root key and pointer.
  747. */
  748. kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
  749. arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
  750. kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
  751. pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
  752. be16_to_cpu(block->bb_level)));
  753. *pp = cpu_to_be64(args.fsbno);
  754. /*
  755. * Do all this logging at the end so that
  756. * the root is at the right level.
  757. */
  758. xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
  759. xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
  760. ASSERT(*curp == NULL);
  761. *curp = cur;
  762. *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
  763. return 0;
  764. }
  765. /*
  766. * Convert a local file to an extents file.
  767. * This code is out of bounds for data forks of regular files,
  768. * since the file data needs to get logged so things will stay consistent.
  769. * (The bmap-level manipulations are ok, though).
  770. */
  771. void
  772. xfs_bmap_local_to_extents_empty(
  773. struct xfs_inode *ip,
  774. int whichfork)
  775. {
  776. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
  777. ASSERT(whichfork != XFS_COW_FORK);
  778. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
  779. ASSERT(ifp->if_bytes == 0);
  780. ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
  781. xfs_bmap_forkoff_reset(ip, whichfork);
  782. ifp->if_flags &= ~XFS_IFINLINE;
  783. ifp->if_flags |= XFS_IFEXTENTS;
  784. XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
  785. }
  786. STATIC int /* error */
  787. xfs_bmap_local_to_extents(
  788. xfs_trans_t *tp, /* transaction pointer */
  789. xfs_inode_t *ip, /* incore inode pointer */
  790. xfs_fsblock_t *firstblock, /* first block allocated in xaction */
  791. xfs_extlen_t total, /* total blocks needed by transaction */
  792. int *logflagsp, /* inode logging flags */
  793. int whichfork,
  794. void (*init_fn)(struct xfs_trans *tp,
  795. struct xfs_buf *bp,
  796. struct xfs_inode *ip,
  797. struct xfs_ifork *ifp))
  798. {
  799. int error = 0;
  800. int flags; /* logging flags returned */
  801. xfs_ifork_t *ifp; /* inode fork pointer */
  802. xfs_alloc_arg_t args; /* allocation arguments */
  803. xfs_buf_t *bp; /* buffer for extent block */
  804. xfs_bmbt_rec_host_t *ep; /* extent record pointer */
  805. /*
  806. * We don't want to deal with the case of keeping inode data inline yet.
  807. * So sending the data fork of a regular inode is invalid.
  808. */
  809. ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
  810. ifp = XFS_IFORK_PTR(ip, whichfork);
  811. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
  812. if (!ifp->if_bytes) {
  813. xfs_bmap_local_to_extents_empty(ip, whichfork);
  814. flags = XFS_ILOG_CORE;
  815. goto done;
  816. }
  817. flags = 0;
  818. error = 0;
  819. ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) ==
  820. XFS_IFINLINE);
  821. memset(&args, 0, sizeof(args));
  822. args.tp = tp;
  823. args.mp = ip->i_mount;
  824. xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
  825. args.firstblock = *firstblock;
  826. /*
  827. * Allocate a block. We know we need only one, since the
  828. * file currently fits in an inode.
  829. */
  830. if (*firstblock == NULLFSBLOCK) {
  831. args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
  832. args.type = XFS_ALLOCTYPE_START_BNO;
  833. } else {
  834. args.fsbno = *firstblock;
  835. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  836. }
  837. args.total = total;
  838. args.minlen = args.maxlen = args.prod = 1;
  839. error = xfs_alloc_vextent(&args);
  840. if (error)
  841. goto done;
  842. /* Can't fail, the space was reserved. */
  843. ASSERT(args.fsbno != NULLFSBLOCK);
  844. ASSERT(args.len == 1);
  845. *firstblock = args.fsbno;
  846. bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
  847. /*
  848. * Initialize the block, copy the data and log the remote buffer.
  849. *
  850. * The callout is responsible for logging because the remote format
  851. * might differ from the local format and thus we don't know how much to
  852. * log here. Note that init_fn must also set the buffer log item type
  853. * correctly.
  854. */
  855. init_fn(tp, bp, ip, ifp);
  856. /* account for the change in fork size */
  857. xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
  858. xfs_bmap_local_to_extents_empty(ip, whichfork);
  859. flags |= XFS_ILOG_CORE;
  860. xfs_iext_add(ifp, 0, 1);
  861. ep = xfs_iext_get_ext(ifp, 0);
  862. xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
  863. trace_xfs_bmap_post_update(ip, 0,
  864. whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
  865. _THIS_IP_);
  866. XFS_IFORK_NEXT_SET(ip, whichfork, 1);
  867. ip->i_d.di_nblocks = 1;
  868. xfs_trans_mod_dquot_byino(tp, ip,
  869. XFS_TRANS_DQ_BCOUNT, 1L);
  870. flags |= xfs_ilog_fext(whichfork);
  871. done:
  872. *logflagsp = flags;
  873. return error;
  874. }
  875. /*
  876. * Called from xfs_bmap_add_attrfork to handle btree format files.
  877. */
  878. STATIC int /* error */
  879. xfs_bmap_add_attrfork_btree(
  880. xfs_trans_t *tp, /* transaction pointer */
  881. xfs_inode_t *ip, /* incore inode pointer */
  882. xfs_fsblock_t *firstblock, /* first block allocated */
  883. struct xfs_defer_ops *dfops, /* blocks to free at commit */
  884. int *flags) /* inode logging flags */
  885. {
  886. xfs_btree_cur_t *cur; /* btree cursor */
  887. int error; /* error return value */
  888. xfs_mount_t *mp; /* file system mount struct */
  889. int stat; /* newroot status */
  890. mp = ip->i_mount;
  891. if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
  892. *flags |= XFS_ILOG_DBROOT;
  893. else {
  894. cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
  895. cur->bc_private.b.dfops = dfops;
  896. cur->bc_private.b.firstblock = *firstblock;
  897. if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
  898. goto error0;
  899. /* must be at least one entry */
  900. XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
  901. if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
  902. goto error0;
  903. if (stat == 0) {
  904. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  905. return -ENOSPC;
  906. }
  907. *firstblock = cur->bc_private.b.firstblock;
  908. cur->bc_private.b.allocated = 0;
  909. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  910. }
  911. return 0;
  912. error0:
  913. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  914. return error;
  915. }
  916. /*
  917. * Called from xfs_bmap_add_attrfork to handle extents format files.
  918. */
  919. STATIC int /* error */
  920. xfs_bmap_add_attrfork_extents(
  921. xfs_trans_t *tp, /* transaction pointer */
  922. xfs_inode_t *ip, /* incore inode pointer */
  923. xfs_fsblock_t *firstblock, /* first block allocated */
  924. struct xfs_defer_ops *dfops, /* blocks to free at commit */
  925. int *flags) /* inode logging flags */
  926. {
  927. xfs_btree_cur_t *cur; /* bmap btree cursor */
  928. int error; /* error return value */
  929. if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
  930. return 0;
  931. cur = NULL;
  932. error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0,
  933. flags, XFS_DATA_FORK);
  934. if (cur) {
  935. cur->bc_private.b.allocated = 0;
  936. xfs_btree_del_cursor(cur,
  937. error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  938. }
  939. return error;
  940. }
  941. /*
  942. * Called from xfs_bmap_add_attrfork to handle local format files. Each
  943. * different data fork content type needs a different callout to do the
  944. * conversion. Some are basic and only require special block initialisation
  945. * callouts for the data formating, others (directories) are so specialised they
  946. * handle everything themselves.
  947. *
  948. * XXX (dgc): investigate whether directory conversion can use the generic
  949. * formatting callout. It should be possible - it's just a very complex
  950. * formatter.
  951. */
  952. STATIC int /* error */
  953. xfs_bmap_add_attrfork_local(
  954. xfs_trans_t *tp, /* transaction pointer */
  955. xfs_inode_t *ip, /* incore inode pointer */
  956. xfs_fsblock_t *firstblock, /* first block allocated */
  957. struct xfs_defer_ops *dfops, /* blocks to free at commit */
  958. int *flags) /* inode logging flags */
  959. {
  960. xfs_da_args_t dargs; /* args for dir/attr code */
  961. if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
  962. return 0;
  963. if (S_ISDIR(VFS_I(ip)->i_mode)) {
  964. memset(&dargs, 0, sizeof(dargs));
  965. dargs.geo = ip->i_mount->m_dir_geo;
  966. dargs.dp = ip;
  967. dargs.firstblock = firstblock;
  968. dargs.dfops = dfops;
  969. dargs.total = dargs.geo->fsbcount;
  970. dargs.whichfork = XFS_DATA_FORK;
  971. dargs.trans = tp;
  972. return xfs_dir2_sf_to_block(&dargs);
  973. }
  974. if (S_ISLNK(VFS_I(ip)->i_mode))
  975. return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
  976. flags, XFS_DATA_FORK,
  977. xfs_symlink_local_to_remote);
  978. /* should only be called for types that support local format data */
  979. ASSERT(0);
  980. return -EFSCORRUPTED;
  981. }
  982. /*
  983. * Convert inode from non-attributed to attributed.
  984. * Must not be in a transaction, ip must not be locked.
  985. */
  986. int /* error code */
  987. xfs_bmap_add_attrfork(
  988. xfs_inode_t *ip, /* incore inode pointer */
  989. int size, /* space new attribute needs */
  990. int rsvd) /* xact may use reserved blks */
  991. {
  992. xfs_fsblock_t firstblock; /* 1st block/ag allocated */
  993. struct xfs_defer_ops dfops; /* freed extent records */
  994. xfs_mount_t *mp; /* mount structure */
  995. xfs_trans_t *tp; /* transaction pointer */
  996. int blks; /* space reservation */
  997. int version = 1; /* superblock attr version */
  998. int logflags; /* logging flags */
  999. int error; /* error return value */
  1000. ASSERT(XFS_IFORK_Q(ip) == 0);
  1001. mp = ip->i_mount;
  1002. ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
  1003. blks = XFS_ADDAFORK_SPACE_RES(mp);
  1004. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
  1005. rsvd ? XFS_TRANS_RESERVE : 0, &tp);
  1006. if (error)
  1007. return error;
  1008. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1009. error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
  1010. XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
  1011. XFS_QMOPT_RES_REGBLKS);
  1012. if (error)
  1013. goto trans_cancel;
  1014. if (XFS_IFORK_Q(ip))
  1015. goto trans_cancel;
  1016. if (ip->i_d.di_anextents != 0) {
  1017. error = -EFSCORRUPTED;
  1018. goto trans_cancel;
  1019. }
  1020. if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
  1021. /*
  1022. * For inodes coming from pre-6.2 filesystems.
  1023. */
  1024. ASSERT(ip->i_d.di_aformat == 0);
  1025. ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
  1026. }
  1027. xfs_trans_ijoin(tp, ip, 0);
  1028. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1029. switch (ip->i_d.di_format) {
  1030. case XFS_DINODE_FMT_DEV:
  1031. ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
  1032. break;
  1033. case XFS_DINODE_FMT_UUID:
  1034. ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
  1035. break;
  1036. case XFS_DINODE_FMT_LOCAL:
  1037. case XFS_DINODE_FMT_EXTENTS:
  1038. case XFS_DINODE_FMT_BTREE:
  1039. ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
  1040. if (!ip->i_d.di_forkoff)
  1041. ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
  1042. else if (mp->m_flags & XFS_MOUNT_ATTR2)
  1043. version = 2;
  1044. break;
  1045. default:
  1046. ASSERT(0);
  1047. error = -EINVAL;
  1048. goto trans_cancel;
  1049. }
  1050. ASSERT(ip->i_afp == NULL);
  1051. ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
  1052. ip->i_afp->if_flags = XFS_IFEXTENTS;
  1053. logflags = 0;
  1054. xfs_defer_init(&dfops, &firstblock);
  1055. switch (ip->i_d.di_format) {
  1056. case XFS_DINODE_FMT_LOCAL:
  1057. error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops,
  1058. &logflags);
  1059. break;
  1060. case XFS_DINODE_FMT_EXTENTS:
  1061. error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
  1062. &dfops, &logflags);
  1063. break;
  1064. case XFS_DINODE_FMT_BTREE:
  1065. error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops,
  1066. &logflags);
  1067. break;
  1068. default:
  1069. error = 0;
  1070. break;
  1071. }
  1072. if (logflags)
  1073. xfs_trans_log_inode(tp, ip, logflags);
  1074. if (error)
  1075. goto bmap_cancel;
  1076. if (!xfs_sb_version_hasattr(&mp->m_sb) ||
  1077. (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
  1078. bool log_sb = false;
  1079. spin_lock(&mp->m_sb_lock);
  1080. if (!xfs_sb_version_hasattr(&mp->m_sb)) {
  1081. xfs_sb_version_addattr(&mp->m_sb);
  1082. log_sb = true;
  1083. }
  1084. if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
  1085. xfs_sb_version_addattr2(&mp->m_sb);
  1086. log_sb = true;
  1087. }
  1088. spin_unlock(&mp->m_sb_lock);
  1089. if (log_sb)
  1090. xfs_log_sb(tp);
  1091. }
  1092. error = xfs_defer_finish(&tp, &dfops, NULL);
  1093. if (error)
  1094. goto bmap_cancel;
  1095. error = xfs_trans_commit(tp);
  1096. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1097. return error;
  1098. bmap_cancel:
  1099. xfs_defer_cancel(&dfops);
  1100. trans_cancel:
  1101. xfs_trans_cancel(tp);
  1102. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1103. return error;
  1104. }
  1105. /*
  1106. * Internal and external extent tree search functions.
  1107. */
  1108. /*
  1109. * Read in the extents to if_extents.
  1110. * All inode fields are set up by caller, we just traverse the btree
  1111. * and copy the records in. If the file system cannot contain unwritten
  1112. * extents, the records are checked for no "state" flags.
  1113. */
  1114. int /* error */
  1115. xfs_bmap_read_extents(
  1116. xfs_trans_t *tp, /* transaction pointer */
  1117. xfs_inode_t *ip, /* incore inode */
  1118. int whichfork) /* data or attr fork */
  1119. {
  1120. struct xfs_btree_block *block; /* current btree block */
  1121. xfs_fsblock_t bno; /* block # of "block" */
  1122. xfs_buf_t *bp; /* buffer for "block" */
  1123. int error; /* error return value */
  1124. xfs_extnum_t i, j; /* index into the extents list */
  1125. xfs_ifork_t *ifp; /* fork structure */
  1126. int level; /* btree level, for checking */
  1127. xfs_mount_t *mp; /* file system mount structure */
  1128. __be64 *pp; /* pointer to block address */
  1129. /* REFERENCED */
  1130. xfs_extnum_t room; /* number of entries there's room for */
  1131. mp = ip->i_mount;
  1132. ifp = XFS_IFORK_PTR(ip, whichfork);
  1133. block = ifp->if_broot;
  1134. /*
  1135. * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
  1136. */
  1137. level = be16_to_cpu(block->bb_level);
  1138. ASSERT(level > 0);
  1139. pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
  1140. bno = be64_to_cpu(*pp);
  1141. /*
  1142. * Go down the tree until leaf level is reached, following the first
  1143. * pointer (leftmost) at each level.
  1144. */
  1145. while (level-- > 0) {
  1146. error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
  1147. XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
  1148. if (error)
  1149. return error;
  1150. block = XFS_BUF_TO_BLOCK(bp);
  1151. if (level == 0)
  1152. break;
  1153. pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
  1154. bno = be64_to_cpu(*pp);
  1155. XFS_WANT_CORRUPTED_GOTO(mp,
  1156. XFS_FSB_SANITY_CHECK(mp, bno), error0);
  1157. xfs_trans_brelse(tp, bp);
  1158. }
  1159. /*
  1160. * Here with bp and block set to the leftmost leaf node in the tree.
  1161. */
  1162. room = xfs_iext_count(ifp);
  1163. i = 0;
  1164. /*
  1165. * Loop over all leaf nodes. Copy information to the extent records.
  1166. */
  1167. for (;;) {
  1168. xfs_bmbt_rec_t *frp;
  1169. xfs_fsblock_t nextbno;
  1170. xfs_extnum_t num_recs;
  1171. xfs_extnum_t start;
  1172. num_recs = xfs_btree_get_numrecs(block);
  1173. if (unlikely(i + num_recs > room)) {
  1174. ASSERT(i + num_recs <= room);
  1175. xfs_warn(ip->i_mount,
  1176. "corrupt dinode %Lu, (btree extents).",
  1177. (unsigned long long) ip->i_ino);
  1178. XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
  1179. XFS_ERRLEVEL_LOW, ip->i_mount, block);
  1180. goto error0;
  1181. }
  1182. /*
  1183. * Read-ahead the next leaf block, if any.
  1184. */
  1185. nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
  1186. if (nextbno != NULLFSBLOCK)
  1187. xfs_btree_reada_bufl(mp, nextbno, 1,
  1188. &xfs_bmbt_buf_ops);
  1189. /*
  1190. * Copy records into the extent records.
  1191. */
  1192. frp = XFS_BMBT_REC_ADDR(mp, block, 1);
  1193. start = i;
  1194. for (j = 0; j < num_recs; j++, i++, frp++) {
  1195. xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
  1196. trp->l0 = be64_to_cpu(frp->l0);
  1197. trp->l1 = be64_to_cpu(frp->l1);
  1198. if (!xfs_bmbt_validate_extent(mp, whichfork, trp)) {
  1199. XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
  1200. XFS_ERRLEVEL_LOW, mp);
  1201. goto error0;
  1202. }
  1203. }
  1204. xfs_trans_brelse(tp, bp);
  1205. bno = nextbno;
  1206. /*
  1207. * If we've reached the end, stop.
  1208. */
  1209. if (bno == NULLFSBLOCK)
  1210. break;
  1211. error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
  1212. XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
  1213. if (error)
  1214. return error;
  1215. block = XFS_BUF_TO_BLOCK(bp);
  1216. }
  1217. if (i != XFS_IFORK_NEXTENTS(ip, whichfork))
  1218. return -EFSCORRUPTED;
  1219. ASSERT(i == xfs_iext_count(ifp));
  1220. XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
  1221. return 0;
  1222. error0:
  1223. xfs_trans_brelse(tp, bp);
  1224. return -EFSCORRUPTED;
  1225. }
  1226. /*
  1227. * Returns the file-relative block number of the first unused block(s)
  1228. * in the file with at least "len" logically contiguous blocks free.
  1229. * This is the lowest-address hole if the file has holes, else the first block
  1230. * past the end of file.
  1231. * Return 0 if the file is currently local (in-inode).
  1232. */
  1233. int /* error */
  1234. xfs_bmap_first_unused(
  1235. xfs_trans_t *tp, /* transaction pointer */
  1236. xfs_inode_t *ip, /* incore inode */
  1237. xfs_extlen_t len, /* size of hole to find */
  1238. xfs_fileoff_t *first_unused, /* unused block */
  1239. int whichfork) /* data or attr fork */
  1240. {
  1241. int error; /* error return value */
  1242. int idx; /* extent record index */
  1243. xfs_ifork_t *ifp; /* inode fork pointer */
  1244. xfs_fileoff_t lastaddr; /* last block number seen */
  1245. xfs_fileoff_t lowest; /* lowest useful block */
  1246. xfs_fileoff_t max; /* starting useful block */
  1247. xfs_fileoff_t off; /* offset for this block */
  1248. xfs_extnum_t nextents; /* number of extent entries */
  1249. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
  1250. XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
  1251. XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
  1252. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
  1253. *first_unused = 0;
  1254. return 0;
  1255. }
  1256. ifp = XFS_IFORK_PTR(ip, whichfork);
  1257. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  1258. (error = xfs_iread_extents(tp, ip, whichfork)))
  1259. return error;
  1260. lowest = *first_unused;
  1261. nextents = xfs_iext_count(ifp);
  1262. for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
  1263. xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
  1264. off = xfs_bmbt_get_startoff(ep);
  1265. /*
  1266. * See if the hole before this extent will work.
  1267. */
  1268. if (off >= lowest + len && off - max >= len) {
  1269. *first_unused = max;
  1270. return 0;
  1271. }
  1272. lastaddr = off + xfs_bmbt_get_blockcount(ep);
  1273. max = XFS_FILEOFF_MAX(lastaddr, lowest);
  1274. }
  1275. *first_unused = max;
  1276. return 0;
  1277. }
  1278. /*
  1279. * Returns the file-relative block number of the last block - 1 before
  1280. * last_block (input value) in the file.
  1281. * This is not based on i_size, it is based on the extent records.
  1282. * Returns 0 for local files, as they do not have extent records.
  1283. */
  1284. int /* error */
  1285. xfs_bmap_last_before(
  1286. struct xfs_trans *tp, /* transaction pointer */
  1287. struct xfs_inode *ip, /* incore inode */
  1288. xfs_fileoff_t *last_block, /* last block */
  1289. int whichfork) /* data or attr fork */
  1290. {
  1291. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
  1292. struct xfs_bmbt_irec got;
  1293. xfs_extnum_t idx;
  1294. int error;
  1295. switch (XFS_IFORK_FORMAT(ip, whichfork)) {
  1296. case XFS_DINODE_FMT_LOCAL:
  1297. *last_block = 0;
  1298. return 0;
  1299. case XFS_DINODE_FMT_BTREE:
  1300. case XFS_DINODE_FMT_EXTENTS:
  1301. break;
  1302. default:
  1303. return -EIO;
  1304. }
  1305. if (!(ifp->if_flags & XFS_IFEXTENTS)) {
  1306. error = xfs_iread_extents(tp, ip, whichfork);
  1307. if (error)
  1308. return error;
  1309. }
  1310. if (xfs_iext_lookup_extent(ip, ifp, *last_block - 1, &idx, &got)) {
  1311. if (got.br_startoff <= *last_block - 1)
  1312. return 0;
  1313. }
  1314. if (xfs_iext_get_extent(ifp, idx - 1, &got)) {
  1315. *last_block = got.br_startoff + got.br_blockcount;
  1316. return 0;
  1317. }
  1318. *last_block = 0;
  1319. return 0;
  1320. }
  1321. int
  1322. xfs_bmap_last_extent(
  1323. struct xfs_trans *tp,
  1324. struct xfs_inode *ip,
  1325. int whichfork,
  1326. struct xfs_bmbt_irec *rec,
  1327. int *is_empty)
  1328. {
  1329. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
  1330. int error;
  1331. int nextents;
  1332. if (!(ifp->if_flags & XFS_IFEXTENTS)) {
  1333. error = xfs_iread_extents(tp, ip, whichfork);
  1334. if (error)
  1335. return error;
  1336. }
  1337. nextents = xfs_iext_count(ifp);
  1338. if (nextents == 0) {
  1339. *is_empty = 1;
  1340. return 0;
  1341. }
  1342. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec);
  1343. *is_empty = 0;
  1344. return 0;
  1345. }
  1346. /*
  1347. * Check the last inode extent to determine whether this allocation will result
  1348. * in blocks being allocated at the end of the file. When we allocate new data
  1349. * blocks at the end of the file which do not start at the previous data block,
  1350. * we will try to align the new blocks at stripe unit boundaries.
  1351. *
  1352. * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
  1353. * at, or past the EOF.
  1354. */
  1355. STATIC int
  1356. xfs_bmap_isaeof(
  1357. struct xfs_bmalloca *bma,
  1358. int whichfork)
  1359. {
  1360. struct xfs_bmbt_irec rec;
  1361. int is_empty;
  1362. int error;
  1363. bma->aeof = 0;
  1364. error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
  1365. &is_empty);
  1366. if (error)
  1367. return error;
  1368. if (is_empty) {
  1369. bma->aeof = 1;
  1370. return 0;
  1371. }
  1372. /*
  1373. * Check if we are allocation or past the last extent, or at least into
  1374. * the last delayed allocated extent.
  1375. */
  1376. bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
  1377. (bma->offset >= rec.br_startoff &&
  1378. isnullstartblock(rec.br_startblock));
  1379. return 0;
  1380. }
  1381. /*
  1382. * Returns the file-relative block number of the first block past eof in
  1383. * the file. This is not based on i_size, it is based on the extent records.
  1384. * Returns 0 for local files, as they do not have extent records.
  1385. */
  1386. int
  1387. xfs_bmap_last_offset(
  1388. struct xfs_inode *ip,
  1389. xfs_fileoff_t *last_block,
  1390. int whichfork)
  1391. {
  1392. struct xfs_bmbt_irec rec;
  1393. int is_empty;
  1394. int error;
  1395. *last_block = 0;
  1396. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
  1397. return 0;
  1398. if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
  1399. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
  1400. return -EIO;
  1401. error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
  1402. if (error || is_empty)
  1403. return error;
  1404. *last_block = rec.br_startoff + rec.br_blockcount;
  1405. return 0;
  1406. }
  1407. /*
  1408. * Returns whether the selected fork of the inode has exactly one
  1409. * block or not. For the data fork we check this matches di_size,
  1410. * implying the file's range is 0..bsize-1.
  1411. */
  1412. int /* 1=>1 block, 0=>otherwise */
  1413. xfs_bmap_one_block(
  1414. xfs_inode_t *ip, /* incore inode */
  1415. int whichfork) /* data or attr fork */
  1416. {
  1417. xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
  1418. xfs_ifork_t *ifp; /* inode fork pointer */
  1419. int rval; /* return value */
  1420. xfs_bmbt_irec_t s; /* internal version of extent */
  1421. #ifndef DEBUG
  1422. if (whichfork == XFS_DATA_FORK)
  1423. return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
  1424. #endif /* !DEBUG */
  1425. if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
  1426. return 0;
  1427. if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
  1428. return 0;
  1429. ifp = XFS_IFORK_PTR(ip, whichfork);
  1430. ASSERT(ifp->if_flags & XFS_IFEXTENTS);
  1431. ep = xfs_iext_get_ext(ifp, 0);
  1432. xfs_bmbt_get_all(ep, &s);
  1433. rval = s.br_startoff == 0 && s.br_blockcount == 1;
  1434. if (rval && whichfork == XFS_DATA_FORK)
  1435. ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
  1436. return rval;
  1437. }
  1438. /*
  1439. * Extent tree manipulation functions used during allocation.
  1440. */
  1441. /*
  1442. * Convert a delayed allocation to a real allocation.
  1443. */
  1444. STATIC int /* error */
  1445. xfs_bmap_add_extent_delay_real(
  1446. struct xfs_bmalloca *bma,
  1447. int whichfork)
  1448. {
  1449. struct xfs_bmbt_irec *new = &bma->got;
  1450. int diff; /* temp value */
  1451. xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
  1452. int error; /* error return value */
  1453. int i; /* temp state */
  1454. xfs_ifork_t *ifp; /* inode fork pointer */
  1455. xfs_fileoff_t new_endoff; /* end offset of new entry */
  1456. xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
  1457. /* left is 0, right is 1, prev is 2 */
  1458. int rval=0; /* return value (logging flags) */
  1459. int state = 0;/* state bits, accessed thru macros */
  1460. xfs_filblks_t da_new; /* new count del alloc blocks used */
  1461. xfs_filblks_t da_old; /* old count del alloc blocks used */
  1462. xfs_filblks_t temp=0; /* value for da_new calculations */
  1463. xfs_filblks_t temp2=0;/* value for da_new calculations */
  1464. int tmp_rval; /* partial logging flags */
  1465. struct xfs_mount *mp;
  1466. xfs_extnum_t *nextents;
  1467. mp = bma->ip->i_mount;
  1468. ifp = XFS_IFORK_PTR(bma->ip, whichfork);
  1469. ASSERT(whichfork != XFS_ATTR_FORK);
  1470. nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
  1471. &bma->ip->i_d.di_nextents);
  1472. ASSERT(bma->idx >= 0);
  1473. ASSERT(bma->idx <= xfs_iext_count(ifp));
  1474. ASSERT(!isnullstartblock(new->br_startblock));
  1475. ASSERT(!bma->cur ||
  1476. (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
  1477. XFS_STATS_INC(mp, xs_add_exlist);
  1478. #define LEFT r[0]
  1479. #define RIGHT r[1]
  1480. #define PREV r[2]
  1481. if (whichfork == XFS_COW_FORK)
  1482. state |= BMAP_COWFORK;
  1483. /*
  1484. * Set up a bunch of variables to make the tests simpler.
  1485. */
  1486. ep = xfs_iext_get_ext(ifp, bma->idx);
  1487. xfs_bmbt_get_all(ep, &PREV);
  1488. new_endoff = new->br_startoff + new->br_blockcount;
  1489. ASSERT(PREV.br_startoff <= new->br_startoff);
  1490. ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
  1491. da_old = startblockval(PREV.br_startblock);
  1492. da_new = 0;
  1493. /*
  1494. * Set flags determining what part of the previous delayed allocation
  1495. * extent is being replaced by a real allocation.
  1496. */
  1497. if (PREV.br_startoff == new->br_startoff)
  1498. state |= BMAP_LEFT_FILLING;
  1499. if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
  1500. state |= BMAP_RIGHT_FILLING;
  1501. /*
  1502. * Check and set flags if this segment has a left neighbor.
  1503. * Don't set contiguous if the combined extent would be too large.
  1504. */
  1505. if (bma->idx > 0) {
  1506. state |= BMAP_LEFT_VALID;
  1507. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT);
  1508. if (isnullstartblock(LEFT.br_startblock))
  1509. state |= BMAP_LEFT_DELAY;
  1510. }
  1511. if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
  1512. LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
  1513. LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
  1514. LEFT.br_state == new->br_state &&
  1515. LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
  1516. state |= BMAP_LEFT_CONTIG;
  1517. /*
  1518. * Check and set flags if this segment has a right neighbor.
  1519. * Don't set contiguous if the combined extent would be too large.
  1520. * Also check for all-three-contiguous being too large.
  1521. */
  1522. if (bma->idx < xfs_iext_count(ifp) - 1) {
  1523. state |= BMAP_RIGHT_VALID;
  1524. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT);
  1525. if (isnullstartblock(RIGHT.br_startblock))
  1526. state |= BMAP_RIGHT_DELAY;
  1527. }
  1528. if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
  1529. new_endoff == RIGHT.br_startoff &&
  1530. new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
  1531. new->br_state == RIGHT.br_state &&
  1532. new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
  1533. ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
  1534. BMAP_RIGHT_FILLING)) !=
  1535. (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
  1536. BMAP_RIGHT_FILLING) ||
  1537. LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
  1538. <= MAXEXTLEN))
  1539. state |= BMAP_RIGHT_CONTIG;
  1540. error = 0;
  1541. /*
  1542. * Switch out based on the FILLING and CONTIG state bits.
  1543. */
  1544. switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
  1545. BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
  1546. case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
  1547. BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
  1548. /*
  1549. * Filling in all of a previously delayed allocation extent.
  1550. * The left and right neighbors are both contiguous with new.
  1551. */
  1552. bma->idx--;
  1553. trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
  1554. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
  1555. LEFT.br_blockcount + PREV.br_blockcount +
  1556. RIGHT.br_blockcount);
  1557. trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
  1558. xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
  1559. (*nextents)--;
  1560. if (bma->cur == NULL)
  1561. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  1562. else {
  1563. rval = XFS_ILOG_CORE;
  1564. error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
  1565. RIGHT.br_startblock,
  1566. RIGHT.br_blockcount, &i);
  1567. if (error)
  1568. goto done;
  1569. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1570. error = xfs_btree_delete(bma->cur, &i);
  1571. if (error)
  1572. goto done;
  1573. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1574. error = xfs_btree_decrement(bma->cur, 0, &i);
  1575. if (error)
  1576. goto done;
  1577. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1578. error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
  1579. LEFT.br_startblock,
  1580. LEFT.br_blockcount +
  1581. PREV.br_blockcount +
  1582. RIGHT.br_blockcount, LEFT.br_state);
  1583. if (error)
  1584. goto done;
  1585. }
  1586. break;
  1587. case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
  1588. /*
  1589. * Filling in all of a previously delayed allocation extent.
  1590. * The left neighbor is contiguous, the right is not.
  1591. */
  1592. bma->idx--;
  1593. trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
  1594. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
  1595. LEFT.br_blockcount + PREV.br_blockcount);
  1596. trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
  1597. xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
  1598. if (bma->cur == NULL)
  1599. rval = XFS_ILOG_DEXT;
  1600. else {
  1601. rval = 0;
  1602. error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
  1603. LEFT.br_startblock, LEFT.br_blockcount,
  1604. &i);
  1605. if (error)
  1606. goto done;
  1607. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1608. error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
  1609. LEFT.br_startblock,
  1610. LEFT.br_blockcount +
  1611. PREV.br_blockcount, LEFT.br_state);
  1612. if (error)
  1613. goto done;
  1614. }
  1615. break;
  1616. case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
  1617. /*
  1618. * Filling in all of a previously delayed allocation extent.
  1619. * The right neighbor is contiguous, the left is not.
  1620. */
  1621. trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
  1622. xfs_bmbt_set_startblock(ep, new->br_startblock);
  1623. xfs_bmbt_set_blockcount(ep,
  1624. PREV.br_blockcount + RIGHT.br_blockcount);
  1625. trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
  1626. xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
  1627. if (bma->cur == NULL)
  1628. rval = XFS_ILOG_DEXT;
  1629. else {
  1630. rval = 0;
  1631. error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
  1632. RIGHT.br_startblock,
  1633. RIGHT.br_blockcount, &i);
  1634. if (error)
  1635. goto done;
  1636. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1637. error = xfs_bmbt_update(bma->cur, PREV.br_startoff,
  1638. new->br_startblock,
  1639. PREV.br_blockcount +
  1640. RIGHT.br_blockcount, PREV.br_state);
  1641. if (error)
  1642. goto done;
  1643. }
  1644. break;
  1645. case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
  1646. /*
  1647. * Filling in all of a previously delayed allocation extent.
  1648. * Neither the left nor right neighbors are contiguous with
  1649. * the new one.
  1650. */
  1651. trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
  1652. xfs_bmbt_set_startblock(ep, new->br_startblock);
  1653. xfs_bmbt_set_state(ep, new->br_state);
  1654. trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
  1655. (*nextents)++;
  1656. if (bma->cur == NULL)
  1657. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  1658. else {
  1659. rval = XFS_ILOG_CORE;
  1660. error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
  1661. new->br_startblock, new->br_blockcount,
  1662. &i);
  1663. if (error)
  1664. goto done;
  1665. XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
  1666. bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
  1667. error = xfs_btree_insert(bma->cur, &i);
  1668. if (error)
  1669. goto done;
  1670. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1671. }
  1672. break;
  1673. case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
  1674. /*
  1675. * Filling in the first part of a previous delayed allocation.
  1676. * The left neighbor is contiguous.
  1677. */
  1678. trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
  1679. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1),
  1680. LEFT.br_blockcount + new->br_blockcount);
  1681. xfs_bmbt_set_startoff(ep,
  1682. PREV.br_startoff + new->br_blockcount);
  1683. trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
  1684. temp = PREV.br_blockcount - new->br_blockcount;
  1685. trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
  1686. xfs_bmbt_set_blockcount(ep, temp);
  1687. if (bma->cur == NULL)
  1688. rval = XFS_ILOG_DEXT;
  1689. else {
  1690. rval = 0;
  1691. error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
  1692. LEFT.br_startblock, LEFT.br_blockcount,
  1693. &i);
  1694. if (error)
  1695. goto done;
  1696. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1697. error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
  1698. LEFT.br_startblock,
  1699. LEFT.br_blockcount +
  1700. new->br_blockcount,
  1701. LEFT.br_state);
  1702. if (error)
  1703. goto done;
  1704. }
  1705. da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
  1706. startblockval(PREV.br_startblock));
  1707. xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
  1708. trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
  1709. bma->idx--;
  1710. break;
  1711. case BMAP_LEFT_FILLING:
  1712. /*
  1713. * Filling in the first part of a previous delayed allocation.
  1714. * The left neighbor is not contiguous.
  1715. */
  1716. trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
  1717. xfs_bmbt_set_startoff(ep, new_endoff);
  1718. temp = PREV.br_blockcount - new->br_blockcount;
  1719. xfs_bmbt_set_blockcount(ep, temp);
  1720. xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
  1721. (*nextents)++;
  1722. if (bma->cur == NULL)
  1723. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  1724. else {
  1725. rval = XFS_ILOG_CORE;
  1726. error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
  1727. new->br_startblock, new->br_blockcount,
  1728. &i);
  1729. if (error)
  1730. goto done;
  1731. XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
  1732. bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
  1733. error = xfs_btree_insert(bma->cur, &i);
  1734. if (error)
  1735. goto done;
  1736. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1737. }
  1738. if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
  1739. error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
  1740. bma->firstblock, bma->dfops,
  1741. &bma->cur, 1, &tmp_rval, whichfork);
  1742. rval |= tmp_rval;
  1743. if (error)
  1744. goto done;
  1745. }
  1746. da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
  1747. startblockval(PREV.br_startblock) -
  1748. (bma->cur ? bma->cur->bc_private.b.allocated : 0));
  1749. ep = xfs_iext_get_ext(ifp, bma->idx + 1);
  1750. xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
  1751. trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
  1752. break;
  1753. case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
  1754. /*
  1755. * Filling in the last part of a previous delayed allocation.
  1756. * The right neighbor is contiguous with the new allocation.
  1757. */
  1758. temp = PREV.br_blockcount - new->br_blockcount;
  1759. trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
  1760. xfs_bmbt_set_blockcount(ep, temp);
  1761. xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1),
  1762. new->br_startoff, new->br_startblock,
  1763. new->br_blockcount + RIGHT.br_blockcount,
  1764. RIGHT.br_state);
  1765. trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
  1766. if (bma->cur == NULL)
  1767. rval = XFS_ILOG_DEXT;
  1768. else {
  1769. rval = 0;
  1770. error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
  1771. RIGHT.br_startblock,
  1772. RIGHT.br_blockcount, &i);
  1773. if (error)
  1774. goto done;
  1775. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1776. error = xfs_bmbt_update(bma->cur, new->br_startoff,
  1777. new->br_startblock,
  1778. new->br_blockcount +
  1779. RIGHT.br_blockcount,
  1780. RIGHT.br_state);
  1781. if (error)
  1782. goto done;
  1783. }
  1784. da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
  1785. startblockval(PREV.br_startblock));
  1786. trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
  1787. xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
  1788. trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
  1789. bma->idx++;
  1790. break;
  1791. case BMAP_RIGHT_FILLING:
  1792. /*
  1793. * Filling in the last part of a previous delayed allocation.
  1794. * The right neighbor is not contiguous.
  1795. */
  1796. temp = PREV.br_blockcount - new->br_blockcount;
  1797. trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
  1798. xfs_bmbt_set_blockcount(ep, temp);
  1799. xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
  1800. (*nextents)++;
  1801. if (bma->cur == NULL)
  1802. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  1803. else {
  1804. rval = XFS_ILOG_CORE;
  1805. error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
  1806. new->br_startblock, new->br_blockcount,
  1807. &i);
  1808. if (error)
  1809. goto done;
  1810. XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
  1811. bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
  1812. error = xfs_btree_insert(bma->cur, &i);
  1813. if (error)
  1814. goto done;
  1815. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1816. }
  1817. if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
  1818. error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
  1819. bma->firstblock, bma->dfops, &bma->cur, 1,
  1820. &tmp_rval, whichfork);
  1821. rval |= tmp_rval;
  1822. if (error)
  1823. goto done;
  1824. }
  1825. da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
  1826. startblockval(PREV.br_startblock) -
  1827. (bma->cur ? bma->cur->bc_private.b.allocated : 0));
  1828. ep = xfs_iext_get_ext(ifp, bma->idx);
  1829. xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
  1830. trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
  1831. bma->idx++;
  1832. break;
  1833. case 0:
  1834. /*
  1835. * Filling in the middle part of a previous delayed allocation.
  1836. * Contiguity is impossible here.
  1837. * This case is avoided almost all the time.
  1838. *
  1839. * We start with a delayed allocation:
  1840. *
  1841. * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
  1842. * PREV @ idx
  1843. *
  1844. * and we are allocating:
  1845. * +rrrrrrrrrrrrrrrrr+
  1846. * new
  1847. *
  1848. * and we set it up for insertion as:
  1849. * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
  1850. * new
  1851. * PREV @ idx LEFT RIGHT
  1852. * inserted at idx + 1
  1853. */
  1854. temp = new->br_startoff - PREV.br_startoff;
  1855. temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
  1856. trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
  1857. xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
  1858. LEFT = *new;
  1859. RIGHT.br_state = PREV.br_state;
  1860. RIGHT.br_startblock = nullstartblock(
  1861. (int)xfs_bmap_worst_indlen(bma->ip, temp2));
  1862. RIGHT.br_startoff = new_endoff;
  1863. RIGHT.br_blockcount = temp2;
  1864. /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
  1865. xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
  1866. (*nextents)++;
  1867. if (bma->cur == NULL)
  1868. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  1869. else {
  1870. rval = XFS_ILOG_CORE;
  1871. error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
  1872. new->br_startblock, new->br_blockcount,
  1873. &i);
  1874. if (error)
  1875. goto done;
  1876. XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
  1877. bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
  1878. error = xfs_btree_insert(bma->cur, &i);
  1879. if (error)
  1880. goto done;
  1881. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1882. }
  1883. if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
  1884. error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
  1885. bma->firstblock, bma->dfops, &bma->cur,
  1886. 1, &tmp_rval, whichfork);
  1887. rval |= tmp_rval;
  1888. if (error)
  1889. goto done;
  1890. }
  1891. temp = xfs_bmap_worst_indlen(bma->ip, temp);
  1892. temp2 = xfs_bmap_worst_indlen(bma->ip, temp2);
  1893. diff = (int)(temp + temp2 -
  1894. (startblockval(PREV.br_startblock) -
  1895. (bma->cur ?
  1896. bma->cur->bc_private.b.allocated : 0)));
  1897. if (diff > 0) {
  1898. error = xfs_mod_fdblocks(bma->ip->i_mount,
  1899. -((int64_t)diff), false);
  1900. ASSERT(!error);
  1901. if (error)
  1902. goto done;
  1903. }
  1904. ep = xfs_iext_get_ext(ifp, bma->idx);
  1905. xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
  1906. trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
  1907. trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
  1908. xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2),
  1909. nullstartblock((int)temp2));
  1910. trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
  1911. bma->idx++;
  1912. da_new = temp + temp2;
  1913. break;
  1914. case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
  1915. case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
  1916. case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
  1917. case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
  1918. case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
  1919. case BMAP_LEFT_CONTIG:
  1920. case BMAP_RIGHT_CONTIG:
  1921. /*
  1922. * These cases are all impossible.
  1923. */
  1924. ASSERT(0);
  1925. }
  1926. /* add reverse mapping */
  1927. error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
  1928. if (error)
  1929. goto done;
  1930. /* convert to a btree if necessary */
  1931. if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
  1932. int tmp_logflags; /* partial log flag return val */
  1933. ASSERT(bma->cur == NULL);
  1934. error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
  1935. bma->firstblock, bma->dfops, &bma->cur,
  1936. da_old > 0, &tmp_logflags, whichfork);
  1937. bma->logflags |= tmp_logflags;
  1938. if (error)
  1939. goto done;
  1940. }
  1941. /* adjust for changes in reserved delayed indirect blocks */
  1942. if (da_old || da_new) {
  1943. temp = da_new;
  1944. if (bma->cur)
  1945. temp += bma->cur->bc_private.b.allocated;
  1946. if (temp < da_old)
  1947. xfs_mod_fdblocks(bma->ip->i_mount,
  1948. (int64_t)(da_old - temp), false);
  1949. }
  1950. /* clear out the allocated field, done with it now in any case. */
  1951. if (bma->cur)
  1952. bma->cur->bc_private.b.allocated = 0;
  1953. xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
  1954. done:
  1955. if (whichfork != XFS_COW_FORK)
  1956. bma->logflags |= rval;
  1957. return error;
  1958. #undef LEFT
  1959. #undef RIGHT
  1960. #undef PREV
  1961. }
  1962. /*
  1963. * Convert an unwritten allocation to a real allocation or vice versa.
  1964. */
  1965. STATIC int /* error */
  1966. xfs_bmap_add_extent_unwritten_real(
  1967. struct xfs_trans *tp,
  1968. xfs_inode_t *ip, /* incore inode pointer */
  1969. int whichfork,
  1970. xfs_extnum_t *idx, /* extent number to update/insert */
  1971. xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
  1972. xfs_bmbt_irec_t *new, /* new data to add to file extents */
  1973. xfs_fsblock_t *first, /* pointer to firstblock variable */
  1974. struct xfs_defer_ops *dfops, /* list of extents to be freed */
  1975. int *logflagsp) /* inode logging flags */
  1976. {
  1977. xfs_btree_cur_t *cur; /* btree cursor */
  1978. xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
  1979. int error; /* error return value */
  1980. int i; /* temp state */
  1981. xfs_ifork_t *ifp; /* inode fork pointer */
  1982. xfs_fileoff_t new_endoff; /* end offset of new entry */
  1983. xfs_exntst_t newext; /* new extent state */
  1984. xfs_exntst_t oldext; /* old extent state */
  1985. xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
  1986. /* left is 0, right is 1, prev is 2 */
  1987. int rval=0; /* return value (logging flags) */
  1988. int state = 0;/* state bits, accessed thru macros */
  1989. struct xfs_mount *mp = ip->i_mount;
  1990. *logflagsp = 0;
  1991. cur = *curp;
  1992. ifp = XFS_IFORK_PTR(ip, whichfork);
  1993. if (whichfork == XFS_COW_FORK)
  1994. state |= BMAP_COWFORK;
  1995. ASSERT(*idx >= 0);
  1996. ASSERT(*idx <= xfs_iext_count(ifp));
  1997. ASSERT(!isnullstartblock(new->br_startblock));
  1998. XFS_STATS_INC(mp, xs_add_exlist);
  1999. #define LEFT r[0]
  2000. #define RIGHT r[1]
  2001. #define PREV r[2]
  2002. /*
  2003. * Set up a bunch of variables to make the tests simpler.
  2004. */
  2005. error = 0;
  2006. ep = xfs_iext_get_ext(ifp, *idx);
  2007. xfs_bmbt_get_all(ep, &PREV);
  2008. newext = new->br_state;
  2009. oldext = (newext == XFS_EXT_UNWRITTEN) ?
  2010. XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
  2011. ASSERT(PREV.br_state == oldext);
  2012. new_endoff = new->br_startoff + new->br_blockcount;
  2013. ASSERT(PREV.br_startoff <= new->br_startoff);
  2014. ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
  2015. /*
  2016. * Set flags determining what part of the previous oldext allocation
  2017. * extent is being replaced by a newext allocation.
  2018. */
  2019. if (PREV.br_startoff == new->br_startoff)
  2020. state |= BMAP_LEFT_FILLING;
  2021. if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
  2022. state |= BMAP_RIGHT_FILLING;
  2023. /*
  2024. * Check and set flags if this segment has a left neighbor.
  2025. * Don't set contiguous if the combined extent would be too large.
  2026. */
  2027. if (*idx > 0) {
  2028. state |= BMAP_LEFT_VALID;
  2029. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
  2030. if (isnullstartblock(LEFT.br_startblock))
  2031. state |= BMAP_LEFT_DELAY;
  2032. }
  2033. if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
  2034. LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
  2035. LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
  2036. LEFT.br_state == newext &&
  2037. LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
  2038. state |= BMAP_LEFT_CONTIG;
  2039. /*
  2040. * Check and set flags if this segment has a right neighbor.
  2041. * Don't set contiguous if the combined extent would be too large.
  2042. * Also check for all-three-contiguous being too large.
  2043. */
  2044. if (*idx < xfs_iext_count(ifp) - 1) {
  2045. state |= BMAP_RIGHT_VALID;
  2046. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
  2047. if (isnullstartblock(RIGHT.br_startblock))
  2048. state |= BMAP_RIGHT_DELAY;
  2049. }
  2050. if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
  2051. new_endoff == RIGHT.br_startoff &&
  2052. new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
  2053. newext == RIGHT.br_state &&
  2054. new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
  2055. ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
  2056. BMAP_RIGHT_FILLING)) !=
  2057. (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
  2058. BMAP_RIGHT_FILLING) ||
  2059. LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
  2060. <= MAXEXTLEN))
  2061. state |= BMAP_RIGHT_CONTIG;
  2062. /*
  2063. * Switch out based on the FILLING and CONTIG state bits.
  2064. */
  2065. switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
  2066. BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
  2067. case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
  2068. BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
  2069. /*
  2070. * Setting all of a previous oldext extent to newext.
  2071. * The left and right neighbors are both contiguous with new.
  2072. */
  2073. --*idx;
  2074. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  2075. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
  2076. LEFT.br_blockcount + PREV.br_blockcount +
  2077. RIGHT.br_blockcount);
  2078. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  2079. xfs_iext_remove(ip, *idx + 1, 2, state);
  2080. XFS_IFORK_NEXT_SET(ip, whichfork,
  2081. XFS_IFORK_NEXTENTS(ip, whichfork) - 2);
  2082. if (cur == NULL)
  2083. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  2084. else {
  2085. rval = XFS_ILOG_CORE;
  2086. if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
  2087. RIGHT.br_startblock,
  2088. RIGHT.br_blockcount, &i)))
  2089. goto done;
  2090. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2091. if ((error = xfs_btree_delete(cur, &i)))
  2092. goto done;
  2093. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2094. if ((error = xfs_btree_decrement(cur, 0, &i)))
  2095. goto done;
  2096. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2097. if ((error = xfs_btree_delete(cur, &i)))
  2098. goto done;
  2099. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2100. if ((error = xfs_btree_decrement(cur, 0, &i)))
  2101. goto done;
  2102. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2103. if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
  2104. LEFT.br_startblock,
  2105. LEFT.br_blockcount + PREV.br_blockcount +
  2106. RIGHT.br_blockcount, LEFT.br_state)))
  2107. goto done;
  2108. }
  2109. break;
  2110. case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
  2111. /*
  2112. * Setting all of a previous oldext extent to newext.
  2113. * The left neighbor is contiguous, the right is not.
  2114. */
  2115. --*idx;
  2116. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  2117. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
  2118. LEFT.br_blockcount + PREV.br_blockcount);
  2119. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  2120. xfs_iext_remove(ip, *idx + 1, 1, state);
  2121. XFS_IFORK_NEXT_SET(ip, whichfork,
  2122. XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
  2123. if (cur == NULL)
  2124. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  2125. else {
  2126. rval = XFS_ILOG_CORE;
  2127. if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
  2128. PREV.br_startblock, PREV.br_blockcount,
  2129. &i)))
  2130. goto done;
  2131. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2132. if ((error = xfs_btree_delete(cur, &i)))
  2133. goto done;
  2134. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2135. if ((error = xfs_btree_decrement(cur, 0, &i)))
  2136. goto done;
  2137. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2138. if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
  2139. LEFT.br_startblock,
  2140. LEFT.br_blockcount + PREV.br_blockcount,
  2141. LEFT.br_state)))
  2142. goto done;
  2143. }
  2144. break;
  2145. case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
  2146. /*
  2147. * Setting all of a previous oldext extent to newext.
  2148. * The right neighbor is contiguous, the left is not.
  2149. */
  2150. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  2151. xfs_bmbt_set_blockcount(ep,
  2152. PREV.br_blockcount + RIGHT.br_blockcount);
  2153. xfs_bmbt_set_state(ep, newext);
  2154. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  2155. xfs_iext_remove(ip, *idx + 1, 1, state);
  2156. XFS_IFORK_NEXT_SET(ip, whichfork,
  2157. XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
  2158. if (cur == NULL)
  2159. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  2160. else {
  2161. rval = XFS_ILOG_CORE;
  2162. if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
  2163. RIGHT.br_startblock,
  2164. RIGHT.br_blockcount, &i)))
  2165. goto done;
  2166. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2167. if ((error = xfs_btree_delete(cur, &i)))
  2168. goto done;
  2169. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2170. if ((error = xfs_btree_decrement(cur, 0, &i)))
  2171. goto done;
  2172. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2173. if ((error = xfs_bmbt_update(cur, new->br_startoff,
  2174. new->br_startblock,
  2175. new->br_blockcount + RIGHT.br_blockcount,
  2176. newext)))
  2177. goto done;
  2178. }
  2179. break;
  2180. case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
  2181. /*
  2182. * Setting all of a previous oldext extent to newext.
  2183. * Neither the left nor right neighbors are contiguous with
  2184. * the new one.
  2185. */
  2186. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  2187. xfs_bmbt_set_state(ep, newext);
  2188. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  2189. if (cur == NULL)
  2190. rval = XFS_ILOG_DEXT;
  2191. else {
  2192. rval = 0;
  2193. if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
  2194. new->br_startblock, new->br_blockcount,
  2195. &i)))
  2196. goto done;
  2197. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2198. if ((error = xfs_bmbt_update(cur, new->br_startoff,
  2199. new->br_startblock, new->br_blockcount,
  2200. newext)))
  2201. goto done;
  2202. }
  2203. break;
  2204. case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
  2205. /*
  2206. * Setting the first part of a previous oldext extent to newext.
  2207. * The left neighbor is contiguous.
  2208. */
  2209. trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
  2210. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
  2211. LEFT.br_blockcount + new->br_blockcount);
  2212. xfs_bmbt_set_startoff(ep,
  2213. PREV.br_startoff + new->br_blockcount);
  2214. trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
  2215. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  2216. xfs_bmbt_set_startblock(ep,
  2217. new->br_startblock + new->br_blockcount);
  2218. xfs_bmbt_set_blockcount(ep,
  2219. PREV.br_blockcount - new->br_blockcount);
  2220. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  2221. --*idx;
  2222. if (cur == NULL)
  2223. rval = XFS_ILOG_DEXT;
  2224. else {
  2225. rval = 0;
  2226. if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
  2227. PREV.br_startblock, PREV.br_blockcount,
  2228. &i)))
  2229. goto done;
  2230. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2231. if ((error = xfs_bmbt_update(cur,
  2232. PREV.br_startoff + new->br_blockcount,
  2233. PREV.br_startblock + new->br_blockcount,
  2234. PREV.br_blockcount - new->br_blockcount,
  2235. oldext)))
  2236. goto done;
  2237. if ((error = xfs_btree_decrement(cur, 0, &i)))
  2238. goto done;
  2239. error = xfs_bmbt_update(cur, LEFT.br_startoff,
  2240. LEFT.br_startblock,
  2241. LEFT.br_blockcount + new->br_blockcount,
  2242. LEFT.br_state);
  2243. if (error)
  2244. goto done;
  2245. }
  2246. break;
  2247. case BMAP_LEFT_FILLING:
  2248. /*
  2249. * Setting the first part of a previous oldext extent to newext.
  2250. * The left neighbor is not contiguous.
  2251. */
  2252. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  2253. ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
  2254. xfs_bmbt_set_startoff(ep, new_endoff);
  2255. xfs_bmbt_set_blockcount(ep,
  2256. PREV.br_blockcount - new->br_blockcount);
  2257. xfs_bmbt_set_startblock(ep,
  2258. new->br_startblock + new->br_blockcount);
  2259. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  2260. xfs_iext_insert(ip, *idx, 1, new, state);
  2261. XFS_IFORK_NEXT_SET(ip, whichfork,
  2262. XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
  2263. if (cur == NULL)
  2264. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  2265. else {
  2266. rval = XFS_ILOG_CORE;
  2267. if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
  2268. PREV.br_startblock, PREV.br_blockcount,
  2269. &i)))
  2270. goto done;
  2271. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2272. if ((error = xfs_bmbt_update(cur,
  2273. PREV.br_startoff + new->br_blockcount,
  2274. PREV.br_startblock + new->br_blockcount,
  2275. PREV.br_blockcount - new->br_blockcount,
  2276. oldext)))
  2277. goto done;
  2278. cur->bc_rec.b = *new;
  2279. if ((error = xfs_btree_insert(cur, &i)))
  2280. goto done;
  2281. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2282. }
  2283. break;
  2284. case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
  2285. /*
  2286. * Setting the last part of a previous oldext extent to newext.
  2287. * The right neighbor is contiguous with the new allocation.
  2288. */
  2289. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  2290. xfs_bmbt_set_blockcount(ep,
  2291. PREV.br_blockcount - new->br_blockcount);
  2292. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  2293. ++*idx;
  2294. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  2295. xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
  2296. new->br_startoff, new->br_startblock,
  2297. new->br_blockcount + RIGHT.br_blockcount, newext);
  2298. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  2299. if (cur == NULL)
  2300. rval = XFS_ILOG_DEXT;
  2301. else {
  2302. rval = 0;
  2303. if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
  2304. PREV.br_startblock,
  2305. PREV.br_blockcount, &i)))
  2306. goto done;
  2307. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2308. if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
  2309. PREV.br_startblock,
  2310. PREV.br_blockcount - new->br_blockcount,
  2311. oldext)))
  2312. goto done;
  2313. if ((error = xfs_btree_increment(cur, 0, &i)))
  2314. goto done;
  2315. if ((error = xfs_bmbt_update(cur, new->br_startoff,
  2316. new->br_startblock,
  2317. new->br_blockcount + RIGHT.br_blockcount,
  2318. newext)))
  2319. goto done;
  2320. }
  2321. break;
  2322. case BMAP_RIGHT_FILLING:
  2323. /*
  2324. * Setting the last part of a previous oldext extent to newext.
  2325. * The right neighbor is not contiguous.
  2326. */
  2327. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  2328. xfs_bmbt_set_blockcount(ep,
  2329. PREV.br_blockcount - new->br_blockcount);
  2330. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  2331. ++*idx;
  2332. xfs_iext_insert(ip, *idx, 1, new, state);
  2333. XFS_IFORK_NEXT_SET(ip, whichfork,
  2334. XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
  2335. if (cur == NULL)
  2336. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  2337. else {
  2338. rval = XFS_ILOG_CORE;
  2339. if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
  2340. PREV.br_startblock, PREV.br_blockcount,
  2341. &i)))
  2342. goto done;
  2343. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2344. if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
  2345. PREV.br_startblock,
  2346. PREV.br_blockcount - new->br_blockcount,
  2347. oldext)))
  2348. goto done;
  2349. if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
  2350. new->br_startblock, new->br_blockcount,
  2351. &i)))
  2352. goto done;
  2353. XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
  2354. cur->bc_rec.b.br_state = XFS_EXT_NORM;
  2355. if ((error = xfs_btree_insert(cur, &i)))
  2356. goto done;
  2357. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2358. }
  2359. break;
  2360. case 0:
  2361. /*
  2362. * Setting the middle part of a previous oldext extent to
  2363. * newext. Contiguity is impossible here.
  2364. * One extent becomes three extents.
  2365. */
  2366. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  2367. xfs_bmbt_set_blockcount(ep,
  2368. new->br_startoff - PREV.br_startoff);
  2369. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  2370. r[0] = *new;
  2371. r[1].br_startoff = new_endoff;
  2372. r[1].br_blockcount =
  2373. PREV.br_startoff + PREV.br_blockcount - new_endoff;
  2374. r[1].br_startblock = new->br_startblock + new->br_blockcount;
  2375. r[1].br_state = oldext;
  2376. ++*idx;
  2377. xfs_iext_insert(ip, *idx, 2, &r[0], state);
  2378. XFS_IFORK_NEXT_SET(ip, whichfork,
  2379. XFS_IFORK_NEXTENTS(ip, whichfork) + 2);
  2380. if (cur == NULL)
  2381. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  2382. else {
  2383. rval = XFS_ILOG_CORE;
  2384. if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
  2385. PREV.br_startblock, PREV.br_blockcount,
  2386. &i)))
  2387. goto done;
  2388. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2389. /* new right extent - oldext */
  2390. if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
  2391. r[1].br_startblock, r[1].br_blockcount,
  2392. r[1].br_state)))
  2393. goto done;
  2394. /* new left extent - oldext */
  2395. cur->bc_rec.b = PREV;
  2396. cur->bc_rec.b.br_blockcount =
  2397. new->br_startoff - PREV.br_startoff;
  2398. if ((error = xfs_btree_insert(cur, &i)))
  2399. goto done;
  2400. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2401. /*
  2402. * Reset the cursor to the position of the new extent
  2403. * we are about to insert as we can't trust it after
  2404. * the previous insert.
  2405. */
  2406. if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
  2407. new->br_startblock, new->br_blockcount,
  2408. &i)))
  2409. goto done;
  2410. XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
  2411. /* new middle extent - newext */
  2412. cur->bc_rec.b.br_state = new->br_state;
  2413. if ((error = xfs_btree_insert(cur, &i)))
  2414. goto done;
  2415. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2416. }
  2417. break;
  2418. case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
  2419. case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
  2420. case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
  2421. case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
  2422. case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
  2423. case BMAP_LEFT_CONTIG:
  2424. case BMAP_RIGHT_CONTIG:
  2425. /*
  2426. * These cases are all impossible.
  2427. */
  2428. ASSERT(0);
  2429. }
  2430. /* update reverse mappings */
  2431. error = xfs_rmap_convert_extent(mp, dfops, ip, whichfork, new);
  2432. if (error)
  2433. goto done;
  2434. /* convert to a btree if necessary */
  2435. if (xfs_bmap_needs_btree(ip, whichfork)) {
  2436. int tmp_logflags; /* partial log flag return val */
  2437. ASSERT(cur == NULL);
  2438. error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur,
  2439. 0, &tmp_logflags, whichfork);
  2440. *logflagsp |= tmp_logflags;
  2441. if (error)
  2442. goto done;
  2443. }
  2444. /* clear out the allocated field, done with it now in any case. */
  2445. if (cur) {
  2446. cur->bc_private.b.allocated = 0;
  2447. *curp = cur;
  2448. }
  2449. xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
  2450. done:
  2451. *logflagsp |= rval;
  2452. return error;
  2453. #undef LEFT
  2454. #undef RIGHT
  2455. #undef PREV
  2456. }
  2457. /*
  2458. * Convert a hole to a delayed allocation.
  2459. */
  2460. STATIC void
  2461. xfs_bmap_add_extent_hole_delay(
  2462. xfs_inode_t *ip, /* incore inode pointer */
  2463. int whichfork,
  2464. xfs_extnum_t *idx, /* extent number to update/insert */
  2465. xfs_bmbt_irec_t *new) /* new data to add to file extents */
  2466. {
  2467. xfs_ifork_t *ifp; /* inode fork pointer */
  2468. xfs_bmbt_irec_t left; /* left neighbor extent entry */
  2469. xfs_filblks_t newlen=0; /* new indirect size */
  2470. xfs_filblks_t oldlen=0; /* old indirect size */
  2471. xfs_bmbt_irec_t right; /* right neighbor extent entry */
  2472. int state; /* state bits, accessed thru macros */
  2473. xfs_filblks_t temp=0; /* temp for indirect calculations */
  2474. ifp = XFS_IFORK_PTR(ip, whichfork);
  2475. state = 0;
  2476. if (whichfork == XFS_COW_FORK)
  2477. state |= BMAP_COWFORK;
  2478. ASSERT(isnullstartblock(new->br_startblock));
  2479. /*
  2480. * Check and set flags if this segment has a left neighbor
  2481. */
  2482. if (*idx > 0) {
  2483. state |= BMAP_LEFT_VALID;
  2484. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
  2485. if (isnullstartblock(left.br_startblock))
  2486. state |= BMAP_LEFT_DELAY;
  2487. }
  2488. /*
  2489. * Check and set flags if the current (right) segment exists.
  2490. * If it doesn't exist, we're converting the hole at end-of-file.
  2491. */
  2492. if (*idx < xfs_iext_count(ifp)) {
  2493. state |= BMAP_RIGHT_VALID;
  2494. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
  2495. if (isnullstartblock(right.br_startblock))
  2496. state |= BMAP_RIGHT_DELAY;
  2497. }
  2498. /*
  2499. * Set contiguity flags on the left and right neighbors.
  2500. * Don't let extents get too large, even if the pieces are contiguous.
  2501. */
  2502. if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
  2503. left.br_startoff + left.br_blockcount == new->br_startoff &&
  2504. left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
  2505. state |= BMAP_LEFT_CONTIG;
  2506. if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
  2507. new->br_startoff + new->br_blockcount == right.br_startoff &&
  2508. new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
  2509. (!(state & BMAP_LEFT_CONTIG) ||
  2510. (left.br_blockcount + new->br_blockcount +
  2511. right.br_blockcount <= MAXEXTLEN)))
  2512. state |= BMAP_RIGHT_CONTIG;
  2513. /*
  2514. * Switch out based on the contiguity flags.
  2515. */
  2516. switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
  2517. case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
  2518. /*
  2519. * New allocation is contiguous with delayed allocations
  2520. * on the left and on the right.
  2521. * Merge all three into a single extent record.
  2522. */
  2523. --*idx;
  2524. temp = left.br_blockcount + new->br_blockcount +
  2525. right.br_blockcount;
  2526. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  2527. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
  2528. oldlen = startblockval(left.br_startblock) +
  2529. startblockval(new->br_startblock) +
  2530. startblockval(right.br_startblock);
  2531. newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
  2532. oldlen);
  2533. xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
  2534. nullstartblock((int)newlen));
  2535. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  2536. xfs_iext_remove(ip, *idx + 1, 1, state);
  2537. break;
  2538. case BMAP_LEFT_CONTIG:
  2539. /*
  2540. * New allocation is contiguous with a delayed allocation
  2541. * on the left.
  2542. * Merge the new allocation with the left neighbor.
  2543. */
  2544. --*idx;
  2545. temp = left.br_blockcount + new->br_blockcount;
  2546. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  2547. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
  2548. oldlen = startblockval(left.br_startblock) +
  2549. startblockval(new->br_startblock);
  2550. newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
  2551. oldlen);
  2552. xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
  2553. nullstartblock((int)newlen));
  2554. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  2555. break;
  2556. case BMAP_RIGHT_CONTIG:
  2557. /*
  2558. * New allocation is contiguous with a delayed allocation
  2559. * on the right.
  2560. * Merge the new allocation with the right neighbor.
  2561. */
  2562. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  2563. temp = new->br_blockcount + right.br_blockcount;
  2564. oldlen = startblockval(new->br_startblock) +
  2565. startblockval(right.br_startblock);
  2566. newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
  2567. oldlen);
  2568. xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
  2569. new->br_startoff,
  2570. nullstartblock((int)newlen), temp, right.br_state);
  2571. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  2572. break;
  2573. case 0:
  2574. /*
  2575. * New allocation is not contiguous with another
  2576. * delayed allocation.
  2577. * Insert a new entry.
  2578. */
  2579. oldlen = newlen = 0;
  2580. xfs_iext_insert(ip, *idx, 1, new, state);
  2581. break;
  2582. }
  2583. if (oldlen != newlen) {
  2584. ASSERT(oldlen > newlen);
  2585. xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
  2586. false);
  2587. /*
  2588. * Nothing to do for disk quota accounting here.
  2589. */
  2590. }
  2591. }
  2592. /*
  2593. * Convert a hole to a real allocation.
  2594. */
  2595. STATIC int /* error */
  2596. xfs_bmap_add_extent_hole_real(
  2597. struct xfs_trans *tp,
  2598. struct xfs_inode *ip,
  2599. int whichfork,
  2600. xfs_extnum_t *idx,
  2601. struct xfs_btree_cur **curp,
  2602. struct xfs_bmbt_irec *new,
  2603. xfs_fsblock_t *first,
  2604. struct xfs_defer_ops *dfops,
  2605. int *logflagsp)
  2606. {
  2607. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
  2608. struct xfs_mount *mp = ip->i_mount;
  2609. struct xfs_btree_cur *cur = *curp;
  2610. int error; /* error return value */
  2611. int i; /* temp state */
  2612. xfs_bmbt_irec_t left; /* left neighbor extent entry */
  2613. xfs_bmbt_irec_t right; /* right neighbor extent entry */
  2614. int rval=0; /* return value (logging flags) */
  2615. int state; /* state bits, accessed thru macros */
  2616. ASSERT(*idx >= 0);
  2617. ASSERT(*idx <= xfs_iext_count(ifp));
  2618. ASSERT(!isnullstartblock(new->br_startblock));
  2619. ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
  2620. XFS_STATS_INC(mp, xs_add_exlist);
  2621. state = 0;
  2622. if (whichfork == XFS_ATTR_FORK)
  2623. state |= BMAP_ATTRFORK;
  2624. if (whichfork == XFS_COW_FORK)
  2625. state |= BMAP_COWFORK;
  2626. /*
  2627. * Check and set flags if this segment has a left neighbor.
  2628. */
  2629. if (*idx > 0) {
  2630. state |= BMAP_LEFT_VALID;
  2631. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
  2632. if (isnullstartblock(left.br_startblock))
  2633. state |= BMAP_LEFT_DELAY;
  2634. }
  2635. /*
  2636. * Check and set flags if this segment has a current value.
  2637. * Not true if we're inserting into the "hole" at eof.
  2638. */
  2639. if (*idx < xfs_iext_count(ifp)) {
  2640. state |= BMAP_RIGHT_VALID;
  2641. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
  2642. if (isnullstartblock(right.br_startblock))
  2643. state |= BMAP_RIGHT_DELAY;
  2644. }
  2645. /*
  2646. * We're inserting a real allocation between "left" and "right".
  2647. * Set the contiguity flags. Don't let extents get too large.
  2648. */
  2649. if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
  2650. left.br_startoff + left.br_blockcount == new->br_startoff &&
  2651. left.br_startblock + left.br_blockcount == new->br_startblock &&
  2652. left.br_state == new->br_state &&
  2653. left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
  2654. state |= BMAP_LEFT_CONTIG;
  2655. if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
  2656. new->br_startoff + new->br_blockcount == right.br_startoff &&
  2657. new->br_startblock + new->br_blockcount == right.br_startblock &&
  2658. new->br_state == right.br_state &&
  2659. new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
  2660. (!(state & BMAP_LEFT_CONTIG) ||
  2661. left.br_blockcount + new->br_blockcount +
  2662. right.br_blockcount <= MAXEXTLEN))
  2663. state |= BMAP_RIGHT_CONTIG;
  2664. error = 0;
  2665. /*
  2666. * Select which case we're in here, and implement it.
  2667. */
  2668. switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
  2669. case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
  2670. /*
  2671. * New allocation is contiguous with real allocations on the
  2672. * left and on the right.
  2673. * Merge all three into a single extent record.
  2674. */
  2675. --*idx;
  2676. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  2677. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
  2678. left.br_blockcount + new->br_blockcount +
  2679. right.br_blockcount);
  2680. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  2681. xfs_iext_remove(ip, *idx + 1, 1, state);
  2682. XFS_IFORK_NEXT_SET(ip, whichfork,
  2683. XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
  2684. if (cur == NULL) {
  2685. rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
  2686. } else {
  2687. rval = XFS_ILOG_CORE;
  2688. error = xfs_bmbt_lookup_eq(cur, right.br_startoff,
  2689. right.br_startblock, right.br_blockcount,
  2690. &i);
  2691. if (error)
  2692. goto done;
  2693. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2694. error = xfs_btree_delete(cur, &i);
  2695. if (error)
  2696. goto done;
  2697. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2698. error = xfs_btree_decrement(cur, 0, &i);
  2699. if (error)
  2700. goto done;
  2701. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2702. error = xfs_bmbt_update(cur, left.br_startoff,
  2703. left.br_startblock,
  2704. left.br_blockcount +
  2705. new->br_blockcount +
  2706. right.br_blockcount,
  2707. left.br_state);
  2708. if (error)
  2709. goto done;
  2710. }
  2711. break;
  2712. case BMAP_LEFT_CONTIG:
  2713. /*
  2714. * New allocation is contiguous with a real allocation
  2715. * on the left.
  2716. * Merge the new allocation with the left neighbor.
  2717. */
  2718. --*idx;
  2719. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  2720. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
  2721. left.br_blockcount + new->br_blockcount);
  2722. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  2723. if (cur == NULL) {
  2724. rval = xfs_ilog_fext(whichfork);
  2725. } else {
  2726. rval = 0;
  2727. error = xfs_bmbt_lookup_eq(cur, left.br_startoff,
  2728. left.br_startblock, left.br_blockcount,
  2729. &i);
  2730. if (error)
  2731. goto done;
  2732. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2733. error = xfs_bmbt_update(cur, left.br_startoff,
  2734. left.br_startblock,
  2735. left.br_blockcount +
  2736. new->br_blockcount,
  2737. left.br_state);
  2738. if (error)
  2739. goto done;
  2740. }
  2741. break;
  2742. case BMAP_RIGHT_CONTIG:
  2743. /*
  2744. * New allocation is contiguous with a real allocation
  2745. * on the right.
  2746. * Merge the new allocation with the right neighbor.
  2747. */
  2748. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  2749. xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
  2750. new->br_startoff, new->br_startblock,
  2751. new->br_blockcount + right.br_blockcount,
  2752. right.br_state);
  2753. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  2754. if (cur == NULL) {
  2755. rval = xfs_ilog_fext(whichfork);
  2756. } else {
  2757. rval = 0;
  2758. error = xfs_bmbt_lookup_eq(cur,
  2759. right.br_startoff,
  2760. right.br_startblock,
  2761. right.br_blockcount, &i);
  2762. if (error)
  2763. goto done;
  2764. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2765. error = xfs_bmbt_update(cur, new->br_startoff,
  2766. new->br_startblock,
  2767. new->br_blockcount +
  2768. right.br_blockcount,
  2769. right.br_state);
  2770. if (error)
  2771. goto done;
  2772. }
  2773. break;
  2774. case 0:
  2775. /*
  2776. * New allocation is not contiguous with another
  2777. * real allocation.
  2778. * Insert a new entry.
  2779. */
  2780. xfs_iext_insert(ip, *idx, 1, new, state);
  2781. XFS_IFORK_NEXT_SET(ip, whichfork,
  2782. XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
  2783. if (cur == NULL) {
  2784. rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
  2785. } else {
  2786. rval = XFS_ILOG_CORE;
  2787. error = xfs_bmbt_lookup_eq(cur,
  2788. new->br_startoff,
  2789. new->br_startblock,
  2790. new->br_blockcount, &i);
  2791. if (error)
  2792. goto done;
  2793. XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
  2794. cur->bc_rec.b.br_state = new->br_state;
  2795. error = xfs_btree_insert(cur, &i);
  2796. if (error)
  2797. goto done;
  2798. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2799. }
  2800. break;
  2801. }
  2802. /* add reverse mapping */
  2803. error = xfs_rmap_map_extent(mp, dfops, ip, whichfork, new);
  2804. if (error)
  2805. goto done;
  2806. /* convert to a btree if necessary */
  2807. if (xfs_bmap_needs_btree(ip, whichfork)) {
  2808. int tmp_logflags; /* partial log flag return val */
  2809. ASSERT(cur == NULL);
  2810. error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, curp,
  2811. 0, &tmp_logflags, whichfork);
  2812. *logflagsp |= tmp_logflags;
  2813. cur = *curp;
  2814. if (error)
  2815. goto done;
  2816. }
  2817. /* clear out the allocated field, done with it now in any case. */
  2818. if (cur)
  2819. cur->bc_private.b.allocated = 0;
  2820. xfs_bmap_check_leaf_extents(cur, ip, whichfork);
  2821. done:
  2822. *logflagsp |= rval;
  2823. return error;
  2824. }
  2825. /*
  2826. * Functions used in the extent read, allocate and remove paths
  2827. */
  2828. /*
  2829. * Adjust the size of the new extent based on di_extsize and rt extsize.
  2830. */
  2831. int
  2832. xfs_bmap_extsize_align(
  2833. xfs_mount_t *mp,
  2834. xfs_bmbt_irec_t *gotp, /* next extent pointer */
  2835. xfs_bmbt_irec_t *prevp, /* previous extent pointer */
  2836. xfs_extlen_t extsz, /* align to this extent size */
  2837. int rt, /* is this a realtime inode? */
  2838. int eof, /* is extent at end-of-file? */
  2839. int delay, /* creating delalloc extent? */
  2840. int convert, /* overwriting unwritten extent? */
  2841. xfs_fileoff_t *offp, /* in/out: aligned offset */
  2842. xfs_extlen_t *lenp) /* in/out: aligned length */
  2843. {
  2844. xfs_fileoff_t orig_off; /* original offset */
  2845. xfs_extlen_t orig_alen; /* original length */
  2846. xfs_fileoff_t orig_end; /* original off+len */
  2847. xfs_fileoff_t nexto; /* next file offset */
  2848. xfs_fileoff_t prevo; /* previous file offset */
  2849. xfs_fileoff_t align_off; /* temp for offset */
  2850. xfs_extlen_t align_alen; /* temp for length */
  2851. xfs_extlen_t temp; /* temp for calculations */
  2852. if (convert)
  2853. return 0;
  2854. orig_off = align_off = *offp;
  2855. orig_alen = align_alen = *lenp;
  2856. orig_end = orig_off + orig_alen;
  2857. /*
  2858. * If this request overlaps an existing extent, then don't
  2859. * attempt to perform any additional alignment.
  2860. */
  2861. if (!delay && !eof &&
  2862. (orig_off >= gotp->br_startoff) &&
  2863. (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
  2864. return 0;
  2865. }
  2866. /*
  2867. * If the file offset is unaligned vs. the extent size
  2868. * we need to align it. This will be possible unless
  2869. * the file was previously written with a kernel that didn't
  2870. * perform this alignment, or if a truncate shot us in the
  2871. * foot.
  2872. */
  2873. temp = do_mod(orig_off, extsz);
  2874. if (temp) {
  2875. align_alen += temp;
  2876. align_off -= temp;
  2877. }
  2878. /* Same adjustment for the end of the requested area. */
  2879. temp = (align_alen % extsz);
  2880. if (temp)
  2881. align_alen += extsz - temp;
  2882. /*
  2883. * For large extent hint sizes, the aligned extent might be larger than
  2884. * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
  2885. * the length back under MAXEXTLEN. The outer allocation loops handle
  2886. * short allocation just fine, so it is safe to do this. We only want to
  2887. * do it when we are forced to, though, because it means more allocation
  2888. * operations are required.
  2889. */
  2890. while (align_alen > MAXEXTLEN)
  2891. align_alen -= extsz;
  2892. ASSERT(align_alen <= MAXEXTLEN);
  2893. /*
  2894. * If the previous block overlaps with this proposed allocation
  2895. * then move the start forward without adjusting the length.
  2896. */
  2897. if (prevp->br_startoff != NULLFILEOFF) {
  2898. if (prevp->br_startblock == HOLESTARTBLOCK)
  2899. prevo = prevp->br_startoff;
  2900. else
  2901. prevo = prevp->br_startoff + prevp->br_blockcount;
  2902. } else
  2903. prevo = 0;
  2904. if (align_off != orig_off && align_off < prevo)
  2905. align_off = prevo;
  2906. /*
  2907. * If the next block overlaps with this proposed allocation
  2908. * then move the start back without adjusting the length,
  2909. * but not before offset 0.
  2910. * This may of course make the start overlap previous block,
  2911. * and if we hit the offset 0 limit then the next block
  2912. * can still overlap too.
  2913. */
  2914. if (!eof && gotp->br_startoff != NULLFILEOFF) {
  2915. if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
  2916. (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
  2917. nexto = gotp->br_startoff + gotp->br_blockcount;
  2918. else
  2919. nexto = gotp->br_startoff;
  2920. } else
  2921. nexto = NULLFILEOFF;
  2922. if (!eof &&
  2923. align_off + align_alen != orig_end &&
  2924. align_off + align_alen > nexto)
  2925. align_off = nexto > align_alen ? nexto - align_alen : 0;
  2926. /*
  2927. * If we're now overlapping the next or previous extent that
  2928. * means we can't fit an extsz piece in this hole. Just move
  2929. * the start forward to the first valid spot and set
  2930. * the length so we hit the end.
  2931. */
  2932. if (align_off != orig_off && align_off < prevo)
  2933. align_off = prevo;
  2934. if (align_off + align_alen != orig_end &&
  2935. align_off + align_alen > nexto &&
  2936. nexto != NULLFILEOFF) {
  2937. ASSERT(nexto > prevo);
  2938. align_alen = nexto - align_off;
  2939. }
  2940. /*
  2941. * If realtime, and the result isn't a multiple of the realtime
  2942. * extent size we need to remove blocks until it is.
  2943. */
  2944. if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
  2945. /*
  2946. * We're not covering the original request, or
  2947. * we won't be able to once we fix the length.
  2948. */
  2949. if (orig_off < align_off ||
  2950. orig_end > align_off + align_alen ||
  2951. align_alen - temp < orig_alen)
  2952. return -EINVAL;
  2953. /*
  2954. * Try to fix it by moving the start up.
  2955. */
  2956. if (align_off + temp <= orig_off) {
  2957. align_alen -= temp;
  2958. align_off += temp;
  2959. }
  2960. /*
  2961. * Try to fix it by moving the end in.
  2962. */
  2963. else if (align_off + align_alen - temp >= orig_end)
  2964. align_alen -= temp;
  2965. /*
  2966. * Set the start to the minimum then trim the length.
  2967. */
  2968. else {
  2969. align_alen -= orig_off - align_off;
  2970. align_off = orig_off;
  2971. align_alen -= align_alen % mp->m_sb.sb_rextsize;
  2972. }
  2973. /*
  2974. * Result doesn't cover the request, fail it.
  2975. */
  2976. if (orig_off < align_off || orig_end > align_off + align_alen)
  2977. return -EINVAL;
  2978. } else {
  2979. ASSERT(orig_off >= align_off);
  2980. /* see MAXEXTLEN handling above */
  2981. ASSERT(orig_end <= align_off + align_alen ||
  2982. align_alen + extsz > MAXEXTLEN);
  2983. }
  2984. #ifdef DEBUG
  2985. if (!eof && gotp->br_startoff != NULLFILEOFF)
  2986. ASSERT(align_off + align_alen <= gotp->br_startoff);
  2987. if (prevp->br_startoff != NULLFILEOFF)
  2988. ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
  2989. #endif
  2990. *lenp = align_alen;
  2991. *offp = align_off;
  2992. return 0;
  2993. }
  2994. #define XFS_ALLOC_GAP_UNITS 4
  2995. void
  2996. xfs_bmap_adjacent(
  2997. struct xfs_bmalloca *ap) /* bmap alloc argument struct */
  2998. {
  2999. xfs_fsblock_t adjust; /* adjustment to block numbers */
  3000. xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
  3001. xfs_mount_t *mp; /* mount point structure */
  3002. int nullfb; /* true if ap->firstblock isn't set */
  3003. int rt; /* true if inode is realtime */
  3004. #define ISVALID(x,y) \
  3005. (rt ? \
  3006. (x) < mp->m_sb.sb_rblocks : \
  3007. XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
  3008. XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
  3009. XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
  3010. mp = ap->ip->i_mount;
  3011. nullfb = *ap->firstblock == NULLFSBLOCK;
  3012. rt = XFS_IS_REALTIME_INODE(ap->ip) &&
  3013. xfs_alloc_is_userdata(ap->datatype);
  3014. fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
  3015. /*
  3016. * If allocating at eof, and there's a previous real block,
  3017. * try to use its last block as our starting point.
  3018. */
  3019. if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
  3020. !isnullstartblock(ap->prev.br_startblock) &&
  3021. ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
  3022. ap->prev.br_startblock)) {
  3023. ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
  3024. /*
  3025. * Adjust for the gap between prevp and us.
  3026. */
  3027. adjust = ap->offset -
  3028. (ap->prev.br_startoff + ap->prev.br_blockcount);
  3029. if (adjust &&
  3030. ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
  3031. ap->blkno += adjust;
  3032. }
  3033. /*
  3034. * If not at eof, then compare the two neighbor blocks.
  3035. * Figure out whether either one gives us a good starting point,
  3036. * and pick the better one.
  3037. */
  3038. else if (!ap->eof) {
  3039. xfs_fsblock_t gotbno; /* right side block number */
  3040. xfs_fsblock_t gotdiff=0; /* right side difference */
  3041. xfs_fsblock_t prevbno; /* left side block number */
  3042. xfs_fsblock_t prevdiff=0; /* left side difference */
  3043. /*
  3044. * If there's a previous (left) block, select a requested
  3045. * start block based on it.
  3046. */
  3047. if (ap->prev.br_startoff != NULLFILEOFF &&
  3048. !isnullstartblock(ap->prev.br_startblock) &&
  3049. (prevbno = ap->prev.br_startblock +
  3050. ap->prev.br_blockcount) &&
  3051. ISVALID(prevbno, ap->prev.br_startblock)) {
  3052. /*
  3053. * Calculate gap to end of previous block.
  3054. */
  3055. adjust = prevdiff = ap->offset -
  3056. (ap->prev.br_startoff +
  3057. ap->prev.br_blockcount);
  3058. /*
  3059. * Figure the startblock based on the previous block's
  3060. * end and the gap size.
  3061. * Heuristic!
  3062. * If the gap is large relative to the piece we're
  3063. * allocating, or using it gives us an invalid block
  3064. * number, then just use the end of the previous block.
  3065. */
  3066. if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
  3067. ISVALID(prevbno + prevdiff,
  3068. ap->prev.br_startblock))
  3069. prevbno += adjust;
  3070. else
  3071. prevdiff += adjust;
  3072. /*
  3073. * If the firstblock forbids it, can't use it,
  3074. * must use default.
  3075. */
  3076. if (!rt && !nullfb &&
  3077. XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
  3078. prevbno = NULLFSBLOCK;
  3079. }
  3080. /*
  3081. * No previous block or can't follow it, just default.
  3082. */
  3083. else
  3084. prevbno = NULLFSBLOCK;
  3085. /*
  3086. * If there's a following (right) block, select a requested
  3087. * start block based on it.
  3088. */
  3089. if (!isnullstartblock(ap->got.br_startblock)) {
  3090. /*
  3091. * Calculate gap to start of next block.
  3092. */
  3093. adjust = gotdiff = ap->got.br_startoff - ap->offset;
  3094. /*
  3095. * Figure the startblock based on the next block's
  3096. * start and the gap size.
  3097. */
  3098. gotbno = ap->got.br_startblock;
  3099. /*
  3100. * Heuristic!
  3101. * If the gap is large relative to the piece we're
  3102. * allocating, or using it gives us an invalid block
  3103. * number, then just use the start of the next block
  3104. * offset by our length.
  3105. */
  3106. if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
  3107. ISVALID(gotbno - gotdiff, gotbno))
  3108. gotbno -= adjust;
  3109. else if (ISVALID(gotbno - ap->length, gotbno)) {
  3110. gotbno -= ap->length;
  3111. gotdiff += adjust - ap->length;
  3112. } else
  3113. gotdiff += adjust;
  3114. /*
  3115. * If the firstblock forbids it, can't use it,
  3116. * must use default.
  3117. */
  3118. if (!rt && !nullfb &&
  3119. XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
  3120. gotbno = NULLFSBLOCK;
  3121. }
  3122. /*
  3123. * No next block, just default.
  3124. */
  3125. else
  3126. gotbno = NULLFSBLOCK;
  3127. /*
  3128. * If both valid, pick the better one, else the only good
  3129. * one, else ap->blkno is already set (to 0 or the inode block).
  3130. */
  3131. if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
  3132. ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
  3133. else if (prevbno != NULLFSBLOCK)
  3134. ap->blkno = prevbno;
  3135. else if (gotbno != NULLFSBLOCK)
  3136. ap->blkno = gotbno;
  3137. }
  3138. #undef ISVALID
  3139. }
  3140. static int
  3141. xfs_bmap_longest_free_extent(
  3142. struct xfs_trans *tp,
  3143. xfs_agnumber_t ag,
  3144. xfs_extlen_t *blen,
  3145. int *notinit)
  3146. {
  3147. struct xfs_mount *mp = tp->t_mountp;
  3148. struct xfs_perag *pag;
  3149. xfs_extlen_t longest;
  3150. int error = 0;
  3151. pag = xfs_perag_get(mp, ag);
  3152. if (!pag->pagf_init) {
  3153. error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
  3154. if (error)
  3155. goto out;
  3156. if (!pag->pagf_init) {
  3157. *notinit = 1;
  3158. goto out;
  3159. }
  3160. }
  3161. longest = xfs_alloc_longest_free_extent(mp, pag,
  3162. xfs_alloc_min_freelist(mp, pag),
  3163. xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
  3164. if (*blen < longest)
  3165. *blen = longest;
  3166. out:
  3167. xfs_perag_put(pag);
  3168. return error;
  3169. }
  3170. static void
  3171. xfs_bmap_select_minlen(
  3172. struct xfs_bmalloca *ap,
  3173. struct xfs_alloc_arg *args,
  3174. xfs_extlen_t *blen,
  3175. int notinit)
  3176. {
  3177. if (notinit || *blen < ap->minlen) {
  3178. /*
  3179. * Since we did a BUF_TRYLOCK above, it is possible that
  3180. * there is space for this request.
  3181. */
  3182. args->minlen = ap->minlen;
  3183. } else if (*blen < args->maxlen) {
  3184. /*
  3185. * If the best seen length is less than the request length,
  3186. * use the best as the minimum.
  3187. */
  3188. args->minlen = *blen;
  3189. } else {
  3190. /*
  3191. * Otherwise we've seen an extent as big as maxlen, use that
  3192. * as the minimum.
  3193. */
  3194. args->minlen = args->maxlen;
  3195. }
  3196. }
  3197. STATIC int
  3198. xfs_bmap_btalloc_nullfb(
  3199. struct xfs_bmalloca *ap,
  3200. struct xfs_alloc_arg *args,
  3201. xfs_extlen_t *blen)
  3202. {
  3203. struct xfs_mount *mp = ap->ip->i_mount;
  3204. xfs_agnumber_t ag, startag;
  3205. int notinit = 0;
  3206. int error;
  3207. args->type = XFS_ALLOCTYPE_START_BNO;
  3208. args->total = ap->total;
  3209. startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
  3210. if (startag == NULLAGNUMBER)
  3211. startag = ag = 0;
  3212. while (*blen < args->maxlen) {
  3213. error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
  3214. &notinit);
  3215. if (error)
  3216. return error;
  3217. if (++ag == mp->m_sb.sb_agcount)
  3218. ag = 0;
  3219. if (ag == startag)
  3220. break;
  3221. }
  3222. xfs_bmap_select_minlen(ap, args, blen, notinit);
  3223. return 0;
  3224. }
  3225. STATIC int
  3226. xfs_bmap_btalloc_filestreams(
  3227. struct xfs_bmalloca *ap,
  3228. struct xfs_alloc_arg *args,
  3229. xfs_extlen_t *blen)
  3230. {
  3231. struct xfs_mount *mp = ap->ip->i_mount;
  3232. xfs_agnumber_t ag;
  3233. int notinit = 0;
  3234. int error;
  3235. args->type = XFS_ALLOCTYPE_NEAR_BNO;
  3236. args->total = ap->total;
  3237. ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
  3238. if (ag == NULLAGNUMBER)
  3239. ag = 0;
  3240. error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
  3241. if (error)
  3242. return error;
  3243. if (*blen < args->maxlen) {
  3244. error = xfs_filestream_new_ag(ap, &ag);
  3245. if (error)
  3246. return error;
  3247. error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
  3248. &notinit);
  3249. if (error)
  3250. return error;
  3251. }
  3252. xfs_bmap_select_minlen(ap, args, blen, notinit);
  3253. /*
  3254. * Set the failure fallback case to look in the selected AG as stream
  3255. * may have moved.
  3256. */
  3257. ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
  3258. return 0;
  3259. }
  3260. STATIC int
  3261. xfs_bmap_btalloc(
  3262. struct xfs_bmalloca *ap) /* bmap alloc argument struct */
  3263. {
  3264. xfs_mount_t *mp; /* mount point structure */
  3265. xfs_alloctype_t atype = 0; /* type for allocation routines */
  3266. xfs_extlen_t align = 0; /* minimum allocation alignment */
  3267. xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
  3268. xfs_agnumber_t ag;
  3269. xfs_alloc_arg_t args;
  3270. xfs_extlen_t blen;
  3271. xfs_extlen_t nextminlen = 0;
  3272. int nullfb; /* true if ap->firstblock isn't set */
  3273. int isaligned;
  3274. int tryagain;
  3275. int error;
  3276. int stripe_align;
  3277. ASSERT(ap->length);
  3278. mp = ap->ip->i_mount;
  3279. /* stripe alignment for allocation is determined by mount parameters */
  3280. stripe_align = 0;
  3281. if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
  3282. stripe_align = mp->m_swidth;
  3283. else if (mp->m_dalign)
  3284. stripe_align = mp->m_dalign;
  3285. if (ap->flags & XFS_BMAPI_COWFORK)
  3286. align = xfs_get_cowextsz_hint(ap->ip);
  3287. else if (xfs_alloc_is_userdata(ap->datatype))
  3288. align = xfs_get_extsz_hint(ap->ip);
  3289. if (align) {
  3290. error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
  3291. align, 0, ap->eof, 0, ap->conv,
  3292. &ap->offset, &ap->length);
  3293. ASSERT(!error);
  3294. ASSERT(ap->length);
  3295. }
  3296. nullfb = *ap->firstblock == NULLFSBLOCK;
  3297. fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
  3298. if (nullfb) {
  3299. if (xfs_alloc_is_userdata(ap->datatype) &&
  3300. xfs_inode_is_filestream(ap->ip)) {
  3301. ag = xfs_filestream_lookup_ag(ap->ip);
  3302. ag = (ag != NULLAGNUMBER) ? ag : 0;
  3303. ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
  3304. } else {
  3305. ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
  3306. }
  3307. } else
  3308. ap->blkno = *ap->firstblock;
  3309. xfs_bmap_adjacent(ap);
  3310. /*
  3311. * If allowed, use ap->blkno; otherwise must use firstblock since
  3312. * it's in the right allocation group.
  3313. */
  3314. if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
  3315. ;
  3316. else
  3317. ap->blkno = *ap->firstblock;
  3318. /*
  3319. * Normal allocation, done through xfs_alloc_vextent.
  3320. */
  3321. tryagain = isaligned = 0;
  3322. memset(&args, 0, sizeof(args));
  3323. args.tp = ap->tp;
  3324. args.mp = mp;
  3325. args.fsbno = ap->blkno;
  3326. xfs_rmap_skip_owner_update(&args.oinfo);
  3327. /* Trim the allocation back to the maximum an AG can fit. */
  3328. args.maxlen = MIN(ap->length, mp->m_ag_max_usable);
  3329. args.firstblock = *ap->firstblock;
  3330. blen = 0;
  3331. if (nullfb) {
  3332. /*
  3333. * Search for an allocation group with a single extent large
  3334. * enough for the request. If one isn't found, then adjust
  3335. * the minimum allocation size to the largest space found.
  3336. */
  3337. if (xfs_alloc_is_userdata(ap->datatype) &&
  3338. xfs_inode_is_filestream(ap->ip))
  3339. error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
  3340. else
  3341. error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
  3342. if (error)
  3343. return error;
  3344. } else if (ap->dfops->dop_low) {
  3345. if (xfs_inode_is_filestream(ap->ip))
  3346. args.type = XFS_ALLOCTYPE_FIRST_AG;
  3347. else
  3348. args.type = XFS_ALLOCTYPE_START_BNO;
  3349. args.total = args.minlen = ap->minlen;
  3350. } else {
  3351. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  3352. args.total = ap->total;
  3353. args.minlen = ap->minlen;
  3354. }
  3355. /* apply extent size hints if obtained earlier */
  3356. if (align) {
  3357. args.prod = align;
  3358. if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
  3359. args.mod = (xfs_extlen_t)(args.prod - args.mod);
  3360. } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
  3361. args.prod = 1;
  3362. args.mod = 0;
  3363. } else {
  3364. args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
  3365. if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
  3366. args.mod = (xfs_extlen_t)(args.prod - args.mod);
  3367. }
  3368. /*
  3369. * If we are not low on available data blocks, and the
  3370. * underlying logical volume manager is a stripe, and
  3371. * the file offset is zero then try to allocate data
  3372. * blocks on stripe unit boundary.
  3373. * NOTE: ap->aeof is only set if the allocation length
  3374. * is >= the stripe unit and the allocation offset is
  3375. * at the end of file.
  3376. */
  3377. if (!ap->dfops->dop_low && ap->aeof) {
  3378. if (!ap->offset) {
  3379. args.alignment = stripe_align;
  3380. atype = args.type;
  3381. isaligned = 1;
  3382. /*
  3383. * Adjust for alignment
  3384. */
  3385. if (blen > args.alignment && blen <= args.maxlen)
  3386. args.minlen = blen - args.alignment;
  3387. args.minalignslop = 0;
  3388. } else {
  3389. /*
  3390. * First try an exact bno allocation.
  3391. * If it fails then do a near or start bno
  3392. * allocation with alignment turned on.
  3393. */
  3394. atype = args.type;
  3395. tryagain = 1;
  3396. args.type = XFS_ALLOCTYPE_THIS_BNO;
  3397. args.alignment = 1;
  3398. /*
  3399. * Compute the minlen+alignment for the
  3400. * next case. Set slop so that the value
  3401. * of minlen+alignment+slop doesn't go up
  3402. * between the calls.
  3403. */
  3404. if (blen > stripe_align && blen <= args.maxlen)
  3405. nextminlen = blen - stripe_align;
  3406. else
  3407. nextminlen = args.minlen;
  3408. if (nextminlen + stripe_align > args.minlen + 1)
  3409. args.minalignslop =
  3410. nextminlen + stripe_align -
  3411. args.minlen - 1;
  3412. else
  3413. args.minalignslop = 0;
  3414. }
  3415. } else {
  3416. args.alignment = 1;
  3417. args.minalignslop = 0;
  3418. }
  3419. args.minleft = ap->minleft;
  3420. args.wasdel = ap->wasdel;
  3421. args.resv = XFS_AG_RESV_NONE;
  3422. args.datatype = ap->datatype;
  3423. if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
  3424. args.ip = ap->ip;
  3425. error = xfs_alloc_vextent(&args);
  3426. if (error)
  3427. return error;
  3428. if (tryagain && args.fsbno == NULLFSBLOCK) {
  3429. /*
  3430. * Exact allocation failed. Now try with alignment
  3431. * turned on.
  3432. */
  3433. args.type = atype;
  3434. args.fsbno = ap->blkno;
  3435. args.alignment = stripe_align;
  3436. args.minlen = nextminlen;
  3437. args.minalignslop = 0;
  3438. isaligned = 1;
  3439. if ((error = xfs_alloc_vextent(&args)))
  3440. return error;
  3441. }
  3442. if (isaligned && args.fsbno == NULLFSBLOCK) {
  3443. /*
  3444. * allocation failed, so turn off alignment and
  3445. * try again.
  3446. */
  3447. args.type = atype;
  3448. args.fsbno = ap->blkno;
  3449. args.alignment = 0;
  3450. if ((error = xfs_alloc_vextent(&args)))
  3451. return error;
  3452. }
  3453. if (args.fsbno == NULLFSBLOCK && nullfb &&
  3454. args.minlen > ap->minlen) {
  3455. args.minlen = ap->minlen;
  3456. args.type = XFS_ALLOCTYPE_START_BNO;
  3457. args.fsbno = ap->blkno;
  3458. if ((error = xfs_alloc_vextent(&args)))
  3459. return error;
  3460. }
  3461. if (args.fsbno == NULLFSBLOCK && nullfb) {
  3462. args.fsbno = 0;
  3463. args.type = XFS_ALLOCTYPE_FIRST_AG;
  3464. args.total = ap->minlen;
  3465. if ((error = xfs_alloc_vextent(&args)))
  3466. return error;
  3467. ap->dfops->dop_low = true;
  3468. }
  3469. if (args.fsbno != NULLFSBLOCK) {
  3470. /*
  3471. * check the allocation happened at the same or higher AG than
  3472. * the first block that was allocated.
  3473. */
  3474. ASSERT(*ap->firstblock == NULLFSBLOCK ||
  3475. XFS_FSB_TO_AGNO(mp, *ap->firstblock) <=
  3476. XFS_FSB_TO_AGNO(mp, args.fsbno));
  3477. ap->blkno = args.fsbno;
  3478. if (*ap->firstblock == NULLFSBLOCK)
  3479. *ap->firstblock = args.fsbno;
  3480. ASSERT(nullfb || fb_agno <= args.agno);
  3481. ap->length = args.len;
  3482. if (!(ap->flags & XFS_BMAPI_COWFORK))
  3483. ap->ip->i_d.di_nblocks += args.len;
  3484. xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
  3485. if (ap->wasdel)
  3486. ap->ip->i_delayed_blks -= args.len;
  3487. /*
  3488. * Adjust the disk quota also. This was reserved
  3489. * earlier.
  3490. */
  3491. xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
  3492. ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
  3493. XFS_TRANS_DQ_BCOUNT,
  3494. (long) args.len);
  3495. } else {
  3496. ap->blkno = NULLFSBLOCK;
  3497. ap->length = 0;
  3498. }
  3499. return 0;
  3500. }
  3501. /*
  3502. * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
  3503. * It figures out where to ask the underlying allocator to put the new extent.
  3504. */
  3505. STATIC int
  3506. xfs_bmap_alloc(
  3507. struct xfs_bmalloca *ap) /* bmap alloc argument struct */
  3508. {
  3509. if (XFS_IS_REALTIME_INODE(ap->ip) &&
  3510. xfs_alloc_is_userdata(ap->datatype))
  3511. return xfs_bmap_rtalloc(ap);
  3512. return xfs_bmap_btalloc(ap);
  3513. }
  3514. /* Trim extent to fit a logical block range. */
  3515. void
  3516. xfs_trim_extent(
  3517. struct xfs_bmbt_irec *irec,
  3518. xfs_fileoff_t bno,
  3519. xfs_filblks_t len)
  3520. {
  3521. xfs_fileoff_t distance;
  3522. xfs_fileoff_t end = bno + len;
  3523. if (irec->br_startoff + irec->br_blockcount <= bno ||
  3524. irec->br_startoff >= end) {
  3525. irec->br_blockcount = 0;
  3526. return;
  3527. }
  3528. if (irec->br_startoff < bno) {
  3529. distance = bno - irec->br_startoff;
  3530. if (isnullstartblock(irec->br_startblock))
  3531. irec->br_startblock = DELAYSTARTBLOCK;
  3532. if (irec->br_startblock != DELAYSTARTBLOCK &&
  3533. irec->br_startblock != HOLESTARTBLOCK)
  3534. irec->br_startblock += distance;
  3535. irec->br_startoff += distance;
  3536. irec->br_blockcount -= distance;
  3537. }
  3538. if (end < irec->br_startoff + irec->br_blockcount) {
  3539. distance = irec->br_startoff + irec->br_blockcount - end;
  3540. irec->br_blockcount -= distance;
  3541. }
  3542. }
  3543. /*
  3544. * Trim the returned map to the required bounds
  3545. */
  3546. STATIC void
  3547. xfs_bmapi_trim_map(
  3548. struct xfs_bmbt_irec *mval,
  3549. struct xfs_bmbt_irec *got,
  3550. xfs_fileoff_t *bno,
  3551. xfs_filblks_t len,
  3552. xfs_fileoff_t obno,
  3553. xfs_fileoff_t end,
  3554. int n,
  3555. int flags)
  3556. {
  3557. if ((flags & XFS_BMAPI_ENTIRE) ||
  3558. got->br_startoff + got->br_blockcount <= obno) {
  3559. *mval = *got;
  3560. if (isnullstartblock(got->br_startblock))
  3561. mval->br_startblock = DELAYSTARTBLOCK;
  3562. return;
  3563. }
  3564. if (obno > *bno)
  3565. *bno = obno;
  3566. ASSERT((*bno >= obno) || (n == 0));
  3567. ASSERT(*bno < end);
  3568. mval->br_startoff = *bno;
  3569. if (isnullstartblock(got->br_startblock))
  3570. mval->br_startblock = DELAYSTARTBLOCK;
  3571. else
  3572. mval->br_startblock = got->br_startblock +
  3573. (*bno - got->br_startoff);
  3574. /*
  3575. * Return the minimum of what we got and what we asked for for
  3576. * the length. We can use the len variable here because it is
  3577. * modified below and we could have been there before coming
  3578. * here if the first part of the allocation didn't overlap what
  3579. * was asked for.
  3580. */
  3581. mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
  3582. got->br_blockcount - (*bno - got->br_startoff));
  3583. mval->br_state = got->br_state;
  3584. ASSERT(mval->br_blockcount <= len);
  3585. return;
  3586. }
  3587. /*
  3588. * Update and validate the extent map to return
  3589. */
  3590. STATIC void
  3591. xfs_bmapi_update_map(
  3592. struct xfs_bmbt_irec **map,
  3593. xfs_fileoff_t *bno,
  3594. xfs_filblks_t *len,
  3595. xfs_fileoff_t obno,
  3596. xfs_fileoff_t end,
  3597. int *n,
  3598. int flags)
  3599. {
  3600. xfs_bmbt_irec_t *mval = *map;
  3601. ASSERT((flags & XFS_BMAPI_ENTIRE) ||
  3602. ((mval->br_startoff + mval->br_blockcount) <= end));
  3603. ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
  3604. (mval->br_startoff < obno));
  3605. *bno = mval->br_startoff + mval->br_blockcount;
  3606. *len = end - *bno;
  3607. if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
  3608. /* update previous map with new information */
  3609. ASSERT(mval->br_startblock == mval[-1].br_startblock);
  3610. ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
  3611. ASSERT(mval->br_state == mval[-1].br_state);
  3612. mval[-1].br_blockcount = mval->br_blockcount;
  3613. mval[-1].br_state = mval->br_state;
  3614. } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
  3615. mval[-1].br_startblock != DELAYSTARTBLOCK &&
  3616. mval[-1].br_startblock != HOLESTARTBLOCK &&
  3617. mval->br_startblock == mval[-1].br_startblock +
  3618. mval[-1].br_blockcount &&
  3619. ((flags & XFS_BMAPI_IGSTATE) ||
  3620. mval[-1].br_state == mval->br_state)) {
  3621. ASSERT(mval->br_startoff ==
  3622. mval[-1].br_startoff + mval[-1].br_blockcount);
  3623. mval[-1].br_blockcount += mval->br_blockcount;
  3624. } else if (*n > 0 &&
  3625. mval->br_startblock == DELAYSTARTBLOCK &&
  3626. mval[-1].br_startblock == DELAYSTARTBLOCK &&
  3627. mval->br_startoff ==
  3628. mval[-1].br_startoff + mval[-1].br_blockcount) {
  3629. mval[-1].br_blockcount += mval->br_blockcount;
  3630. mval[-1].br_state = mval->br_state;
  3631. } else if (!((*n == 0) &&
  3632. ((mval->br_startoff + mval->br_blockcount) <=
  3633. obno))) {
  3634. mval++;
  3635. (*n)++;
  3636. }
  3637. *map = mval;
  3638. }
  3639. /*
  3640. * Map file blocks to filesystem blocks without allocation.
  3641. */
  3642. int
  3643. xfs_bmapi_read(
  3644. struct xfs_inode *ip,
  3645. xfs_fileoff_t bno,
  3646. xfs_filblks_t len,
  3647. struct xfs_bmbt_irec *mval,
  3648. int *nmap,
  3649. int flags)
  3650. {
  3651. struct xfs_mount *mp = ip->i_mount;
  3652. struct xfs_ifork *ifp;
  3653. struct xfs_bmbt_irec got;
  3654. xfs_fileoff_t obno;
  3655. xfs_fileoff_t end;
  3656. xfs_extnum_t idx;
  3657. int error;
  3658. bool eof = false;
  3659. int n = 0;
  3660. int whichfork = xfs_bmapi_whichfork(flags);
  3661. ASSERT(*nmap >= 1);
  3662. ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
  3663. XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK)));
  3664. ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
  3665. if (unlikely(XFS_TEST_ERROR(
  3666. (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
  3667. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
  3668. mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
  3669. XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
  3670. return -EFSCORRUPTED;
  3671. }
  3672. if (XFS_FORCED_SHUTDOWN(mp))
  3673. return -EIO;
  3674. XFS_STATS_INC(mp, xs_blk_mapr);
  3675. ifp = XFS_IFORK_PTR(ip, whichfork);
  3676. /* No CoW fork? Return a hole. */
  3677. if (whichfork == XFS_COW_FORK && !ifp) {
  3678. mval->br_startoff = bno;
  3679. mval->br_startblock = HOLESTARTBLOCK;
  3680. mval->br_blockcount = len;
  3681. mval->br_state = XFS_EXT_NORM;
  3682. *nmap = 1;
  3683. return 0;
  3684. }
  3685. if (!(ifp->if_flags & XFS_IFEXTENTS)) {
  3686. error = xfs_iread_extents(NULL, ip, whichfork);
  3687. if (error)
  3688. return error;
  3689. }
  3690. if (!xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got))
  3691. eof = true;
  3692. end = bno + len;
  3693. obno = bno;
  3694. while (bno < end && n < *nmap) {
  3695. /* Reading past eof, act as though there's a hole up to end. */
  3696. if (eof)
  3697. got.br_startoff = end;
  3698. if (got.br_startoff > bno) {
  3699. /* Reading in a hole. */
  3700. mval->br_startoff = bno;
  3701. mval->br_startblock = HOLESTARTBLOCK;
  3702. mval->br_blockcount =
  3703. XFS_FILBLKS_MIN(len, got.br_startoff - bno);
  3704. mval->br_state = XFS_EXT_NORM;
  3705. bno += mval->br_blockcount;
  3706. len -= mval->br_blockcount;
  3707. mval++;
  3708. n++;
  3709. continue;
  3710. }
  3711. /* set up the extent map to return. */
  3712. xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
  3713. xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
  3714. /* If we're done, stop now. */
  3715. if (bno >= end || n >= *nmap)
  3716. break;
  3717. /* Else go on to the next record. */
  3718. if (!xfs_iext_get_extent(ifp, ++idx, &got))
  3719. eof = true;
  3720. }
  3721. *nmap = n;
  3722. return 0;
  3723. }
  3724. /*
  3725. * Add a delayed allocation extent to an inode. Blocks are reserved from the
  3726. * global pool and the extent inserted into the inode in-core extent tree.
  3727. *
  3728. * On entry, got refers to the first extent beyond the offset of the extent to
  3729. * allocate or eof is specified if no such extent exists. On return, got refers
  3730. * to the extent record that was inserted to the inode fork.
  3731. *
  3732. * Note that the allocated extent may have been merged with contiguous extents
  3733. * during insertion into the inode fork. Thus, got does not reflect the current
  3734. * state of the inode fork on return. If necessary, the caller can use lastx to
  3735. * look up the updated record in the inode fork.
  3736. */
  3737. int
  3738. xfs_bmapi_reserve_delalloc(
  3739. struct xfs_inode *ip,
  3740. int whichfork,
  3741. xfs_fileoff_t off,
  3742. xfs_filblks_t len,
  3743. xfs_filblks_t prealloc,
  3744. struct xfs_bmbt_irec *got,
  3745. xfs_extnum_t *lastx,
  3746. int eof)
  3747. {
  3748. struct xfs_mount *mp = ip->i_mount;
  3749. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
  3750. xfs_extlen_t alen;
  3751. xfs_extlen_t indlen;
  3752. char rt = XFS_IS_REALTIME_INODE(ip);
  3753. xfs_extlen_t extsz;
  3754. int error;
  3755. xfs_fileoff_t aoff = off;
  3756. /*
  3757. * Cap the alloc length. Keep track of prealloc so we know whether to
  3758. * tag the inode before we return.
  3759. */
  3760. alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
  3761. if (!eof)
  3762. alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
  3763. if (prealloc && alen >= len)
  3764. prealloc = alen - len;
  3765. /* Figure out the extent size, adjust alen */
  3766. if (whichfork == XFS_COW_FORK)
  3767. extsz = xfs_get_cowextsz_hint(ip);
  3768. else
  3769. extsz = xfs_get_extsz_hint(ip);
  3770. if (extsz) {
  3771. struct xfs_bmbt_irec prev;
  3772. if (!xfs_iext_get_extent(ifp, *lastx - 1, &prev))
  3773. prev.br_startoff = NULLFILEOFF;
  3774. error = xfs_bmap_extsize_align(mp, got, &prev, extsz, rt, eof,
  3775. 1, 0, &aoff, &alen);
  3776. ASSERT(!error);
  3777. }
  3778. if (rt)
  3779. extsz = alen / mp->m_sb.sb_rextsize;
  3780. /*
  3781. * Make a transaction-less quota reservation for delayed allocation
  3782. * blocks. This number gets adjusted later. We return if we haven't
  3783. * allocated blocks already inside this loop.
  3784. */
  3785. error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
  3786. rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
  3787. if (error)
  3788. return error;
  3789. /*
  3790. * Split changing sb for alen and indlen since they could be coming
  3791. * from different places.
  3792. */
  3793. indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
  3794. ASSERT(indlen > 0);
  3795. if (rt) {
  3796. error = xfs_mod_frextents(mp, -((int64_t)extsz));
  3797. } else {
  3798. error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
  3799. }
  3800. if (error)
  3801. goto out_unreserve_quota;
  3802. error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
  3803. if (error)
  3804. goto out_unreserve_blocks;
  3805. ip->i_delayed_blks += alen;
  3806. got->br_startoff = aoff;
  3807. got->br_startblock = nullstartblock(indlen);
  3808. got->br_blockcount = alen;
  3809. got->br_state = XFS_EXT_NORM;
  3810. xfs_bmap_add_extent_hole_delay(ip, whichfork, lastx, got);
  3811. /*
  3812. * Tag the inode if blocks were preallocated. Note that COW fork
  3813. * preallocation can occur at the start or end of the extent, even when
  3814. * prealloc == 0, so we must also check the aligned offset and length.
  3815. */
  3816. if (whichfork == XFS_DATA_FORK && prealloc)
  3817. xfs_inode_set_eofblocks_tag(ip);
  3818. if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
  3819. xfs_inode_set_cowblocks_tag(ip);
  3820. return 0;
  3821. out_unreserve_blocks:
  3822. if (rt)
  3823. xfs_mod_frextents(mp, extsz);
  3824. else
  3825. xfs_mod_fdblocks(mp, alen, false);
  3826. out_unreserve_quota:
  3827. if (XFS_IS_QUOTA_ON(mp))
  3828. xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
  3829. XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
  3830. return error;
  3831. }
  3832. static int
  3833. xfs_bmapi_allocate(
  3834. struct xfs_bmalloca *bma)
  3835. {
  3836. struct xfs_mount *mp = bma->ip->i_mount;
  3837. int whichfork = xfs_bmapi_whichfork(bma->flags);
  3838. struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
  3839. int tmp_logflags = 0;
  3840. int error;
  3841. ASSERT(bma->length > 0);
  3842. /*
  3843. * For the wasdelay case, we could also just allocate the stuff asked
  3844. * for in this bmap call but that wouldn't be as good.
  3845. */
  3846. if (bma->wasdel) {
  3847. bma->length = (xfs_extlen_t)bma->got.br_blockcount;
  3848. bma->offset = bma->got.br_startoff;
  3849. if (bma->idx) {
  3850. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1),
  3851. &bma->prev);
  3852. }
  3853. } else {
  3854. bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
  3855. if (!bma->eof)
  3856. bma->length = XFS_FILBLKS_MIN(bma->length,
  3857. bma->got.br_startoff - bma->offset);
  3858. }
  3859. /*
  3860. * Set the data type being allocated. For the data fork, the first data
  3861. * in the file is treated differently to all other allocations. For the
  3862. * attribute fork, we only need to ensure the allocated range is not on
  3863. * the busy list.
  3864. */
  3865. if (!(bma->flags & XFS_BMAPI_METADATA)) {
  3866. bma->datatype = XFS_ALLOC_NOBUSY;
  3867. if (whichfork == XFS_DATA_FORK) {
  3868. if (bma->offset == 0)
  3869. bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
  3870. else
  3871. bma->datatype |= XFS_ALLOC_USERDATA;
  3872. }
  3873. if (bma->flags & XFS_BMAPI_ZERO)
  3874. bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
  3875. }
  3876. bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
  3877. /*
  3878. * Only want to do the alignment at the eof if it is userdata and
  3879. * allocation length is larger than a stripe unit.
  3880. */
  3881. if (mp->m_dalign && bma->length >= mp->m_dalign &&
  3882. !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
  3883. error = xfs_bmap_isaeof(bma, whichfork);
  3884. if (error)
  3885. return error;
  3886. }
  3887. error = xfs_bmap_alloc(bma);
  3888. if (error)
  3889. return error;
  3890. if (bma->cur)
  3891. bma->cur->bc_private.b.firstblock = *bma->firstblock;
  3892. if (bma->blkno == NULLFSBLOCK)
  3893. return 0;
  3894. if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
  3895. bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
  3896. bma->cur->bc_private.b.firstblock = *bma->firstblock;
  3897. bma->cur->bc_private.b.dfops = bma->dfops;
  3898. }
  3899. /*
  3900. * Bump the number of extents we've allocated
  3901. * in this call.
  3902. */
  3903. bma->nallocs++;
  3904. if (bma->cur)
  3905. bma->cur->bc_private.b.flags =
  3906. bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
  3907. bma->got.br_startoff = bma->offset;
  3908. bma->got.br_startblock = bma->blkno;
  3909. bma->got.br_blockcount = bma->length;
  3910. bma->got.br_state = XFS_EXT_NORM;
  3911. /*
  3912. * In the data fork, a wasdelay extent has been initialized, so
  3913. * shouldn't be flagged as unwritten.
  3914. *
  3915. * For the cow fork, however, we convert delalloc reservations
  3916. * (extents allocated for speculative preallocation) to
  3917. * allocated unwritten extents, and only convert the unwritten
  3918. * extents to real extents when we're about to write the data.
  3919. */
  3920. if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) &&
  3921. (bma->flags & XFS_BMAPI_PREALLOC) &&
  3922. xfs_sb_version_hasextflgbit(&mp->m_sb))
  3923. bma->got.br_state = XFS_EXT_UNWRITTEN;
  3924. if (bma->wasdel)
  3925. error = xfs_bmap_add_extent_delay_real(bma, whichfork);
  3926. else
  3927. error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
  3928. whichfork, &bma->idx, &bma->cur, &bma->got,
  3929. bma->firstblock, bma->dfops, &bma->logflags);
  3930. bma->logflags |= tmp_logflags;
  3931. if (error)
  3932. return error;
  3933. /*
  3934. * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
  3935. * or xfs_bmap_add_extent_hole_real might have merged it into one of
  3936. * the neighbouring ones.
  3937. */
  3938. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
  3939. ASSERT(bma->got.br_startoff <= bma->offset);
  3940. ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
  3941. bma->offset + bma->length);
  3942. ASSERT(bma->got.br_state == XFS_EXT_NORM ||
  3943. bma->got.br_state == XFS_EXT_UNWRITTEN);
  3944. return 0;
  3945. }
  3946. STATIC int
  3947. xfs_bmapi_convert_unwritten(
  3948. struct xfs_bmalloca *bma,
  3949. struct xfs_bmbt_irec *mval,
  3950. xfs_filblks_t len,
  3951. int flags)
  3952. {
  3953. int whichfork = xfs_bmapi_whichfork(flags);
  3954. struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
  3955. int tmp_logflags = 0;
  3956. int error;
  3957. /* check if we need to do unwritten->real conversion */
  3958. if (mval->br_state == XFS_EXT_UNWRITTEN &&
  3959. (flags & XFS_BMAPI_PREALLOC))
  3960. return 0;
  3961. /* check if we need to do real->unwritten conversion */
  3962. if (mval->br_state == XFS_EXT_NORM &&
  3963. (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
  3964. (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
  3965. return 0;
  3966. /*
  3967. * Modify (by adding) the state flag, if writing.
  3968. */
  3969. ASSERT(mval->br_blockcount <= len);
  3970. if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
  3971. bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
  3972. bma->ip, whichfork);
  3973. bma->cur->bc_private.b.firstblock = *bma->firstblock;
  3974. bma->cur->bc_private.b.dfops = bma->dfops;
  3975. }
  3976. mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
  3977. ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
  3978. /*
  3979. * Before insertion into the bmbt, zero the range being converted
  3980. * if required.
  3981. */
  3982. if (flags & XFS_BMAPI_ZERO) {
  3983. error = xfs_zero_extent(bma->ip, mval->br_startblock,
  3984. mval->br_blockcount);
  3985. if (error)
  3986. return error;
  3987. }
  3988. error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
  3989. &bma->idx, &bma->cur, mval, bma->firstblock, bma->dfops,
  3990. &tmp_logflags);
  3991. /*
  3992. * Log the inode core unconditionally in the unwritten extent conversion
  3993. * path because the conversion might not have done so (e.g., if the
  3994. * extent count hasn't changed). We need to make sure the inode is dirty
  3995. * in the transaction for the sake of fsync(), even if nothing has
  3996. * changed, because fsync() will not force the log for this transaction
  3997. * unless it sees the inode pinned.
  3998. *
  3999. * Note: If we're only converting cow fork extents, there aren't
  4000. * any on-disk updates to make, so we don't need to log anything.
  4001. */
  4002. if (whichfork != XFS_COW_FORK)
  4003. bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
  4004. if (error)
  4005. return error;
  4006. /*
  4007. * Update our extent pointer, given that
  4008. * xfs_bmap_add_extent_unwritten_real might have merged it into one
  4009. * of the neighbouring ones.
  4010. */
  4011. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
  4012. /*
  4013. * We may have combined previously unwritten space with written space,
  4014. * so generate another request.
  4015. */
  4016. if (mval->br_blockcount < len)
  4017. return -EAGAIN;
  4018. return 0;
  4019. }
  4020. /*
  4021. * Map file blocks to filesystem blocks, and allocate blocks or convert the
  4022. * extent state if necessary. Details behaviour is controlled by the flags
  4023. * parameter. Only allocates blocks from a single allocation group, to avoid
  4024. * locking problems.
  4025. *
  4026. * The returned value in "firstblock" from the first call in a transaction
  4027. * must be remembered and presented to subsequent calls in "firstblock".
  4028. * An upper bound for the number of blocks to be allocated is supplied to
  4029. * the first call in "total"; if no allocation group has that many free
  4030. * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
  4031. */
  4032. int
  4033. xfs_bmapi_write(
  4034. struct xfs_trans *tp, /* transaction pointer */
  4035. struct xfs_inode *ip, /* incore inode */
  4036. xfs_fileoff_t bno, /* starting file offs. mapped */
  4037. xfs_filblks_t len, /* length to map in file */
  4038. int flags, /* XFS_BMAPI_... */
  4039. xfs_fsblock_t *firstblock, /* first allocated block
  4040. controls a.g. for allocs */
  4041. xfs_extlen_t total, /* total blocks needed */
  4042. struct xfs_bmbt_irec *mval, /* output: map values */
  4043. int *nmap, /* i/o: mval size/count */
  4044. struct xfs_defer_ops *dfops) /* i/o: list extents to free */
  4045. {
  4046. struct xfs_mount *mp = ip->i_mount;
  4047. struct xfs_ifork *ifp;
  4048. struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
  4049. xfs_fileoff_t end; /* end of mapped file region */
  4050. bool eof = false; /* after the end of extents */
  4051. int error; /* error return */
  4052. int n; /* current extent index */
  4053. xfs_fileoff_t obno; /* old block number (offset) */
  4054. int whichfork; /* data or attr fork */
  4055. #ifdef DEBUG
  4056. xfs_fileoff_t orig_bno; /* original block number value */
  4057. int orig_flags; /* original flags arg value */
  4058. xfs_filblks_t orig_len; /* original value of len arg */
  4059. struct xfs_bmbt_irec *orig_mval; /* original value of mval */
  4060. int orig_nmap; /* original value of *nmap */
  4061. orig_bno = bno;
  4062. orig_len = len;
  4063. orig_flags = flags;
  4064. orig_mval = mval;
  4065. orig_nmap = *nmap;
  4066. #endif
  4067. whichfork = xfs_bmapi_whichfork(flags);
  4068. ASSERT(*nmap >= 1);
  4069. ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
  4070. ASSERT(!(flags & XFS_BMAPI_IGSTATE));
  4071. ASSERT(tp != NULL ||
  4072. (flags & (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)) ==
  4073. (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK));
  4074. ASSERT(len > 0);
  4075. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
  4076. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  4077. ASSERT(!(flags & XFS_BMAPI_REMAP));
  4078. /* zeroing is for currently only for data extents, not metadata */
  4079. ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
  4080. (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
  4081. /*
  4082. * we can allocate unwritten extents or pre-zero allocated blocks,
  4083. * but it makes no sense to do both at once. This would result in
  4084. * zeroing the unwritten extent twice, but it still being an
  4085. * unwritten extent....
  4086. */
  4087. ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
  4088. (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
  4089. if (unlikely(XFS_TEST_ERROR(
  4090. (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
  4091. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
  4092. mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
  4093. XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
  4094. return -EFSCORRUPTED;
  4095. }
  4096. if (XFS_FORCED_SHUTDOWN(mp))
  4097. return -EIO;
  4098. ifp = XFS_IFORK_PTR(ip, whichfork);
  4099. XFS_STATS_INC(mp, xs_blk_mapw);
  4100. if (*firstblock == NULLFSBLOCK) {
  4101. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
  4102. bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
  4103. else
  4104. bma.minleft = 1;
  4105. } else {
  4106. bma.minleft = 0;
  4107. }
  4108. if (!(ifp->if_flags & XFS_IFEXTENTS)) {
  4109. error = xfs_iread_extents(tp, ip, whichfork);
  4110. if (error)
  4111. goto error0;
  4112. }
  4113. n = 0;
  4114. end = bno + len;
  4115. obno = bno;
  4116. if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.idx, &bma.got))
  4117. eof = true;
  4118. if (!xfs_iext_get_extent(ifp, bma.idx - 1, &bma.prev))
  4119. bma.prev.br_startoff = NULLFILEOFF;
  4120. bma.tp = tp;
  4121. bma.ip = ip;
  4122. bma.total = total;
  4123. bma.datatype = 0;
  4124. bma.dfops = dfops;
  4125. bma.firstblock = firstblock;
  4126. while (bno < end && n < *nmap) {
  4127. bool need_alloc = false, wasdelay = false;
  4128. /* in hole or beyoned EOF? */
  4129. if (eof || bma.got.br_startoff > bno) {
  4130. if (flags & XFS_BMAPI_DELALLOC) {
  4131. /*
  4132. * For the COW fork we can reasonably get a
  4133. * request for converting an extent that races
  4134. * with other threads already having converted
  4135. * part of it, as there converting COW to
  4136. * regular blocks is not protected using the
  4137. * IOLOCK.
  4138. */
  4139. ASSERT(flags & XFS_BMAPI_COWFORK);
  4140. if (!(flags & XFS_BMAPI_COWFORK)) {
  4141. error = -EIO;
  4142. goto error0;
  4143. }
  4144. if (eof || bno >= end)
  4145. break;
  4146. } else {
  4147. need_alloc = true;
  4148. }
  4149. } else if (isnullstartblock(bma.got.br_startblock)) {
  4150. wasdelay = true;
  4151. }
  4152. /*
  4153. * First, deal with the hole before the allocated space
  4154. * that we found, if any.
  4155. */
  4156. if (need_alloc || wasdelay) {
  4157. bma.eof = eof;
  4158. bma.conv = !!(flags & XFS_BMAPI_CONVERT);
  4159. bma.wasdel = wasdelay;
  4160. bma.offset = bno;
  4161. bma.flags = flags;
  4162. /*
  4163. * There's a 32/64 bit type mismatch between the
  4164. * allocation length request (which can be 64 bits in
  4165. * length) and the bma length request, which is
  4166. * xfs_extlen_t and therefore 32 bits. Hence we have to
  4167. * check for 32-bit overflows and handle them here.
  4168. */
  4169. if (len > (xfs_filblks_t)MAXEXTLEN)
  4170. bma.length = MAXEXTLEN;
  4171. else
  4172. bma.length = len;
  4173. ASSERT(len > 0);
  4174. ASSERT(bma.length > 0);
  4175. error = xfs_bmapi_allocate(&bma);
  4176. if (error)
  4177. goto error0;
  4178. if (bma.blkno == NULLFSBLOCK)
  4179. break;
  4180. /*
  4181. * If this is a CoW allocation, record the data in
  4182. * the refcount btree for orphan recovery.
  4183. */
  4184. if (whichfork == XFS_COW_FORK) {
  4185. error = xfs_refcount_alloc_cow_extent(mp, dfops,
  4186. bma.blkno, bma.length);
  4187. if (error)
  4188. goto error0;
  4189. }
  4190. }
  4191. /* Deal with the allocated space we found. */
  4192. xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
  4193. end, n, flags);
  4194. /* Execute unwritten extent conversion if necessary */
  4195. error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
  4196. if (error == -EAGAIN)
  4197. continue;
  4198. if (error)
  4199. goto error0;
  4200. /* update the extent map to return */
  4201. xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
  4202. /*
  4203. * If we're done, stop now. Stop when we've allocated
  4204. * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
  4205. * the transaction may get too big.
  4206. */
  4207. if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
  4208. break;
  4209. /* Else go on to the next record. */
  4210. bma.prev = bma.got;
  4211. if (!xfs_iext_get_extent(ifp, ++bma.idx, &bma.got))
  4212. eof = true;
  4213. }
  4214. *nmap = n;
  4215. /*
  4216. * Transform from btree to extents, give it cur.
  4217. */
  4218. if (xfs_bmap_wants_extents(ip, whichfork)) {
  4219. int tmp_logflags = 0;
  4220. ASSERT(bma.cur);
  4221. error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
  4222. &tmp_logflags, whichfork);
  4223. bma.logflags |= tmp_logflags;
  4224. if (error)
  4225. goto error0;
  4226. }
  4227. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
  4228. XFS_IFORK_NEXTENTS(ip, whichfork) >
  4229. XFS_IFORK_MAXEXT(ip, whichfork));
  4230. error = 0;
  4231. error0:
  4232. /*
  4233. * Log everything. Do this after conversion, there's no point in
  4234. * logging the extent records if we've converted to btree format.
  4235. */
  4236. if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
  4237. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
  4238. bma.logflags &= ~xfs_ilog_fext(whichfork);
  4239. else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
  4240. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
  4241. bma.logflags &= ~xfs_ilog_fbroot(whichfork);
  4242. /*
  4243. * Log whatever the flags say, even if error. Otherwise we might miss
  4244. * detecting a case where the data is changed, there's an error,
  4245. * and it's not logged so we don't shutdown when we should.
  4246. */
  4247. if (bma.logflags)
  4248. xfs_trans_log_inode(tp, ip, bma.logflags);
  4249. if (bma.cur) {
  4250. if (!error) {
  4251. ASSERT(*firstblock == NULLFSBLOCK ||
  4252. XFS_FSB_TO_AGNO(mp, *firstblock) <=
  4253. XFS_FSB_TO_AGNO(mp,
  4254. bma.cur->bc_private.b.firstblock));
  4255. *firstblock = bma.cur->bc_private.b.firstblock;
  4256. }
  4257. xfs_btree_del_cursor(bma.cur,
  4258. error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  4259. }
  4260. if (!error)
  4261. xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
  4262. orig_nmap, *nmap);
  4263. return error;
  4264. }
  4265. static int
  4266. xfs_bmapi_remap(
  4267. struct xfs_trans *tp,
  4268. struct xfs_inode *ip,
  4269. xfs_fileoff_t bno,
  4270. xfs_filblks_t len,
  4271. xfs_fsblock_t startblock,
  4272. struct xfs_defer_ops *dfops)
  4273. {
  4274. struct xfs_mount *mp = ip->i_mount;
  4275. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
  4276. struct xfs_btree_cur *cur = NULL;
  4277. xfs_fsblock_t firstblock = NULLFSBLOCK;
  4278. struct xfs_bmbt_irec got;
  4279. xfs_extnum_t idx;
  4280. int logflags = 0, error;
  4281. ASSERT(len > 0);
  4282. ASSERT(len <= (xfs_filblks_t)MAXEXTLEN);
  4283. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  4284. if (unlikely(XFS_TEST_ERROR(
  4285. (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
  4286. XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
  4287. mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
  4288. XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp);
  4289. return -EFSCORRUPTED;
  4290. }
  4291. if (XFS_FORCED_SHUTDOWN(mp))
  4292. return -EIO;
  4293. if (!(ifp->if_flags & XFS_IFEXTENTS)) {
  4294. error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
  4295. if (error)
  4296. return error;
  4297. }
  4298. if (xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got)) {
  4299. /* make sure we only reflink into a hole. */
  4300. ASSERT(got.br_startoff > bno);
  4301. ASSERT(got.br_startoff - bno >= len);
  4302. }
  4303. ip->i_d.di_nblocks += len;
  4304. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  4305. if (ifp->if_flags & XFS_IFBROOT) {
  4306. cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
  4307. cur->bc_private.b.firstblock = firstblock;
  4308. cur->bc_private.b.dfops = dfops;
  4309. cur->bc_private.b.flags = 0;
  4310. }
  4311. got.br_startoff = bno;
  4312. got.br_startblock = startblock;
  4313. got.br_blockcount = len;
  4314. got.br_state = XFS_EXT_NORM;
  4315. error = xfs_bmap_add_extent_hole_real(tp, ip, XFS_DATA_FORK, &idx, &cur,
  4316. &got, &firstblock, dfops, &logflags);
  4317. if (error)
  4318. goto error0;
  4319. if (xfs_bmap_wants_extents(ip, XFS_DATA_FORK)) {
  4320. int tmp_logflags = 0;
  4321. error = xfs_bmap_btree_to_extents(tp, ip, cur,
  4322. &tmp_logflags, XFS_DATA_FORK);
  4323. logflags |= tmp_logflags;
  4324. }
  4325. error0:
  4326. if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS)
  4327. logflags &= ~XFS_ILOG_DEXT;
  4328. else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
  4329. logflags &= ~XFS_ILOG_DBROOT;
  4330. if (logflags)
  4331. xfs_trans_log_inode(tp, ip, logflags);
  4332. if (cur) {
  4333. xfs_btree_del_cursor(cur,
  4334. error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  4335. }
  4336. return error;
  4337. }
  4338. /*
  4339. * When a delalloc extent is split (e.g., due to a hole punch), the original
  4340. * indlen reservation must be shared across the two new extents that are left
  4341. * behind.
  4342. *
  4343. * Given the original reservation and the worst case indlen for the two new
  4344. * extents (as calculated by xfs_bmap_worst_indlen()), split the original
  4345. * reservation fairly across the two new extents. If necessary, steal available
  4346. * blocks from a deleted extent to make up a reservation deficiency (e.g., if
  4347. * ores == 1). The number of stolen blocks is returned. The availability and
  4348. * subsequent accounting of stolen blocks is the responsibility of the caller.
  4349. */
  4350. static xfs_filblks_t
  4351. xfs_bmap_split_indlen(
  4352. xfs_filblks_t ores, /* original res. */
  4353. xfs_filblks_t *indlen1, /* ext1 worst indlen */
  4354. xfs_filblks_t *indlen2, /* ext2 worst indlen */
  4355. xfs_filblks_t avail) /* stealable blocks */
  4356. {
  4357. xfs_filblks_t len1 = *indlen1;
  4358. xfs_filblks_t len2 = *indlen2;
  4359. xfs_filblks_t nres = len1 + len2; /* new total res. */
  4360. xfs_filblks_t stolen = 0;
  4361. xfs_filblks_t resfactor;
  4362. /*
  4363. * Steal as many blocks as we can to try and satisfy the worst case
  4364. * indlen for both new extents.
  4365. */
  4366. if (ores < nres && avail)
  4367. stolen = XFS_FILBLKS_MIN(nres - ores, avail);
  4368. ores += stolen;
  4369. /* nothing else to do if we've satisfied the new reservation */
  4370. if (ores >= nres)
  4371. return stolen;
  4372. /*
  4373. * We can't meet the total required reservation for the two extents.
  4374. * Calculate the percent of the overall shortage between both extents
  4375. * and apply this percentage to each of the requested indlen values.
  4376. * This distributes the shortage fairly and reduces the chances that one
  4377. * of the two extents is left with nothing when extents are repeatedly
  4378. * split.
  4379. */
  4380. resfactor = (ores * 100);
  4381. do_div(resfactor, nres);
  4382. len1 *= resfactor;
  4383. do_div(len1, 100);
  4384. len2 *= resfactor;
  4385. do_div(len2, 100);
  4386. ASSERT(len1 + len2 <= ores);
  4387. ASSERT(len1 < *indlen1 && len2 < *indlen2);
  4388. /*
  4389. * Hand out the remainder to each extent. If one of the two reservations
  4390. * is zero, we want to make sure that one gets a block first. The loop
  4391. * below starts with len1, so hand len2 a block right off the bat if it
  4392. * is zero.
  4393. */
  4394. ores -= (len1 + len2);
  4395. ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
  4396. if (ores && !len2 && *indlen2) {
  4397. len2++;
  4398. ores--;
  4399. }
  4400. while (ores) {
  4401. if (len1 < *indlen1) {
  4402. len1++;
  4403. ores--;
  4404. }
  4405. if (!ores)
  4406. break;
  4407. if (len2 < *indlen2) {
  4408. len2++;
  4409. ores--;
  4410. }
  4411. }
  4412. *indlen1 = len1;
  4413. *indlen2 = len2;
  4414. return stolen;
  4415. }
  4416. int
  4417. xfs_bmap_del_extent_delay(
  4418. struct xfs_inode *ip,
  4419. int whichfork,
  4420. xfs_extnum_t *idx,
  4421. struct xfs_bmbt_irec *got,
  4422. struct xfs_bmbt_irec *del)
  4423. {
  4424. struct xfs_mount *mp = ip->i_mount;
  4425. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
  4426. struct xfs_bmbt_irec new;
  4427. int64_t da_old, da_new, da_diff = 0;
  4428. xfs_fileoff_t del_endoff, got_endoff;
  4429. xfs_filblks_t got_indlen, new_indlen, stolen;
  4430. int error = 0, state = 0;
  4431. bool isrt;
  4432. XFS_STATS_INC(mp, xs_del_exlist);
  4433. isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
  4434. del_endoff = del->br_startoff + del->br_blockcount;
  4435. got_endoff = got->br_startoff + got->br_blockcount;
  4436. da_old = startblockval(got->br_startblock);
  4437. da_new = 0;
  4438. ASSERT(*idx >= 0);
  4439. ASSERT(*idx <= xfs_iext_count(ifp));
  4440. ASSERT(del->br_blockcount > 0);
  4441. ASSERT(got->br_startoff <= del->br_startoff);
  4442. ASSERT(got_endoff >= del_endoff);
  4443. if (isrt) {
  4444. uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
  4445. do_div(rtexts, mp->m_sb.sb_rextsize);
  4446. xfs_mod_frextents(mp, rtexts);
  4447. }
  4448. /*
  4449. * Update the inode delalloc counter now and wait to update the
  4450. * sb counters as we might have to borrow some blocks for the
  4451. * indirect block accounting.
  4452. */
  4453. error = xfs_trans_reserve_quota_nblks(NULL, ip,
  4454. -((long)del->br_blockcount), 0,
  4455. isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
  4456. if (error)
  4457. return error;
  4458. ip->i_delayed_blks -= del->br_blockcount;
  4459. if (whichfork == XFS_COW_FORK)
  4460. state |= BMAP_COWFORK;
  4461. if (got->br_startoff == del->br_startoff)
  4462. state |= BMAP_LEFT_CONTIG;
  4463. if (got_endoff == del_endoff)
  4464. state |= BMAP_RIGHT_CONTIG;
  4465. switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
  4466. case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
  4467. /*
  4468. * Matches the whole extent. Delete the entry.
  4469. */
  4470. xfs_iext_remove(ip, *idx, 1, state);
  4471. --*idx;
  4472. break;
  4473. case BMAP_LEFT_CONTIG:
  4474. /*
  4475. * Deleting the first part of the extent.
  4476. */
  4477. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  4478. got->br_startoff = del_endoff;
  4479. got->br_blockcount -= del->br_blockcount;
  4480. da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
  4481. got->br_blockcount), da_old);
  4482. got->br_startblock = nullstartblock((int)da_new);
  4483. xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
  4484. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  4485. break;
  4486. case BMAP_RIGHT_CONTIG:
  4487. /*
  4488. * Deleting the last part of the extent.
  4489. */
  4490. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  4491. got->br_blockcount = got->br_blockcount - del->br_blockcount;
  4492. da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
  4493. got->br_blockcount), da_old);
  4494. got->br_startblock = nullstartblock((int)da_new);
  4495. xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
  4496. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  4497. break;
  4498. case 0:
  4499. /*
  4500. * Deleting the middle of the extent.
  4501. *
  4502. * Distribute the original indlen reservation across the two new
  4503. * extents. Steal blocks from the deleted extent if necessary.
  4504. * Stealing blocks simply fudges the fdblocks accounting below.
  4505. * Warn if either of the new indlen reservations is zero as this
  4506. * can lead to delalloc problems.
  4507. */
  4508. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  4509. got->br_blockcount = del->br_startoff - got->br_startoff;
  4510. got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
  4511. new.br_blockcount = got_endoff - del_endoff;
  4512. new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
  4513. WARN_ON_ONCE(!got_indlen || !new_indlen);
  4514. stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
  4515. del->br_blockcount);
  4516. got->br_startblock = nullstartblock((int)got_indlen);
  4517. xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
  4518. trace_xfs_bmap_post_update(ip, *idx, 0, _THIS_IP_);
  4519. new.br_startoff = del_endoff;
  4520. new.br_state = got->br_state;
  4521. new.br_startblock = nullstartblock((int)new_indlen);
  4522. ++*idx;
  4523. xfs_iext_insert(ip, *idx, 1, &new, state);
  4524. da_new = got_indlen + new_indlen - stolen;
  4525. del->br_blockcount -= stolen;
  4526. break;
  4527. }
  4528. ASSERT(da_old >= da_new);
  4529. da_diff = da_old - da_new;
  4530. if (!isrt)
  4531. da_diff += del->br_blockcount;
  4532. if (da_diff)
  4533. xfs_mod_fdblocks(mp, da_diff, false);
  4534. return error;
  4535. }
  4536. void
  4537. xfs_bmap_del_extent_cow(
  4538. struct xfs_inode *ip,
  4539. xfs_extnum_t *idx,
  4540. struct xfs_bmbt_irec *got,
  4541. struct xfs_bmbt_irec *del)
  4542. {
  4543. struct xfs_mount *mp = ip->i_mount;
  4544. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
  4545. struct xfs_bmbt_irec new;
  4546. xfs_fileoff_t del_endoff, got_endoff;
  4547. int state = BMAP_COWFORK;
  4548. XFS_STATS_INC(mp, xs_del_exlist);
  4549. del_endoff = del->br_startoff + del->br_blockcount;
  4550. got_endoff = got->br_startoff + got->br_blockcount;
  4551. ASSERT(*idx >= 0);
  4552. ASSERT(*idx <= xfs_iext_count(ifp));
  4553. ASSERT(del->br_blockcount > 0);
  4554. ASSERT(got->br_startoff <= del->br_startoff);
  4555. ASSERT(got_endoff >= del_endoff);
  4556. ASSERT(!isnullstartblock(got->br_startblock));
  4557. if (got->br_startoff == del->br_startoff)
  4558. state |= BMAP_LEFT_CONTIG;
  4559. if (got_endoff == del_endoff)
  4560. state |= BMAP_RIGHT_CONTIG;
  4561. switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
  4562. case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
  4563. /*
  4564. * Matches the whole extent. Delete the entry.
  4565. */
  4566. xfs_iext_remove(ip, *idx, 1, state);
  4567. --*idx;
  4568. break;
  4569. case BMAP_LEFT_CONTIG:
  4570. /*
  4571. * Deleting the first part of the extent.
  4572. */
  4573. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  4574. got->br_startoff = del_endoff;
  4575. got->br_blockcount -= del->br_blockcount;
  4576. got->br_startblock = del->br_startblock + del->br_blockcount;
  4577. xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
  4578. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  4579. break;
  4580. case BMAP_RIGHT_CONTIG:
  4581. /*
  4582. * Deleting the last part of the extent.
  4583. */
  4584. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  4585. got->br_blockcount -= del->br_blockcount;
  4586. xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
  4587. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  4588. break;
  4589. case 0:
  4590. /*
  4591. * Deleting the middle of the extent.
  4592. */
  4593. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  4594. got->br_blockcount = del->br_startoff - got->br_startoff;
  4595. xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
  4596. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  4597. new.br_startoff = del_endoff;
  4598. new.br_blockcount = got_endoff - del_endoff;
  4599. new.br_state = got->br_state;
  4600. new.br_startblock = del->br_startblock + del->br_blockcount;
  4601. ++*idx;
  4602. xfs_iext_insert(ip, *idx, 1, &new, state);
  4603. break;
  4604. }
  4605. }
  4606. /*
  4607. * Called by xfs_bmapi to update file extent records and the btree
  4608. * after removing space (or undoing a delayed allocation).
  4609. */
  4610. STATIC int /* error */
  4611. xfs_bmap_del_extent(
  4612. xfs_inode_t *ip, /* incore inode pointer */
  4613. xfs_trans_t *tp, /* current transaction pointer */
  4614. xfs_extnum_t *idx, /* extent number to update/delete */
  4615. struct xfs_defer_ops *dfops, /* list of extents to be freed */
  4616. xfs_btree_cur_t *cur, /* if null, not a btree */
  4617. xfs_bmbt_irec_t *del, /* data to remove from extents */
  4618. int *logflagsp, /* inode logging flags */
  4619. int whichfork, /* data or attr fork */
  4620. int bflags) /* bmapi flags */
  4621. {
  4622. xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
  4623. xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
  4624. xfs_fsblock_t del_endblock=0; /* first block past del */
  4625. xfs_fileoff_t del_endoff; /* first offset past del */
  4626. int delay; /* current block is delayed allocated */
  4627. int do_fx; /* free extent at end of routine */
  4628. xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
  4629. int error; /* error return value */
  4630. int flags; /* inode logging flags */
  4631. xfs_bmbt_irec_t got; /* current extent entry */
  4632. xfs_fileoff_t got_endoff; /* first offset past got */
  4633. int i; /* temp state */
  4634. xfs_ifork_t *ifp; /* inode fork pointer */
  4635. xfs_mount_t *mp; /* mount structure */
  4636. xfs_filblks_t nblks; /* quota/sb block count */
  4637. xfs_bmbt_irec_t new; /* new record to be inserted */
  4638. /* REFERENCED */
  4639. uint qfield; /* quota field to update */
  4640. xfs_filblks_t temp; /* for indirect length calculations */
  4641. xfs_filblks_t temp2; /* for indirect length calculations */
  4642. int state = 0;
  4643. mp = ip->i_mount;
  4644. XFS_STATS_INC(mp, xs_del_exlist);
  4645. if (whichfork == XFS_ATTR_FORK)
  4646. state |= BMAP_ATTRFORK;
  4647. else if (whichfork == XFS_COW_FORK)
  4648. state |= BMAP_COWFORK;
  4649. ifp = XFS_IFORK_PTR(ip, whichfork);
  4650. ASSERT((*idx >= 0) && (*idx < xfs_iext_count(ifp)));
  4651. ASSERT(del->br_blockcount > 0);
  4652. ep = xfs_iext_get_ext(ifp, *idx);
  4653. xfs_bmbt_get_all(ep, &got);
  4654. ASSERT(got.br_startoff <= del->br_startoff);
  4655. del_endoff = del->br_startoff + del->br_blockcount;
  4656. got_endoff = got.br_startoff + got.br_blockcount;
  4657. ASSERT(got_endoff >= del_endoff);
  4658. delay = isnullstartblock(got.br_startblock);
  4659. ASSERT(isnullstartblock(del->br_startblock) == delay);
  4660. flags = 0;
  4661. qfield = 0;
  4662. error = 0;
  4663. /*
  4664. * If deleting a real allocation, must free up the disk space.
  4665. */
  4666. if (!delay) {
  4667. flags = XFS_ILOG_CORE;
  4668. /*
  4669. * Realtime allocation. Free it and record di_nblocks update.
  4670. */
  4671. if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
  4672. xfs_fsblock_t bno;
  4673. xfs_filblks_t len;
  4674. ASSERT(do_mod(del->br_blockcount,
  4675. mp->m_sb.sb_rextsize) == 0);
  4676. ASSERT(do_mod(del->br_startblock,
  4677. mp->m_sb.sb_rextsize) == 0);
  4678. bno = del->br_startblock;
  4679. len = del->br_blockcount;
  4680. do_div(bno, mp->m_sb.sb_rextsize);
  4681. do_div(len, mp->m_sb.sb_rextsize);
  4682. error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
  4683. if (error)
  4684. goto done;
  4685. do_fx = 0;
  4686. nblks = len * mp->m_sb.sb_rextsize;
  4687. qfield = XFS_TRANS_DQ_RTBCOUNT;
  4688. }
  4689. /*
  4690. * Ordinary allocation.
  4691. */
  4692. else {
  4693. do_fx = 1;
  4694. nblks = del->br_blockcount;
  4695. qfield = XFS_TRANS_DQ_BCOUNT;
  4696. }
  4697. /*
  4698. * Set up del_endblock and cur for later.
  4699. */
  4700. del_endblock = del->br_startblock + del->br_blockcount;
  4701. if (cur) {
  4702. if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
  4703. got.br_startblock, got.br_blockcount,
  4704. &i)))
  4705. goto done;
  4706. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  4707. }
  4708. da_old = da_new = 0;
  4709. } else {
  4710. da_old = startblockval(got.br_startblock);
  4711. da_new = 0;
  4712. nblks = 0;
  4713. do_fx = 0;
  4714. }
  4715. /*
  4716. * Set flag value to use in switch statement.
  4717. * Left-contig is 2, right-contig is 1.
  4718. */
  4719. switch (((got.br_startoff == del->br_startoff) << 1) |
  4720. (got_endoff == del_endoff)) {
  4721. case 3:
  4722. /*
  4723. * Matches the whole extent. Delete the entry.
  4724. */
  4725. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  4726. xfs_iext_remove(ip, *idx, 1,
  4727. whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
  4728. --*idx;
  4729. if (delay)
  4730. break;
  4731. XFS_IFORK_NEXT_SET(ip, whichfork,
  4732. XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
  4733. flags |= XFS_ILOG_CORE;
  4734. if (!cur) {
  4735. flags |= xfs_ilog_fext(whichfork);
  4736. break;
  4737. }
  4738. if ((error = xfs_btree_delete(cur, &i)))
  4739. goto done;
  4740. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  4741. break;
  4742. case 2:
  4743. /*
  4744. * Deleting the first part of the extent.
  4745. */
  4746. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  4747. xfs_bmbt_set_startoff(ep, del_endoff);
  4748. temp = got.br_blockcount - del->br_blockcount;
  4749. xfs_bmbt_set_blockcount(ep, temp);
  4750. if (delay) {
  4751. temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
  4752. da_old);
  4753. xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
  4754. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  4755. da_new = temp;
  4756. break;
  4757. }
  4758. xfs_bmbt_set_startblock(ep, del_endblock);
  4759. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  4760. if (!cur) {
  4761. flags |= xfs_ilog_fext(whichfork);
  4762. break;
  4763. }
  4764. if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
  4765. got.br_blockcount - del->br_blockcount,
  4766. got.br_state)))
  4767. goto done;
  4768. break;
  4769. case 1:
  4770. /*
  4771. * Deleting the last part of the extent.
  4772. */
  4773. temp = got.br_blockcount - del->br_blockcount;
  4774. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  4775. xfs_bmbt_set_blockcount(ep, temp);
  4776. if (delay) {
  4777. temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
  4778. da_old);
  4779. xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
  4780. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  4781. da_new = temp;
  4782. break;
  4783. }
  4784. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  4785. if (!cur) {
  4786. flags |= xfs_ilog_fext(whichfork);
  4787. break;
  4788. }
  4789. if ((error = xfs_bmbt_update(cur, got.br_startoff,
  4790. got.br_startblock,
  4791. got.br_blockcount - del->br_blockcount,
  4792. got.br_state)))
  4793. goto done;
  4794. break;
  4795. case 0:
  4796. /*
  4797. * Deleting the middle of the extent.
  4798. */
  4799. temp = del->br_startoff - got.br_startoff;
  4800. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  4801. xfs_bmbt_set_blockcount(ep, temp);
  4802. new.br_startoff = del_endoff;
  4803. temp2 = got_endoff - del_endoff;
  4804. new.br_blockcount = temp2;
  4805. new.br_state = got.br_state;
  4806. if (!delay) {
  4807. new.br_startblock = del_endblock;
  4808. flags |= XFS_ILOG_CORE;
  4809. if (cur) {
  4810. if ((error = xfs_bmbt_update(cur,
  4811. got.br_startoff,
  4812. got.br_startblock, temp,
  4813. got.br_state)))
  4814. goto done;
  4815. if ((error = xfs_btree_increment(cur, 0, &i)))
  4816. goto done;
  4817. cur->bc_rec.b = new;
  4818. error = xfs_btree_insert(cur, &i);
  4819. if (error && error != -ENOSPC)
  4820. goto done;
  4821. /*
  4822. * If get no-space back from btree insert,
  4823. * it tried a split, and we have a zero
  4824. * block reservation.
  4825. * Fix up our state and return the error.
  4826. */
  4827. if (error == -ENOSPC) {
  4828. /*
  4829. * Reset the cursor, don't trust
  4830. * it after any insert operation.
  4831. */
  4832. if ((error = xfs_bmbt_lookup_eq(cur,
  4833. got.br_startoff,
  4834. got.br_startblock,
  4835. temp, &i)))
  4836. goto done;
  4837. XFS_WANT_CORRUPTED_GOTO(mp,
  4838. i == 1, done);
  4839. /*
  4840. * Update the btree record back
  4841. * to the original value.
  4842. */
  4843. if ((error = xfs_bmbt_update(cur,
  4844. got.br_startoff,
  4845. got.br_startblock,
  4846. got.br_blockcount,
  4847. got.br_state)))
  4848. goto done;
  4849. /*
  4850. * Reset the extent record back
  4851. * to the original value.
  4852. */
  4853. xfs_bmbt_set_blockcount(ep,
  4854. got.br_blockcount);
  4855. flags = 0;
  4856. error = -ENOSPC;
  4857. goto done;
  4858. }
  4859. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  4860. } else
  4861. flags |= xfs_ilog_fext(whichfork);
  4862. XFS_IFORK_NEXT_SET(ip, whichfork,
  4863. XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
  4864. } else {
  4865. xfs_filblks_t stolen;
  4866. ASSERT(whichfork == XFS_DATA_FORK);
  4867. /*
  4868. * Distribute the original indlen reservation across the
  4869. * two new extents. Steal blocks from the deleted extent
  4870. * if necessary. Stealing blocks simply fudges the
  4871. * fdblocks accounting in xfs_bunmapi().
  4872. */
  4873. temp = xfs_bmap_worst_indlen(ip, got.br_blockcount);
  4874. temp2 = xfs_bmap_worst_indlen(ip, new.br_blockcount);
  4875. stolen = xfs_bmap_split_indlen(da_old, &temp, &temp2,
  4876. del->br_blockcount);
  4877. da_new = temp + temp2 - stolen;
  4878. del->br_blockcount -= stolen;
  4879. /*
  4880. * Set the reservation for each extent. Warn if either
  4881. * is zero as this can lead to delalloc problems.
  4882. */
  4883. WARN_ON_ONCE(!temp || !temp2);
  4884. xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
  4885. new.br_startblock = nullstartblock((int)temp2);
  4886. }
  4887. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  4888. xfs_iext_insert(ip, *idx + 1, 1, &new, state);
  4889. ++*idx;
  4890. break;
  4891. }
  4892. /* remove reverse mapping */
  4893. if (!delay) {
  4894. error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del);
  4895. if (error)
  4896. goto done;
  4897. }
  4898. /*
  4899. * If we need to, add to list of extents to delete.
  4900. */
  4901. if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
  4902. if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
  4903. error = xfs_refcount_decrease_extent(mp, dfops, del);
  4904. if (error)
  4905. goto done;
  4906. } else
  4907. xfs_bmap_add_free(mp, dfops, del->br_startblock,
  4908. del->br_blockcount, NULL);
  4909. }
  4910. /*
  4911. * Adjust inode # blocks in the file.
  4912. */
  4913. if (nblks)
  4914. ip->i_d.di_nblocks -= nblks;
  4915. /*
  4916. * Adjust quota data.
  4917. */
  4918. if (qfield && !(bflags & XFS_BMAPI_REMAP))
  4919. xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
  4920. /*
  4921. * Account for change in delayed indirect blocks.
  4922. * Nothing to do for disk quota accounting here.
  4923. */
  4924. ASSERT(da_old >= da_new);
  4925. if (da_old > da_new)
  4926. xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), false);
  4927. done:
  4928. *logflagsp = flags;
  4929. return error;
  4930. }
  4931. /*
  4932. * Unmap (remove) blocks from a file.
  4933. * If nexts is nonzero then the number of extents to remove is limited to
  4934. * that value. If not all extents in the block range can be removed then
  4935. * *done is set.
  4936. */
  4937. int /* error */
  4938. __xfs_bunmapi(
  4939. xfs_trans_t *tp, /* transaction pointer */
  4940. struct xfs_inode *ip, /* incore inode */
  4941. xfs_fileoff_t bno, /* starting offset to unmap */
  4942. xfs_filblks_t *rlen, /* i/o: amount remaining */
  4943. int flags, /* misc flags */
  4944. xfs_extnum_t nexts, /* number of extents max */
  4945. xfs_fsblock_t *firstblock, /* first allocated block
  4946. controls a.g. for allocs */
  4947. struct xfs_defer_ops *dfops) /* i/o: deferred updates */
  4948. {
  4949. xfs_btree_cur_t *cur; /* bmap btree cursor */
  4950. xfs_bmbt_irec_t del; /* extent being deleted */
  4951. int error; /* error return value */
  4952. xfs_extnum_t extno; /* extent number in list */
  4953. xfs_bmbt_irec_t got; /* current extent record */
  4954. xfs_ifork_t *ifp; /* inode fork pointer */
  4955. int isrt; /* freeing in rt area */
  4956. xfs_extnum_t lastx; /* last extent index used */
  4957. int logflags; /* transaction logging flags */
  4958. xfs_extlen_t mod; /* rt extent offset */
  4959. xfs_mount_t *mp; /* mount structure */
  4960. xfs_fileoff_t start; /* first file offset deleted */
  4961. int tmp_logflags; /* partial logging flags */
  4962. int wasdel; /* was a delayed alloc extent */
  4963. int whichfork; /* data or attribute fork */
  4964. xfs_fsblock_t sum;
  4965. xfs_filblks_t len = *rlen; /* length to unmap in file */
  4966. trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
  4967. whichfork = xfs_bmapi_whichfork(flags);
  4968. ASSERT(whichfork != XFS_COW_FORK);
  4969. ifp = XFS_IFORK_PTR(ip, whichfork);
  4970. if (unlikely(
  4971. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
  4972. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
  4973. XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
  4974. ip->i_mount);
  4975. return -EFSCORRUPTED;
  4976. }
  4977. mp = ip->i_mount;
  4978. if (XFS_FORCED_SHUTDOWN(mp))
  4979. return -EIO;
  4980. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  4981. ASSERT(len > 0);
  4982. ASSERT(nexts >= 0);
  4983. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  4984. (error = xfs_iread_extents(tp, ip, whichfork)))
  4985. return error;
  4986. if (xfs_iext_count(ifp) == 0) {
  4987. *rlen = 0;
  4988. return 0;
  4989. }
  4990. XFS_STATS_INC(mp, xs_blk_unmap);
  4991. isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
  4992. start = bno;
  4993. bno = start + len - 1;
  4994. /*
  4995. * Check to see if the given block number is past the end of the
  4996. * file, back up to the last block if so...
  4997. */
  4998. if (!xfs_iext_lookup_extent(ip, ifp, bno, &lastx, &got)) {
  4999. ASSERT(lastx > 0);
  5000. xfs_iext_get_extent(ifp, --lastx, &got);
  5001. bno = got.br_startoff + got.br_blockcount - 1;
  5002. }
  5003. logflags = 0;
  5004. if (ifp->if_flags & XFS_IFBROOT) {
  5005. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
  5006. cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
  5007. cur->bc_private.b.firstblock = *firstblock;
  5008. cur->bc_private.b.dfops = dfops;
  5009. cur->bc_private.b.flags = 0;
  5010. } else
  5011. cur = NULL;
  5012. if (isrt) {
  5013. /*
  5014. * Synchronize by locking the bitmap inode.
  5015. */
  5016. xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
  5017. xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
  5018. xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
  5019. xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
  5020. }
  5021. extno = 0;
  5022. while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
  5023. (nexts == 0 || extno < nexts)) {
  5024. /*
  5025. * Is the found extent after a hole in which bno lives?
  5026. * Just back up to the previous extent, if so.
  5027. */
  5028. if (got.br_startoff > bno) {
  5029. if (--lastx < 0)
  5030. break;
  5031. xfs_iext_get_extent(ifp, lastx, &got);
  5032. }
  5033. /*
  5034. * Is the last block of this extent before the range
  5035. * we're supposed to delete? If so, we're done.
  5036. */
  5037. bno = XFS_FILEOFF_MIN(bno,
  5038. got.br_startoff + got.br_blockcount - 1);
  5039. if (bno < start)
  5040. break;
  5041. /*
  5042. * Then deal with the (possibly delayed) allocated space
  5043. * we found.
  5044. */
  5045. del = got;
  5046. wasdel = isnullstartblock(del.br_startblock);
  5047. if (got.br_startoff < start) {
  5048. del.br_startoff = start;
  5049. del.br_blockcount -= start - got.br_startoff;
  5050. if (!wasdel)
  5051. del.br_startblock += start - got.br_startoff;
  5052. }
  5053. if (del.br_startoff + del.br_blockcount > bno + 1)
  5054. del.br_blockcount = bno + 1 - del.br_startoff;
  5055. sum = del.br_startblock + del.br_blockcount;
  5056. if (isrt &&
  5057. (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
  5058. /*
  5059. * Realtime extent not lined up at the end.
  5060. * The extent could have been split into written
  5061. * and unwritten pieces, or we could just be
  5062. * unmapping part of it. But we can't really
  5063. * get rid of part of a realtime extent.
  5064. */
  5065. if (del.br_state == XFS_EXT_UNWRITTEN ||
  5066. !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
  5067. /*
  5068. * This piece is unwritten, or we're not
  5069. * using unwritten extents. Skip over it.
  5070. */
  5071. ASSERT(bno >= mod);
  5072. bno -= mod > del.br_blockcount ?
  5073. del.br_blockcount : mod;
  5074. if (bno < got.br_startoff) {
  5075. if (--lastx >= 0)
  5076. xfs_bmbt_get_all(xfs_iext_get_ext(
  5077. ifp, lastx), &got);
  5078. }
  5079. continue;
  5080. }
  5081. /*
  5082. * It's written, turn it unwritten.
  5083. * This is better than zeroing it.
  5084. */
  5085. ASSERT(del.br_state == XFS_EXT_NORM);
  5086. ASSERT(tp->t_blk_res > 0);
  5087. /*
  5088. * If this spans a realtime extent boundary,
  5089. * chop it back to the start of the one we end at.
  5090. */
  5091. if (del.br_blockcount > mod) {
  5092. del.br_startoff += del.br_blockcount - mod;
  5093. del.br_startblock += del.br_blockcount - mod;
  5094. del.br_blockcount = mod;
  5095. }
  5096. del.br_state = XFS_EXT_UNWRITTEN;
  5097. error = xfs_bmap_add_extent_unwritten_real(tp, ip,
  5098. whichfork, &lastx, &cur, &del,
  5099. firstblock, dfops, &logflags);
  5100. if (error)
  5101. goto error0;
  5102. goto nodelete;
  5103. }
  5104. if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
  5105. /*
  5106. * Realtime extent is lined up at the end but not
  5107. * at the front. We'll get rid of full extents if
  5108. * we can.
  5109. */
  5110. mod = mp->m_sb.sb_rextsize - mod;
  5111. if (del.br_blockcount > mod) {
  5112. del.br_blockcount -= mod;
  5113. del.br_startoff += mod;
  5114. del.br_startblock += mod;
  5115. } else if ((del.br_startoff == start &&
  5116. (del.br_state == XFS_EXT_UNWRITTEN ||
  5117. tp->t_blk_res == 0)) ||
  5118. !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
  5119. /*
  5120. * Can't make it unwritten. There isn't
  5121. * a full extent here so just skip it.
  5122. */
  5123. ASSERT(bno >= del.br_blockcount);
  5124. bno -= del.br_blockcount;
  5125. if (got.br_startoff > bno && --lastx >= 0)
  5126. xfs_iext_get_extent(ifp, lastx, &got);
  5127. continue;
  5128. } else if (del.br_state == XFS_EXT_UNWRITTEN) {
  5129. struct xfs_bmbt_irec prev;
  5130. /*
  5131. * This one is already unwritten.
  5132. * It must have a written left neighbor.
  5133. * Unwrite the killed part of that one and
  5134. * try again.
  5135. */
  5136. ASSERT(lastx > 0);
  5137. xfs_iext_get_extent(ifp, lastx - 1, &prev);
  5138. ASSERT(prev.br_state == XFS_EXT_NORM);
  5139. ASSERT(!isnullstartblock(prev.br_startblock));
  5140. ASSERT(del.br_startblock ==
  5141. prev.br_startblock + prev.br_blockcount);
  5142. if (prev.br_startoff < start) {
  5143. mod = start - prev.br_startoff;
  5144. prev.br_blockcount -= mod;
  5145. prev.br_startblock += mod;
  5146. prev.br_startoff = start;
  5147. }
  5148. prev.br_state = XFS_EXT_UNWRITTEN;
  5149. lastx--;
  5150. error = xfs_bmap_add_extent_unwritten_real(tp,
  5151. ip, whichfork, &lastx, &cur,
  5152. &prev, firstblock, dfops,
  5153. &logflags);
  5154. if (error)
  5155. goto error0;
  5156. goto nodelete;
  5157. } else {
  5158. ASSERT(del.br_state == XFS_EXT_NORM);
  5159. del.br_state = XFS_EXT_UNWRITTEN;
  5160. error = xfs_bmap_add_extent_unwritten_real(tp,
  5161. ip, whichfork, &lastx, &cur,
  5162. &del, firstblock, dfops,
  5163. &logflags);
  5164. if (error)
  5165. goto error0;
  5166. goto nodelete;
  5167. }
  5168. }
  5169. /*
  5170. * If it's the case where the directory code is running
  5171. * with no block reservation, and the deleted block is in
  5172. * the middle of its extent, and the resulting insert
  5173. * of an extent would cause transformation to btree format,
  5174. * then reject it. The calling code will then swap
  5175. * blocks around instead.
  5176. * We have to do this now, rather than waiting for the
  5177. * conversion to btree format, since the transaction
  5178. * will be dirty.
  5179. */
  5180. if (!wasdel && tp->t_blk_res == 0 &&
  5181. XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
  5182. XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */
  5183. XFS_IFORK_MAXEXT(ip, whichfork) &&
  5184. del.br_startoff > got.br_startoff &&
  5185. del.br_startoff + del.br_blockcount <
  5186. got.br_startoff + got.br_blockcount) {
  5187. error = -ENOSPC;
  5188. goto error0;
  5189. }
  5190. /*
  5191. * Unreserve quota and update realtime free space, if
  5192. * appropriate. If delayed allocation, update the inode delalloc
  5193. * counter now and wait to update the sb counters as
  5194. * xfs_bmap_del_extent() might need to borrow some blocks.
  5195. */
  5196. if (wasdel) {
  5197. ASSERT(startblockval(del.br_startblock) > 0);
  5198. if (isrt) {
  5199. xfs_filblks_t rtexts;
  5200. rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
  5201. do_div(rtexts, mp->m_sb.sb_rextsize);
  5202. xfs_mod_frextents(mp, (int64_t)rtexts);
  5203. (void)xfs_trans_reserve_quota_nblks(NULL,
  5204. ip, -((long)del.br_blockcount), 0,
  5205. XFS_QMOPT_RES_RTBLKS);
  5206. } else {
  5207. (void)xfs_trans_reserve_quota_nblks(NULL,
  5208. ip, -((long)del.br_blockcount), 0,
  5209. XFS_QMOPT_RES_REGBLKS);
  5210. }
  5211. ip->i_delayed_blks -= del.br_blockcount;
  5212. if (cur)
  5213. cur->bc_private.b.flags |=
  5214. XFS_BTCUR_BPRV_WASDEL;
  5215. } else if (cur)
  5216. cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
  5217. error = xfs_bmap_del_extent(ip, tp, &lastx, dfops, cur, &del,
  5218. &tmp_logflags, whichfork, flags);
  5219. logflags |= tmp_logflags;
  5220. if (error)
  5221. goto error0;
  5222. if (!isrt && wasdel)
  5223. xfs_mod_fdblocks(mp, (int64_t)del.br_blockcount, false);
  5224. bno = del.br_startoff - 1;
  5225. nodelete:
  5226. /*
  5227. * If not done go on to the next (previous) record.
  5228. */
  5229. if (bno != (xfs_fileoff_t)-1 && bno >= start) {
  5230. if (lastx >= 0) {
  5231. xfs_iext_get_extent(ifp, lastx, &got);
  5232. if (got.br_startoff > bno && --lastx >= 0)
  5233. xfs_iext_get_extent(ifp, lastx, &got);
  5234. }
  5235. extno++;
  5236. }
  5237. }
  5238. if (bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0)
  5239. *rlen = 0;
  5240. else
  5241. *rlen = bno - start + 1;
  5242. /*
  5243. * Convert to a btree if necessary.
  5244. */
  5245. if (xfs_bmap_needs_btree(ip, whichfork)) {
  5246. ASSERT(cur == NULL);
  5247. error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops,
  5248. &cur, 0, &tmp_logflags, whichfork);
  5249. logflags |= tmp_logflags;
  5250. if (error)
  5251. goto error0;
  5252. }
  5253. /*
  5254. * transform from btree to extents, give it cur
  5255. */
  5256. else if (xfs_bmap_wants_extents(ip, whichfork)) {
  5257. ASSERT(cur != NULL);
  5258. error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
  5259. whichfork);
  5260. logflags |= tmp_logflags;
  5261. if (error)
  5262. goto error0;
  5263. }
  5264. /*
  5265. * transform from extents to local?
  5266. */
  5267. error = 0;
  5268. error0:
  5269. /*
  5270. * Log everything. Do this after conversion, there's no point in
  5271. * logging the extent records if we've converted to btree format.
  5272. */
  5273. if ((logflags & xfs_ilog_fext(whichfork)) &&
  5274. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
  5275. logflags &= ~xfs_ilog_fext(whichfork);
  5276. else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
  5277. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
  5278. logflags &= ~xfs_ilog_fbroot(whichfork);
  5279. /*
  5280. * Log inode even in the error case, if the transaction
  5281. * is dirty we'll need to shut down the filesystem.
  5282. */
  5283. if (logflags)
  5284. xfs_trans_log_inode(tp, ip, logflags);
  5285. if (cur) {
  5286. if (!error) {
  5287. *firstblock = cur->bc_private.b.firstblock;
  5288. cur->bc_private.b.allocated = 0;
  5289. }
  5290. xfs_btree_del_cursor(cur,
  5291. error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  5292. }
  5293. return error;
  5294. }
  5295. /* Unmap a range of a file. */
  5296. int
  5297. xfs_bunmapi(
  5298. xfs_trans_t *tp,
  5299. struct xfs_inode *ip,
  5300. xfs_fileoff_t bno,
  5301. xfs_filblks_t len,
  5302. int flags,
  5303. xfs_extnum_t nexts,
  5304. xfs_fsblock_t *firstblock,
  5305. struct xfs_defer_ops *dfops,
  5306. int *done)
  5307. {
  5308. int error;
  5309. error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock,
  5310. dfops);
  5311. *done = (len == 0);
  5312. return error;
  5313. }
  5314. /*
  5315. * Determine whether an extent shift can be accomplished by a merge with the
  5316. * extent that precedes the target hole of the shift.
  5317. */
  5318. STATIC bool
  5319. xfs_bmse_can_merge(
  5320. struct xfs_bmbt_irec *left, /* preceding extent */
  5321. struct xfs_bmbt_irec *got, /* current extent to shift */
  5322. xfs_fileoff_t shift) /* shift fsb */
  5323. {
  5324. xfs_fileoff_t startoff;
  5325. startoff = got->br_startoff - shift;
  5326. /*
  5327. * The extent, once shifted, must be adjacent in-file and on-disk with
  5328. * the preceding extent.
  5329. */
  5330. if ((left->br_startoff + left->br_blockcount != startoff) ||
  5331. (left->br_startblock + left->br_blockcount != got->br_startblock) ||
  5332. (left->br_state != got->br_state) ||
  5333. (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
  5334. return false;
  5335. return true;
  5336. }
  5337. /*
  5338. * A bmap extent shift adjusts the file offset of an extent to fill a preceding
  5339. * hole in the file. If an extent shift would result in the extent being fully
  5340. * adjacent to the extent that currently precedes the hole, we can merge with
  5341. * the preceding extent rather than do the shift.
  5342. *
  5343. * This function assumes the caller has verified a shift-by-merge is possible
  5344. * with the provided extents via xfs_bmse_can_merge().
  5345. */
  5346. STATIC int
  5347. xfs_bmse_merge(
  5348. struct xfs_inode *ip,
  5349. int whichfork,
  5350. xfs_fileoff_t shift, /* shift fsb */
  5351. int current_ext, /* idx of gotp */
  5352. struct xfs_bmbt_rec_host *gotp, /* extent to shift */
  5353. struct xfs_bmbt_rec_host *leftp, /* preceding extent */
  5354. struct xfs_btree_cur *cur,
  5355. int *logflags) /* output */
  5356. {
  5357. struct xfs_bmbt_irec got;
  5358. struct xfs_bmbt_irec left;
  5359. xfs_filblks_t blockcount;
  5360. int error, i;
  5361. struct xfs_mount *mp = ip->i_mount;
  5362. xfs_bmbt_get_all(gotp, &got);
  5363. xfs_bmbt_get_all(leftp, &left);
  5364. blockcount = left.br_blockcount + got.br_blockcount;
  5365. ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  5366. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  5367. ASSERT(xfs_bmse_can_merge(&left, &got, shift));
  5368. /*
  5369. * Merge the in-core extents. Note that the host record pointers and
  5370. * current_ext index are invalid once the extent has been removed via
  5371. * xfs_iext_remove().
  5372. */
  5373. xfs_bmbt_set_blockcount(leftp, blockcount);
  5374. xfs_iext_remove(ip, current_ext, 1, 0);
  5375. /*
  5376. * Update the on-disk extent count, the btree if necessary and log the
  5377. * inode.
  5378. */
  5379. XFS_IFORK_NEXT_SET(ip, whichfork,
  5380. XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
  5381. *logflags |= XFS_ILOG_CORE;
  5382. if (!cur) {
  5383. *logflags |= XFS_ILOG_DEXT;
  5384. return 0;
  5385. }
  5386. /* lookup and remove the extent to merge */
  5387. error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
  5388. got.br_blockcount, &i);
  5389. if (error)
  5390. return error;
  5391. XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
  5392. error = xfs_btree_delete(cur, &i);
  5393. if (error)
  5394. return error;
  5395. XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
  5396. /* lookup and update size of the previous extent */
  5397. error = xfs_bmbt_lookup_eq(cur, left.br_startoff, left.br_startblock,
  5398. left.br_blockcount, &i);
  5399. if (error)
  5400. return error;
  5401. XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
  5402. left.br_blockcount = blockcount;
  5403. return xfs_bmbt_update(cur, left.br_startoff, left.br_startblock,
  5404. left.br_blockcount, left.br_state);
  5405. }
  5406. /*
  5407. * Shift a single extent.
  5408. */
  5409. STATIC int
  5410. xfs_bmse_shift_one(
  5411. struct xfs_inode *ip,
  5412. int whichfork,
  5413. xfs_fileoff_t offset_shift_fsb,
  5414. int *current_ext,
  5415. struct xfs_bmbt_rec_host *gotp,
  5416. struct xfs_btree_cur *cur,
  5417. int *logflags,
  5418. enum shift_direction direction,
  5419. struct xfs_defer_ops *dfops)
  5420. {
  5421. struct xfs_ifork *ifp;
  5422. struct xfs_mount *mp;
  5423. xfs_fileoff_t startoff;
  5424. struct xfs_bmbt_rec_host *adj_irecp;
  5425. struct xfs_bmbt_irec got;
  5426. struct xfs_bmbt_irec adj_irec;
  5427. int error;
  5428. int i;
  5429. int total_extents;
  5430. mp = ip->i_mount;
  5431. ifp = XFS_IFORK_PTR(ip, whichfork);
  5432. total_extents = xfs_iext_count(ifp);
  5433. xfs_bmbt_get_all(gotp, &got);
  5434. /* delalloc extents should be prevented by caller */
  5435. XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock));
  5436. if (direction == SHIFT_LEFT) {
  5437. startoff = got.br_startoff - offset_shift_fsb;
  5438. /*
  5439. * Check for merge if we've got an extent to the left,
  5440. * otherwise make sure there's enough room at the start
  5441. * of the file for the shift.
  5442. */
  5443. if (!*current_ext) {
  5444. if (got.br_startoff < offset_shift_fsb)
  5445. return -EINVAL;
  5446. goto update_current_ext;
  5447. }
  5448. /*
  5449. * grab the left extent and check for a large
  5450. * enough hole.
  5451. */
  5452. adj_irecp = xfs_iext_get_ext(ifp, *current_ext - 1);
  5453. xfs_bmbt_get_all(adj_irecp, &adj_irec);
  5454. if (startoff <
  5455. adj_irec.br_startoff + adj_irec.br_blockcount)
  5456. return -EINVAL;
  5457. /* check whether to merge the extent or shift it down */
  5458. if (xfs_bmse_can_merge(&adj_irec, &got,
  5459. offset_shift_fsb)) {
  5460. error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
  5461. *current_ext, gotp, adj_irecp,
  5462. cur, logflags);
  5463. if (error)
  5464. return error;
  5465. adj_irec = got;
  5466. goto update_rmap;
  5467. }
  5468. } else {
  5469. startoff = got.br_startoff + offset_shift_fsb;
  5470. /* nothing to move if this is the last extent */
  5471. if (*current_ext >= (total_extents - 1))
  5472. goto update_current_ext;
  5473. /*
  5474. * If this is not the last extent in the file, make sure there
  5475. * is enough room between current extent and next extent for
  5476. * accommodating the shift.
  5477. */
  5478. adj_irecp = xfs_iext_get_ext(ifp, *current_ext + 1);
  5479. xfs_bmbt_get_all(adj_irecp, &adj_irec);
  5480. if (startoff + got.br_blockcount > adj_irec.br_startoff)
  5481. return -EINVAL;
  5482. /*
  5483. * Unlike a left shift (which involves a hole punch),
  5484. * a right shift does not modify extent neighbors
  5485. * in any way. We should never find mergeable extents
  5486. * in this scenario. Check anyways and warn if we
  5487. * encounter two extents that could be one.
  5488. */
  5489. if (xfs_bmse_can_merge(&got, &adj_irec, offset_shift_fsb))
  5490. WARN_ON_ONCE(1);
  5491. }
  5492. /*
  5493. * Increment the extent index for the next iteration, update the start
  5494. * offset of the in-core extent and update the btree if applicable.
  5495. */
  5496. update_current_ext:
  5497. if (direction == SHIFT_LEFT)
  5498. (*current_ext)++;
  5499. else
  5500. (*current_ext)--;
  5501. xfs_bmbt_set_startoff(gotp, startoff);
  5502. *logflags |= XFS_ILOG_CORE;
  5503. adj_irec = got;
  5504. if (!cur) {
  5505. *logflags |= XFS_ILOG_DEXT;
  5506. goto update_rmap;
  5507. }
  5508. error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
  5509. got.br_blockcount, &i);
  5510. if (error)
  5511. return error;
  5512. XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
  5513. got.br_startoff = startoff;
  5514. error = xfs_bmbt_update(cur, got.br_startoff, got.br_startblock,
  5515. got.br_blockcount, got.br_state);
  5516. if (error)
  5517. return error;
  5518. update_rmap:
  5519. /* update reverse mapping */
  5520. error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &adj_irec);
  5521. if (error)
  5522. return error;
  5523. adj_irec.br_startoff = startoff;
  5524. return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &adj_irec);
  5525. }
  5526. /*
  5527. * Shift extent records to the left/right to cover/create a hole.
  5528. *
  5529. * The maximum number of extents to be shifted in a single operation is
  5530. * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
  5531. * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
  5532. * is the length by which each extent is shifted. If there is no hole to shift
  5533. * the extents into, this will be considered invalid operation and we abort
  5534. * immediately.
  5535. */
  5536. int
  5537. xfs_bmap_shift_extents(
  5538. struct xfs_trans *tp,
  5539. struct xfs_inode *ip,
  5540. xfs_fileoff_t *next_fsb,
  5541. xfs_fileoff_t offset_shift_fsb,
  5542. int *done,
  5543. xfs_fileoff_t stop_fsb,
  5544. xfs_fsblock_t *firstblock,
  5545. struct xfs_defer_ops *dfops,
  5546. enum shift_direction direction,
  5547. int num_exts)
  5548. {
  5549. struct xfs_btree_cur *cur = NULL;
  5550. struct xfs_bmbt_rec_host *gotp;
  5551. struct xfs_bmbt_irec got;
  5552. struct xfs_mount *mp = ip->i_mount;
  5553. struct xfs_ifork *ifp;
  5554. xfs_extnum_t nexts = 0;
  5555. xfs_extnum_t current_ext;
  5556. xfs_extnum_t total_extents;
  5557. xfs_extnum_t stop_extent;
  5558. int error = 0;
  5559. int whichfork = XFS_DATA_FORK;
  5560. int logflags = 0;
  5561. if (unlikely(XFS_TEST_ERROR(
  5562. (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
  5563. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
  5564. mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
  5565. XFS_ERROR_REPORT("xfs_bmap_shift_extents",
  5566. XFS_ERRLEVEL_LOW, mp);
  5567. return -EFSCORRUPTED;
  5568. }
  5569. if (XFS_FORCED_SHUTDOWN(mp))
  5570. return -EIO;
  5571. ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  5572. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  5573. ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT);
  5574. ASSERT(*next_fsb != NULLFSBLOCK || direction == SHIFT_RIGHT);
  5575. ifp = XFS_IFORK_PTR(ip, whichfork);
  5576. if (!(ifp->if_flags & XFS_IFEXTENTS)) {
  5577. /* Read in all the extents */
  5578. error = xfs_iread_extents(tp, ip, whichfork);
  5579. if (error)
  5580. return error;
  5581. }
  5582. if (ifp->if_flags & XFS_IFBROOT) {
  5583. cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
  5584. cur->bc_private.b.firstblock = *firstblock;
  5585. cur->bc_private.b.dfops = dfops;
  5586. cur->bc_private.b.flags = 0;
  5587. }
  5588. /*
  5589. * There may be delalloc extents in the data fork before the range we
  5590. * are collapsing out, so we cannot use the count of real extents here.
  5591. * Instead we have to calculate it from the incore fork.
  5592. */
  5593. total_extents = xfs_iext_count(ifp);
  5594. if (total_extents == 0) {
  5595. *done = 1;
  5596. goto del_cursor;
  5597. }
  5598. /*
  5599. * In case of first right shift, we need to initialize next_fsb
  5600. */
  5601. if (*next_fsb == NULLFSBLOCK) {
  5602. gotp = xfs_iext_get_ext(ifp, total_extents - 1);
  5603. xfs_bmbt_get_all(gotp, &got);
  5604. *next_fsb = got.br_startoff;
  5605. if (stop_fsb > *next_fsb) {
  5606. *done = 1;
  5607. goto del_cursor;
  5608. }
  5609. }
  5610. /* Lookup the extent index at which we have to stop */
  5611. if (direction == SHIFT_RIGHT) {
  5612. gotp = xfs_iext_bno_to_ext(ifp, stop_fsb, &stop_extent);
  5613. /* Make stop_extent exclusive of shift range */
  5614. stop_extent--;
  5615. } else
  5616. stop_extent = total_extents;
  5617. /*
  5618. * Look up the extent index for the fsb where we start shifting. We can
  5619. * henceforth iterate with current_ext as extent list changes are locked
  5620. * out via ilock.
  5621. *
  5622. * gotp can be null in 2 cases: 1) if there are no extents or 2)
  5623. * *next_fsb lies in a hole beyond which there are no extents. Either
  5624. * way, we are done.
  5625. */
  5626. gotp = xfs_iext_bno_to_ext(ifp, *next_fsb, &current_ext);
  5627. if (!gotp) {
  5628. *done = 1;
  5629. goto del_cursor;
  5630. }
  5631. /* some sanity checking before we finally start shifting extents */
  5632. if ((direction == SHIFT_LEFT && current_ext >= stop_extent) ||
  5633. (direction == SHIFT_RIGHT && current_ext <= stop_extent)) {
  5634. error = -EIO;
  5635. goto del_cursor;
  5636. }
  5637. while (nexts++ < num_exts) {
  5638. error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb,
  5639. &current_ext, gotp, cur, &logflags,
  5640. direction, dfops);
  5641. if (error)
  5642. goto del_cursor;
  5643. /*
  5644. * If there was an extent merge during the shift, the extent
  5645. * count can change. Update the total and grade the next record.
  5646. */
  5647. if (direction == SHIFT_LEFT) {
  5648. total_extents = xfs_iext_count(ifp);
  5649. stop_extent = total_extents;
  5650. }
  5651. if (current_ext == stop_extent) {
  5652. *done = 1;
  5653. *next_fsb = NULLFSBLOCK;
  5654. break;
  5655. }
  5656. gotp = xfs_iext_get_ext(ifp, current_ext);
  5657. }
  5658. if (!*done) {
  5659. xfs_bmbt_get_all(gotp, &got);
  5660. *next_fsb = got.br_startoff;
  5661. }
  5662. del_cursor:
  5663. if (cur)
  5664. xfs_btree_del_cursor(cur,
  5665. error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  5666. if (logflags)
  5667. xfs_trans_log_inode(tp, ip, logflags);
  5668. return error;
  5669. }
  5670. /*
  5671. * Splits an extent into two extents at split_fsb block such that it is
  5672. * the first block of the current_ext. @current_ext is a target extent
  5673. * to be split. @split_fsb is a block where the extents is split.
  5674. * If split_fsb lies in a hole or the first block of extents, just return 0.
  5675. */
  5676. STATIC int
  5677. xfs_bmap_split_extent_at(
  5678. struct xfs_trans *tp,
  5679. struct xfs_inode *ip,
  5680. xfs_fileoff_t split_fsb,
  5681. xfs_fsblock_t *firstfsb,
  5682. struct xfs_defer_ops *dfops)
  5683. {
  5684. int whichfork = XFS_DATA_FORK;
  5685. struct xfs_btree_cur *cur = NULL;
  5686. struct xfs_bmbt_rec_host *gotp;
  5687. struct xfs_bmbt_irec got;
  5688. struct xfs_bmbt_irec new; /* split extent */
  5689. struct xfs_mount *mp = ip->i_mount;
  5690. struct xfs_ifork *ifp;
  5691. xfs_fsblock_t gotblkcnt; /* new block count for got */
  5692. xfs_extnum_t current_ext;
  5693. int error = 0;
  5694. int logflags = 0;
  5695. int i = 0;
  5696. if (unlikely(XFS_TEST_ERROR(
  5697. (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
  5698. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
  5699. mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
  5700. XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
  5701. XFS_ERRLEVEL_LOW, mp);
  5702. return -EFSCORRUPTED;
  5703. }
  5704. if (XFS_FORCED_SHUTDOWN(mp))
  5705. return -EIO;
  5706. ifp = XFS_IFORK_PTR(ip, whichfork);
  5707. if (!(ifp->if_flags & XFS_IFEXTENTS)) {
  5708. /* Read in all the extents */
  5709. error = xfs_iread_extents(tp, ip, whichfork);
  5710. if (error)
  5711. return error;
  5712. }
  5713. /*
  5714. * gotp can be null in 2 cases: 1) if there are no extents
  5715. * or 2) split_fsb lies in a hole beyond which there are
  5716. * no extents. Either way, we are done.
  5717. */
  5718. gotp = xfs_iext_bno_to_ext(ifp, split_fsb, &current_ext);
  5719. if (!gotp)
  5720. return 0;
  5721. xfs_bmbt_get_all(gotp, &got);
  5722. /*
  5723. * Check split_fsb lies in a hole or the start boundary offset
  5724. * of the extent.
  5725. */
  5726. if (got.br_startoff >= split_fsb)
  5727. return 0;
  5728. gotblkcnt = split_fsb - got.br_startoff;
  5729. new.br_startoff = split_fsb;
  5730. new.br_startblock = got.br_startblock + gotblkcnt;
  5731. new.br_blockcount = got.br_blockcount - gotblkcnt;
  5732. new.br_state = got.br_state;
  5733. if (ifp->if_flags & XFS_IFBROOT) {
  5734. cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
  5735. cur->bc_private.b.firstblock = *firstfsb;
  5736. cur->bc_private.b.dfops = dfops;
  5737. cur->bc_private.b.flags = 0;
  5738. error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
  5739. got.br_startblock,
  5740. got.br_blockcount,
  5741. &i);
  5742. if (error)
  5743. goto del_cursor;
  5744. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
  5745. }
  5746. xfs_bmbt_set_blockcount(gotp, gotblkcnt);
  5747. got.br_blockcount = gotblkcnt;
  5748. logflags = XFS_ILOG_CORE;
  5749. if (cur) {
  5750. error = xfs_bmbt_update(cur, got.br_startoff,
  5751. got.br_startblock,
  5752. got.br_blockcount,
  5753. got.br_state);
  5754. if (error)
  5755. goto del_cursor;
  5756. } else
  5757. logflags |= XFS_ILOG_DEXT;
  5758. /* Add new extent */
  5759. current_ext++;
  5760. xfs_iext_insert(ip, current_ext, 1, &new, 0);
  5761. XFS_IFORK_NEXT_SET(ip, whichfork,
  5762. XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
  5763. if (cur) {
  5764. error = xfs_bmbt_lookup_eq(cur, new.br_startoff,
  5765. new.br_startblock, new.br_blockcount,
  5766. &i);
  5767. if (error)
  5768. goto del_cursor;
  5769. XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
  5770. cur->bc_rec.b.br_state = new.br_state;
  5771. error = xfs_btree_insert(cur, &i);
  5772. if (error)
  5773. goto del_cursor;
  5774. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
  5775. }
  5776. /*
  5777. * Convert to a btree if necessary.
  5778. */
  5779. if (xfs_bmap_needs_btree(ip, whichfork)) {
  5780. int tmp_logflags; /* partial log flag return val */
  5781. ASSERT(cur == NULL);
  5782. error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops,
  5783. &cur, 0, &tmp_logflags, whichfork);
  5784. logflags |= tmp_logflags;
  5785. }
  5786. del_cursor:
  5787. if (cur) {
  5788. cur->bc_private.b.allocated = 0;
  5789. xfs_btree_del_cursor(cur,
  5790. error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  5791. }
  5792. if (logflags)
  5793. xfs_trans_log_inode(tp, ip, logflags);
  5794. return error;
  5795. }
  5796. int
  5797. xfs_bmap_split_extent(
  5798. struct xfs_inode *ip,
  5799. xfs_fileoff_t split_fsb)
  5800. {
  5801. struct xfs_mount *mp = ip->i_mount;
  5802. struct xfs_trans *tp;
  5803. struct xfs_defer_ops dfops;
  5804. xfs_fsblock_t firstfsb;
  5805. int error;
  5806. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
  5807. XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
  5808. if (error)
  5809. return error;
  5810. xfs_ilock(ip, XFS_ILOCK_EXCL);
  5811. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  5812. xfs_defer_init(&dfops, &firstfsb);
  5813. error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
  5814. &firstfsb, &dfops);
  5815. if (error)
  5816. goto out;
  5817. error = xfs_defer_finish(&tp, &dfops, NULL);
  5818. if (error)
  5819. goto out;
  5820. return xfs_trans_commit(tp);
  5821. out:
  5822. xfs_defer_cancel(&dfops);
  5823. xfs_trans_cancel(tp);
  5824. return error;
  5825. }
  5826. /* Deferred mapping is only for real extents in the data fork. */
  5827. static bool
  5828. xfs_bmap_is_update_needed(
  5829. struct xfs_bmbt_irec *bmap)
  5830. {
  5831. return bmap->br_startblock != HOLESTARTBLOCK &&
  5832. bmap->br_startblock != DELAYSTARTBLOCK;
  5833. }
  5834. /* Record a bmap intent. */
  5835. static int
  5836. __xfs_bmap_add(
  5837. struct xfs_mount *mp,
  5838. struct xfs_defer_ops *dfops,
  5839. enum xfs_bmap_intent_type type,
  5840. struct xfs_inode *ip,
  5841. int whichfork,
  5842. struct xfs_bmbt_irec *bmap)
  5843. {
  5844. int error;
  5845. struct xfs_bmap_intent *bi;
  5846. trace_xfs_bmap_defer(mp,
  5847. XFS_FSB_TO_AGNO(mp, bmap->br_startblock),
  5848. type,
  5849. XFS_FSB_TO_AGBNO(mp, bmap->br_startblock),
  5850. ip->i_ino, whichfork,
  5851. bmap->br_startoff,
  5852. bmap->br_blockcount,
  5853. bmap->br_state);
  5854. bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS);
  5855. INIT_LIST_HEAD(&bi->bi_list);
  5856. bi->bi_type = type;
  5857. bi->bi_owner = ip;
  5858. bi->bi_whichfork = whichfork;
  5859. bi->bi_bmap = *bmap;
  5860. error = xfs_defer_join(dfops, bi->bi_owner);
  5861. if (error) {
  5862. kmem_free(bi);
  5863. return error;
  5864. }
  5865. xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
  5866. return 0;
  5867. }
  5868. /* Map an extent into a file. */
  5869. int
  5870. xfs_bmap_map_extent(
  5871. struct xfs_mount *mp,
  5872. struct xfs_defer_ops *dfops,
  5873. struct xfs_inode *ip,
  5874. struct xfs_bmbt_irec *PREV)
  5875. {
  5876. if (!xfs_bmap_is_update_needed(PREV))
  5877. return 0;
  5878. return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip,
  5879. XFS_DATA_FORK, PREV);
  5880. }
  5881. /* Unmap an extent out of a file. */
  5882. int
  5883. xfs_bmap_unmap_extent(
  5884. struct xfs_mount *mp,
  5885. struct xfs_defer_ops *dfops,
  5886. struct xfs_inode *ip,
  5887. struct xfs_bmbt_irec *PREV)
  5888. {
  5889. if (!xfs_bmap_is_update_needed(PREV))
  5890. return 0;
  5891. return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip,
  5892. XFS_DATA_FORK, PREV);
  5893. }
  5894. /*
  5895. * Process one of the deferred bmap operations. We pass back the
  5896. * btree cursor to maintain our lock on the bmapbt between calls.
  5897. */
  5898. int
  5899. xfs_bmap_finish_one(
  5900. struct xfs_trans *tp,
  5901. struct xfs_defer_ops *dfops,
  5902. struct xfs_inode *ip,
  5903. enum xfs_bmap_intent_type type,
  5904. int whichfork,
  5905. xfs_fileoff_t startoff,
  5906. xfs_fsblock_t startblock,
  5907. xfs_filblks_t blockcount,
  5908. xfs_exntst_t state)
  5909. {
  5910. int error = 0, done;
  5911. trace_xfs_bmap_deferred(tp->t_mountp,
  5912. XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
  5913. XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
  5914. ip->i_ino, whichfork, startoff, blockcount, state);
  5915. if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK))
  5916. return -EFSCORRUPTED;
  5917. if (XFS_TEST_ERROR(false, tp->t_mountp,
  5918. XFS_ERRTAG_BMAP_FINISH_ONE,
  5919. XFS_RANDOM_BMAP_FINISH_ONE))
  5920. return -EIO;
  5921. switch (type) {
  5922. case XFS_BMAP_MAP:
  5923. error = xfs_bmapi_remap(tp, ip, startoff, blockcount,
  5924. startblock, dfops);
  5925. break;
  5926. case XFS_BMAP_UNMAP:
  5927. error = xfs_bunmapi(tp, ip, startoff, blockcount,
  5928. XFS_BMAPI_REMAP, 1, &startblock, dfops, &done);
  5929. ASSERT(done);
  5930. break;
  5931. default:
  5932. ASSERT(0);
  5933. error = -EFSCORRUPTED;
  5934. }
  5935. return error;
  5936. }