xfs_bmap.c 163 KB

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