xfs_bmap.c 180 KB

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