inode.c 262 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993
  1. /*
  2. * Copyright (C) 2007 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/bio.h>
  20. #include <linux/buffer_head.h>
  21. #include <linux/file.h>
  22. #include <linux/fs.h>
  23. #include <linux/pagemap.h>
  24. #include <linux/highmem.h>
  25. #include <linux/time.h>
  26. #include <linux/init.h>
  27. #include <linux/string.h>
  28. #include <linux/backing-dev.h>
  29. #include <linux/mpage.h>
  30. #include <linux/swap.h>
  31. #include <linux/writeback.h>
  32. #include <linux/statfs.h>
  33. #include <linux/compat.h>
  34. #include <linux/bit_spinlock.h>
  35. #include <linux/xattr.h>
  36. #include <linux/posix_acl.h>
  37. #include <linux/falloc.h>
  38. #include <linux/slab.h>
  39. #include <linux/ratelimit.h>
  40. #include <linux/mount.h>
  41. #include <linux/btrfs.h>
  42. #include <linux/blkdev.h>
  43. #include <linux/posix_acl_xattr.h>
  44. #include <linux/uio.h>
  45. #include "ctree.h"
  46. #include "disk-io.h"
  47. #include "transaction.h"
  48. #include "btrfs_inode.h"
  49. #include "print-tree.h"
  50. #include "ordered-data.h"
  51. #include "xattr.h"
  52. #include "tree-log.h"
  53. #include "volumes.h"
  54. #include "compression.h"
  55. #include "locking.h"
  56. #include "free-space-cache.h"
  57. #include "inode-map.h"
  58. #include "backref.h"
  59. #include "hash.h"
  60. #include "props.h"
  61. #include "qgroup.h"
  62. struct btrfs_iget_args {
  63. struct btrfs_key *location;
  64. struct btrfs_root *root;
  65. };
  66. static const struct inode_operations btrfs_dir_inode_operations;
  67. static const struct inode_operations btrfs_symlink_inode_operations;
  68. static const struct inode_operations btrfs_dir_ro_inode_operations;
  69. static const struct inode_operations btrfs_special_inode_operations;
  70. static const struct inode_operations btrfs_file_inode_operations;
  71. static const struct address_space_operations btrfs_aops;
  72. static const struct address_space_operations btrfs_symlink_aops;
  73. static const struct file_operations btrfs_dir_file_operations;
  74. static struct extent_io_ops btrfs_extent_io_ops;
  75. static struct kmem_cache *btrfs_inode_cachep;
  76. static struct kmem_cache *btrfs_delalloc_work_cachep;
  77. struct kmem_cache *btrfs_trans_handle_cachep;
  78. struct kmem_cache *btrfs_transaction_cachep;
  79. struct kmem_cache *btrfs_path_cachep;
  80. struct kmem_cache *btrfs_free_space_cachep;
  81. #define S_SHIFT 12
  82. static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
  83. [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
  84. [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
  85. [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
  86. [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
  87. [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
  88. [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
  89. [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
  90. };
  91. static int btrfs_setsize(struct inode *inode, struct iattr *attr);
  92. static int btrfs_truncate(struct inode *inode);
  93. static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
  94. static noinline int cow_file_range(struct inode *inode,
  95. struct page *locked_page,
  96. u64 start, u64 end, int *page_started,
  97. unsigned long *nr_written, int unlock);
  98. static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
  99. u64 len, u64 orig_start,
  100. u64 block_start, u64 block_len,
  101. u64 orig_block_len, u64 ram_bytes,
  102. int type);
  103. static int btrfs_dirty_inode(struct inode *inode);
  104. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  105. void btrfs_test_inode_set_ops(struct inode *inode)
  106. {
  107. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  108. }
  109. #endif
  110. static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
  111. struct inode *inode, struct inode *dir,
  112. const struct qstr *qstr)
  113. {
  114. int err;
  115. err = btrfs_init_acl(trans, inode, dir);
  116. if (!err)
  117. err = btrfs_xattr_security_init(trans, inode, dir, qstr);
  118. return err;
  119. }
  120. /*
  121. * this does all the hard work for inserting an inline extent into
  122. * the btree. The caller should have done a btrfs_drop_extents so that
  123. * no overlapping inline items exist in the btree
  124. */
  125. static int insert_inline_extent(struct btrfs_trans_handle *trans,
  126. struct btrfs_path *path, int extent_inserted,
  127. struct btrfs_root *root, struct inode *inode,
  128. u64 start, size_t size, size_t compressed_size,
  129. int compress_type,
  130. struct page **compressed_pages)
  131. {
  132. struct extent_buffer *leaf;
  133. struct page *page = NULL;
  134. char *kaddr;
  135. unsigned long ptr;
  136. struct btrfs_file_extent_item *ei;
  137. int err = 0;
  138. int ret;
  139. size_t cur_size = size;
  140. unsigned long offset;
  141. if (compressed_size && compressed_pages)
  142. cur_size = compressed_size;
  143. inode_add_bytes(inode, size);
  144. if (!extent_inserted) {
  145. struct btrfs_key key;
  146. size_t datasize;
  147. key.objectid = btrfs_ino(inode);
  148. key.offset = start;
  149. key.type = BTRFS_EXTENT_DATA_KEY;
  150. datasize = btrfs_file_extent_calc_inline_size(cur_size);
  151. path->leave_spinning = 1;
  152. ret = btrfs_insert_empty_item(trans, root, path, &key,
  153. datasize);
  154. if (ret) {
  155. err = ret;
  156. goto fail;
  157. }
  158. }
  159. leaf = path->nodes[0];
  160. ei = btrfs_item_ptr(leaf, path->slots[0],
  161. struct btrfs_file_extent_item);
  162. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  163. btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
  164. btrfs_set_file_extent_encryption(leaf, ei, 0);
  165. btrfs_set_file_extent_other_encoding(leaf, ei, 0);
  166. btrfs_set_file_extent_ram_bytes(leaf, ei, size);
  167. ptr = btrfs_file_extent_inline_start(ei);
  168. if (compress_type != BTRFS_COMPRESS_NONE) {
  169. struct page *cpage;
  170. int i = 0;
  171. while (compressed_size > 0) {
  172. cpage = compressed_pages[i];
  173. cur_size = min_t(unsigned long, compressed_size,
  174. PAGE_CACHE_SIZE);
  175. kaddr = kmap_atomic(cpage);
  176. write_extent_buffer(leaf, kaddr, ptr, cur_size);
  177. kunmap_atomic(kaddr);
  178. i++;
  179. ptr += cur_size;
  180. compressed_size -= cur_size;
  181. }
  182. btrfs_set_file_extent_compression(leaf, ei,
  183. compress_type);
  184. } else {
  185. page = find_get_page(inode->i_mapping,
  186. start >> PAGE_CACHE_SHIFT);
  187. btrfs_set_file_extent_compression(leaf, ei, 0);
  188. kaddr = kmap_atomic(page);
  189. offset = start & (PAGE_CACHE_SIZE - 1);
  190. write_extent_buffer(leaf, kaddr + offset, ptr, size);
  191. kunmap_atomic(kaddr);
  192. page_cache_release(page);
  193. }
  194. btrfs_mark_buffer_dirty(leaf);
  195. btrfs_release_path(path);
  196. /*
  197. * we're an inline extent, so nobody can
  198. * extend the file past i_size without locking
  199. * a page we already have locked.
  200. *
  201. * We must do any isize and inode updates
  202. * before we unlock the pages. Otherwise we
  203. * could end up racing with unlink.
  204. */
  205. BTRFS_I(inode)->disk_i_size = inode->i_size;
  206. ret = btrfs_update_inode(trans, root, inode);
  207. return ret;
  208. fail:
  209. return err;
  210. }
  211. /*
  212. * conditionally insert an inline extent into the file. This
  213. * does the checks required to make sure the data is small enough
  214. * to fit as an inline extent.
  215. */
  216. static noinline int cow_file_range_inline(struct btrfs_root *root,
  217. struct inode *inode, u64 start,
  218. u64 end, size_t compressed_size,
  219. int compress_type,
  220. struct page **compressed_pages)
  221. {
  222. struct btrfs_trans_handle *trans;
  223. u64 isize = i_size_read(inode);
  224. u64 actual_end = min(end + 1, isize);
  225. u64 inline_len = actual_end - start;
  226. u64 aligned_end = ALIGN(end, root->sectorsize);
  227. u64 data_len = inline_len;
  228. int ret;
  229. struct btrfs_path *path;
  230. int extent_inserted = 0;
  231. u32 extent_item_size;
  232. if (compressed_size)
  233. data_len = compressed_size;
  234. if (start > 0 ||
  235. actual_end > PAGE_CACHE_SIZE ||
  236. data_len > BTRFS_MAX_INLINE_DATA_SIZE(root) ||
  237. (!compressed_size &&
  238. (actual_end & (root->sectorsize - 1)) == 0) ||
  239. end + 1 < isize ||
  240. data_len > root->fs_info->max_inline) {
  241. return 1;
  242. }
  243. path = btrfs_alloc_path();
  244. if (!path)
  245. return -ENOMEM;
  246. trans = btrfs_join_transaction(root);
  247. if (IS_ERR(trans)) {
  248. btrfs_free_path(path);
  249. return PTR_ERR(trans);
  250. }
  251. trans->block_rsv = &root->fs_info->delalloc_block_rsv;
  252. if (compressed_size && compressed_pages)
  253. extent_item_size = btrfs_file_extent_calc_inline_size(
  254. compressed_size);
  255. else
  256. extent_item_size = btrfs_file_extent_calc_inline_size(
  257. inline_len);
  258. ret = __btrfs_drop_extents(trans, root, inode, path,
  259. start, aligned_end, NULL,
  260. 1, 1, extent_item_size, &extent_inserted);
  261. if (ret) {
  262. btrfs_abort_transaction(trans, root, ret);
  263. goto out;
  264. }
  265. if (isize > actual_end)
  266. inline_len = min_t(u64, isize, actual_end);
  267. ret = insert_inline_extent(trans, path, extent_inserted,
  268. root, inode, start,
  269. inline_len, compressed_size,
  270. compress_type, compressed_pages);
  271. if (ret && ret != -ENOSPC) {
  272. btrfs_abort_transaction(trans, root, ret);
  273. goto out;
  274. } else if (ret == -ENOSPC) {
  275. ret = 1;
  276. goto out;
  277. }
  278. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
  279. btrfs_delalloc_release_metadata(inode, end + 1 - start);
  280. btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
  281. out:
  282. btrfs_free_path(path);
  283. btrfs_end_transaction(trans, root);
  284. return ret;
  285. }
  286. struct async_extent {
  287. u64 start;
  288. u64 ram_size;
  289. u64 compressed_size;
  290. struct page **pages;
  291. unsigned long nr_pages;
  292. int compress_type;
  293. struct list_head list;
  294. };
  295. struct async_cow {
  296. struct inode *inode;
  297. struct btrfs_root *root;
  298. struct page *locked_page;
  299. u64 start;
  300. u64 end;
  301. struct list_head extents;
  302. struct btrfs_work work;
  303. };
  304. static noinline int add_async_extent(struct async_cow *cow,
  305. u64 start, u64 ram_size,
  306. u64 compressed_size,
  307. struct page **pages,
  308. unsigned long nr_pages,
  309. int compress_type)
  310. {
  311. struct async_extent *async_extent;
  312. async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
  313. BUG_ON(!async_extent); /* -ENOMEM */
  314. async_extent->start = start;
  315. async_extent->ram_size = ram_size;
  316. async_extent->compressed_size = compressed_size;
  317. async_extent->pages = pages;
  318. async_extent->nr_pages = nr_pages;
  319. async_extent->compress_type = compress_type;
  320. list_add_tail(&async_extent->list, &cow->extents);
  321. return 0;
  322. }
  323. static inline int inode_need_compress(struct inode *inode)
  324. {
  325. struct btrfs_root *root = BTRFS_I(inode)->root;
  326. /* force compress */
  327. if (btrfs_test_opt(root, FORCE_COMPRESS))
  328. return 1;
  329. /* bad compression ratios */
  330. if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
  331. return 0;
  332. if (btrfs_test_opt(root, COMPRESS) ||
  333. BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
  334. BTRFS_I(inode)->force_compress)
  335. return 1;
  336. return 0;
  337. }
  338. /*
  339. * we create compressed extents in two phases. The first
  340. * phase compresses a range of pages that have already been
  341. * locked (both pages and state bits are locked).
  342. *
  343. * This is done inside an ordered work queue, and the compression
  344. * is spread across many cpus. The actual IO submission is step
  345. * two, and the ordered work queue takes care of making sure that
  346. * happens in the same order things were put onto the queue by
  347. * writepages and friends.
  348. *
  349. * If this code finds it can't get good compression, it puts an
  350. * entry onto the work queue to write the uncompressed bytes. This
  351. * makes sure that both compressed inodes and uncompressed inodes
  352. * are written in the same order that the flusher thread sent them
  353. * down.
  354. */
  355. static noinline void compress_file_range(struct inode *inode,
  356. struct page *locked_page,
  357. u64 start, u64 end,
  358. struct async_cow *async_cow,
  359. int *num_added)
  360. {
  361. struct btrfs_root *root = BTRFS_I(inode)->root;
  362. u64 num_bytes;
  363. u64 blocksize = root->sectorsize;
  364. u64 actual_end;
  365. u64 isize = i_size_read(inode);
  366. int ret = 0;
  367. struct page **pages = NULL;
  368. unsigned long nr_pages;
  369. unsigned long nr_pages_ret = 0;
  370. unsigned long total_compressed = 0;
  371. unsigned long total_in = 0;
  372. unsigned long max_compressed = 128 * 1024;
  373. unsigned long max_uncompressed = 128 * 1024;
  374. int i;
  375. int will_compress;
  376. int compress_type = root->fs_info->compress_type;
  377. int redirty = 0;
  378. /* if this is a small write inside eof, kick off a defrag */
  379. if ((end - start + 1) < 16 * 1024 &&
  380. (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
  381. btrfs_add_inode_defrag(NULL, inode);
  382. actual_end = min_t(u64, isize, end + 1);
  383. again:
  384. will_compress = 0;
  385. nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
  386. nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
  387. /*
  388. * we don't want to send crud past the end of i_size through
  389. * compression, that's just a waste of CPU time. So, if the
  390. * end of the file is before the start of our current
  391. * requested range of bytes, we bail out to the uncompressed
  392. * cleanup code that can deal with all of this.
  393. *
  394. * It isn't really the fastest way to fix things, but this is a
  395. * very uncommon corner.
  396. */
  397. if (actual_end <= start)
  398. goto cleanup_and_bail_uncompressed;
  399. total_compressed = actual_end - start;
  400. /*
  401. * skip compression for a small file range(<=blocksize) that
  402. * isn't an inline extent, since it dosen't save disk space at all.
  403. */
  404. if (total_compressed <= blocksize &&
  405. (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
  406. goto cleanup_and_bail_uncompressed;
  407. /* we want to make sure that amount of ram required to uncompress
  408. * an extent is reasonable, so we limit the total size in ram
  409. * of a compressed extent to 128k. This is a crucial number
  410. * because it also controls how easily we can spread reads across
  411. * cpus for decompression.
  412. *
  413. * We also want to make sure the amount of IO required to do
  414. * a random read is reasonably small, so we limit the size of
  415. * a compressed extent to 128k.
  416. */
  417. total_compressed = min(total_compressed, max_uncompressed);
  418. num_bytes = ALIGN(end - start + 1, blocksize);
  419. num_bytes = max(blocksize, num_bytes);
  420. total_in = 0;
  421. ret = 0;
  422. /*
  423. * we do compression for mount -o compress and when the
  424. * inode has not been flagged as nocompress. This flag can
  425. * change at any time if we discover bad compression ratios.
  426. */
  427. if (inode_need_compress(inode)) {
  428. WARN_ON(pages);
  429. pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
  430. if (!pages) {
  431. /* just bail out to the uncompressed code */
  432. goto cont;
  433. }
  434. if (BTRFS_I(inode)->force_compress)
  435. compress_type = BTRFS_I(inode)->force_compress;
  436. /*
  437. * we need to call clear_page_dirty_for_io on each
  438. * page in the range. Otherwise applications with the file
  439. * mmap'd can wander in and change the page contents while
  440. * we are compressing them.
  441. *
  442. * If the compression fails for any reason, we set the pages
  443. * dirty again later on.
  444. */
  445. extent_range_clear_dirty_for_io(inode, start, end);
  446. redirty = 1;
  447. ret = btrfs_compress_pages(compress_type,
  448. inode->i_mapping, start,
  449. total_compressed, pages,
  450. nr_pages, &nr_pages_ret,
  451. &total_in,
  452. &total_compressed,
  453. max_compressed);
  454. if (!ret) {
  455. unsigned long offset = total_compressed &
  456. (PAGE_CACHE_SIZE - 1);
  457. struct page *page = pages[nr_pages_ret - 1];
  458. char *kaddr;
  459. /* zero the tail end of the last page, we might be
  460. * sending it down to disk
  461. */
  462. if (offset) {
  463. kaddr = kmap_atomic(page);
  464. memset(kaddr + offset, 0,
  465. PAGE_CACHE_SIZE - offset);
  466. kunmap_atomic(kaddr);
  467. }
  468. will_compress = 1;
  469. }
  470. }
  471. cont:
  472. if (start == 0) {
  473. /* lets try to make an inline extent */
  474. if (ret || total_in < (actual_end - start)) {
  475. /* we didn't compress the entire range, try
  476. * to make an uncompressed inline extent.
  477. */
  478. ret = cow_file_range_inline(root, inode, start, end,
  479. 0, 0, NULL);
  480. } else {
  481. /* try making a compressed inline extent */
  482. ret = cow_file_range_inline(root, inode, start, end,
  483. total_compressed,
  484. compress_type, pages);
  485. }
  486. if (ret <= 0) {
  487. unsigned long clear_flags = EXTENT_DELALLOC |
  488. EXTENT_DEFRAG;
  489. unsigned long page_error_op;
  490. clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
  491. page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
  492. /*
  493. * inline extent creation worked or returned error,
  494. * we don't need to create any more async work items.
  495. * Unlock and free up our temp pages.
  496. */
  497. extent_clear_unlock_delalloc(inode, start, end, NULL,
  498. clear_flags, PAGE_UNLOCK |
  499. PAGE_CLEAR_DIRTY |
  500. PAGE_SET_WRITEBACK |
  501. page_error_op |
  502. PAGE_END_WRITEBACK);
  503. goto free_pages_out;
  504. }
  505. }
  506. if (will_compress) {
  507. /*
  508. * we aren't doing an inline extent round the compressed size
  509. * up to a block size boundary so the allocator does sane
  510. * things
  511. */
  512. total_compressed = ALIGN(total_compressed, blocksize);
  513. /*
  514. * one last check to make sure the compression is really a
  515. * win, compare the page count read with the blocks on disk
  516. */
  517. total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
  518. if (total_compressed >= total_in) {
  519. will_compress = 0;
  520. } else {
  521. num_bytes = total_in;
  522. }
  523. }
  524. if (!will_compress && pages) {
  525. /*
  526. * the compression code ran but failed to make things smaller,
  527. * free any pages it allocated and our page pointer array
  528. */
  529. for (i = 0; i < nr_pages_ret; i++) {
  530. WARN_ON(pages[i]->mapping);
  531. page_cache_release(pages[i]);
  532. }
  533. kfree(pages);
  534. pages = NULL;
  535. total_compressed = 0;
  536. nr_pages_ret = 0;
  537. /* flag the file so we don't compress in the future */
  538. if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
  539. !(BTRFS_I(inode)->force_compress)) {
  540. BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
  541. }
  542. }
  543. if (will_compress) {
  544. *num_added += 1;
  545. /* the async work queues will take care of doing actual
  546. * allocation on disk for these compressed pages,
  547. * and will submit them to the elevator.
  548. */
  549. add_async_extent(async_cow, start, num_bytes,
  550. total_compressed, pages, nr_pages_ret,
  551. compress_type);
  552. if (start + num_bytes < end) {
  553. start += num_bytes;
  554. pages = NULL;
  555. cond_resched();
  556. goto again;
  557. }
  558. } else {
  559. cleanup_and_bail_uncompressed:
  560. /*
  561. * No compression, but we still need to write the pages in
  562. * the file we've been given so far. redirty the locked
  563. * page if it corresponds to our extent and set things up
  564. * for the async work queue to run cow_file_range to do
  565. * the normal delalloc dance
  566. */
  567. if (page_offset(locked_page) >= start &&
  568. page_offset(locked_page) <= end) {
  569. __set_page_dirty_nobuffers(locked_page);
  570. /* unlocked later on in the async handlers */
  571. }
  572. if (redirty)
  573. extent_range_redirty_for_io(inode, start, end);
  574. add_async_extent(async_cow, start, end - start + 1,
  575. 0, NULL, 0, BTRFS_COMPRESS_NONE);
  576. *num_added += 1;
  577. }
  578. return;
  579. free_pages_out:
  580. for (i = 0; i < nr_pages_ret; i++) {
  581. WARN_ON(pages[i]->mapping);
  582. page_cache_release(pages[i]);
  583. }
  584. kfree(pages);
  585. }
  586. static void free_async_extent_pages(struct async_extent *async_extent)
  587. {
  588. int i;
  589. if (!async_extent->pages)
  590. return;
  591. for (i = 0; i < async_extent->nr_pages; i++) {
  592. WARN_ON(async_extent->pages[i]->mapping);
  593. page_cache_release(async_extent->pages[i]);
  594. }
  595. kfree(async_extent->pages);
  596. async_extent->nr_pages = 0;
  597. async_extent->pages = NULL;
  598. }
  599. /*
  600. * phase two of compressed writeback. This is the ordered portion
  601. * of the code, which only gets called in the order the work was
  602. * queued. We walk all the async extents created by compress_file_range
  603. * and send them down to the disk.
  604. */
  605. static noinline void submit_compressed_extents(struct inode *inode,
  606. struct async_cow *async_cow)
  607. {
  608. struct async_extent *async_extent;
  609. u64 alloc_hint = 0;
  610. struct btrfs_key ins;
  611. struct extent_map *em;
  612. struct btrfs_root *root = BTRFS_I(inode)->root;
  613. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  614. struct extent_io_tree *io_tree;
  615. int ret = 0;
  616. again:
  617. while (!list_empty(&async_cow->extents)) {
  618. async_extent = list_entry(async_cow->extents.next,
  619. struct async_extent, list);
  620. list_del(&async_extent->list);
  621. io_tree = &BTRFS_I(inode)->io_tree;
  622. retry:
  623. /* did the compression code fall back to uncompressed IO? */
  624. if (!async_extent->pages) {
  625. int page_started = 0;
  626. unsigned long nr_written = 0;
  627. lock_extent(io_tree, async_extent->start,
  628. async_extent->start +
  629. async_extent->ram_size - 1);
  630. /* allocate blocks */
  631. ret = cow_file_range(inode, async_cow->locked_page,
  632. async_extent->start,
  633. async_extent->start +
  634. async_extent->ram_size - 1,
  635. &page_started, &nr_written, 0);
  636. /* JDM XXX */
  637. /*
  638. * if page_started, cow_file_range inserted an
  639. * inline extent and took care of all the unlocking
  640. * and IO for us. Otherwise, we need to submit
  641. * all those pages down to the drive.
  642. */
  643. if (!page_started && !ret)
  644. extent_write_locked_range(io_tree,
  645. inode, async_extent->start,
  646. async_extent->start +
  647. async_extent->ram_size - 1,
  648. btrfs_get_extent,
  649. WB_SYNC_ALL);
  650. else if (ret)
  651. unlock_page(async_cow->locked_page);
  652. kfree(async_extent);
  653. cond_resched();
  654. continue;
  655. }
  656. lock_extent(io_tree, async_extent->start,
  657. async_extent->start + async_extent->ram_size - 1);
  658. ret = btrfs_reserve_extent(root,
  659. async_extent->compressed_size,
  660. async_extent->compressed_size,
  661. 0, alloc_hint, &ins, 1, 1);
  662. if (ret) {
  663. free_async_extent_pages(async_extent);
  664. if (ret == -ENOSPC) {
  665. unlock_extent(io_tree, async_extent->start,
  666. async_extent->start +
  667. async_extent->ram_size - 1);
  668. /*
  669. * we need to redirty the pages if we decide to
  670. * fallback to uncompressed IO, otherwise we
  671. * will not submit these pages down to lower
  672. * layers.
  673. */
  674. extent_range_redirty_for_io(inode,
  675. async_extent->start,
  676. async_extent->start +
  677. async_extent->ram_size - 1);
  678. goto retry;
  679. }
  680. goto out_free;
  681. }
  682. /*
  683. * here we're doing allocation and writeback of the
  684. * compressed pages
  685. */
  686. btrfs_drop_extent_cache(inode, async_extent->start,
  687. async_extent->start +
  688. async_extent->ram_size - 1, 0);
  689. em = alloc_extent_map();
  690. if (!em) {
  691. ret = -ENOMEM;
  692. goto out_free_reserve;
  693. }
  694. em->start = async_extent->start;
  695. em->len = async_extent->ram_size;
  696. em->orig_start = em->start;
  697. em->mod_start = em->start;
  698. em->mod_len = em->len;
  699. em->block_start = ins.objectid;
  700. em->block_len = ins.offset;
  701. em->orig_block_len = ins.offset;
  702. em->ram_bytes = async_extent->ram_size;
  703. em->bdev = root->fs_info->fs_devices->latest_bdev;
  704. em->compress_type = async_extent->compress_type;
  705. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  706. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  707. em->generation = -1;
  708. while (1) {
  709. write_lock(&em_tree->lock);
  710. ret = add_extent_mapping(em_tree, em, 1);
  711. write_unlock(&em_tree->lock);
  712. if (ret != -EEXIST) {
  713. free_extent_map(em);
  714. break;
  715. }
  716. btrfs_drop_extent_cache(inode, async_extent->start,
  717. async_extent->start +
  718. async_extent->ram_size - 1, 0);
  719. }
  720. if (ret)
  721. goto out_free_reserve;
  722. ret = btrfs_add_ordered_extent_compress(inode,
  723. async_extent->start,
  724. ins.objectid,
  725. async_extent->ram_size,
  726. ins.offset,
  727. BTRFS_ORDERED_COMPRESSED,
  728. async_extent->compress_type);
  729. if (ret) {
  730. btrfs_drop_extent_cache(inode, async_extent->start,
  731. async_extent->start +
  732. async_extent->ram_size - 1, 0);
  733. goto out_free_reserve;
  734. }
  735. /*
  736. * clear dirty, set writeback and unlock the pages.
  737. */
  738. extent_clear_unlock_delalloc(inode, async_extent->start,
  739. async_extent->start +
  740. async_extent->ram_size - 1,
  741. NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
  742. PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
  743. PAGE_SET_WRITEBACK);
  744. ret = btrfs_submit_compressed_write(inode,
  745. async_extent->start,
  746. async_extent->ram_size,
  747. ins.objectid,
  748. ins.offset, async_extent->pages,
  749. async_extent->nr_pages);
  750. if (ret) {
  751. struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
  752. struct page *p = async_extent->pages[0];
  753. const u64 start = async_extent->start;
  754. const u64 end = start + async_extent->ram_size - 1;
  755. p->mapping = inode->i_mapping;
  756. tree->ops->writepage_end_io_hook(p, start, end,
  757. NULL, 0);
  758. p->mapping = NULL;
  759. extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
  760. PAGE_END_WRITEBACK |
  761. PAGE_SET_ERROR);
  762. free_async_extent_pages(async_extent);
  763. }
  764. alloc_hint = ins.objectid + ins.offset;
  765. kfree(async_extent);
  766. cond_resched();
  767. }
  768. return;
  769. out_free_reserve:
  770. btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
  771. out_free:
  772. extent_clear_unlock_delalloc(inode, async_extent->start,
  773. async_extent->start +
  774. async_extent->ram_size - 1,
  775. NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
  776. EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
  777. PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
  778. PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
  779. PAGE_SET_ERROR);
  780. free_async_extent_pages(async_extent);
  781. kfree(async_extent);
  782. goto again;
  783. }
  784. static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
  785. u64 num_bytes)
  786. {
  787. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  788. struct extent_map *em;
  789. u64 alloc_hint = 0;
  790. read_lock(&em_tree->lock);
  791. em = search_extent_mapping(em_tree, start, num_bytes);
  792. if (em) {
  793. /*
  794. * if block start isn't an actual block number then find the
  795. * first block in this inode and use that as a hint. If that
  796. * block is also bogus then just don't worry about it.
  797. */
  798. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  799. free_extent_map(em);
  800. em = search_extent_mapping(em_tree, 0, 0);
  801. if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
  802. alloc_hint = em->block_start;
  803. if (em)
  804. free_extent_map(em);
  805. } else {
  806. alloc_hint = em->block_start;
  807. free_extent_map(em);
  808. }
  809. }
  810. read_unlock(&em_tree->lock);
  811. return alloc_hint;
  812. }
  813. /*
  814. * when extent_io.c finds a delayed allocation range in the file,
  815. * the call backs end up in this code. The basic idea is to
  816. * allocate extents on disk for the range, and create ordered data structs
  817. * in ram to track those extents.
  818. *
  819. * locked_page is the page that writepage had locked already. We use
  820. * it to make sure we don't do extra locks or unlocks.
  821. *
  822. * *page_started is set to one if we unlock locked_page and do everything
  823. * required to start IO on it. It may be clean and already done with
  824. * IO when we return.
  825. */
  826. static noinline int cow_file_range(struct inode *inode,
  827. struct page *locked_page,
  828. u64 start, u64 end, int *page_started,
  829. unsigned long *nr_written,
  830. int unlock)
  831. {
  832. struct btrfs_root *root = BTRFS_I(inode)->root;
  833. u64 alloc_hint = 0;
  834. u64 num_bytes;
  835. unsigned long ram_size;
  836. u64 disk_num_bytes;
  837. u64 cur_alloc_size;
  838. u64 blocksize = root->sectorsize;
  839. struct btrfs_key ins;
  840. struct extent_map *em;
  841. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  842. int ret = 0;
  843. if (btrfs_is_free_space_inode(inode)) {
  844. WARN_ON_ONCE(1);
  845. ret = -EINVAL;
  846. goto out_unlock;
  847. }
  848. num_bytes = ALIGN(end - start + 1, blocksize);
  849. num_bytes = max(blocksize, num_bytes);
  850. disk_num_bytes = num_bytes;
  851. /* if this is a small write inside eof, kick off defrag */
  852. if (num_bytes < 64 * 1024 &&
  853. (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
  854. btrfs_add_inode_defrag(NULL, inode);
  855. if (start == 0) {
  856. /* lets try to make an inline extent */
  857. ret = cow_file_range_inline(root, inode, start, end, 0, 0,
  858. NULL);
  859. if (ret == 0) {
  860. extent_clear_unlock_delalloc(inode, start, end, NULL,
  861. EXTENT_LOCKED | EXTENT_DELALLOC |
  862. EXTENT_DEFRAG, PAGE_UNLOCK |
  863. PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
  864. PAGE_END_WRITEBACK);
  865. *nr_written = *nr_written +
  866. (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
  867. *page_started = 1;
  868. goto out;
  869. } else if (ret < 0) {
  870. goto out_unlock;
  871. }
  872. }
  873. BUG_ON(disk_num_bytes >
  874. btrfs_super_total_bytes(root->fs_info->super_copy));
  875. alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
  876. btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
  877. while (disk_num_bytes > 0) {
  878. unsigned long op;
  879. cur_alloc_size = disk_num_bytes;
  880. ret = btrfs_reserve_extent(root, cur_alloc_size,
  881. root->sectorsize, 0, alloc_hint,
  882. &ins, 1, 1);
  883. if (ret < 0)
  884. goto out_unlock;
  885. em = alloc_extent_map();
  886. if (!em) {
  887. ret = -ENOMEM;
  888. goto out_reserve;
  889. }
  890. em->start = start;
  891. em->orig_start = em->start;
  892. ram_size = ins.offset;
  893. em->len = ins.offset;
  894. em->mod_start = em->start;
  895. em->mod_len = em->len;
  896. em->block_start = ins.objectid;
  897. em->block_len = ins.offset;
  898. em->orig_block_len = ins.offset;
  899. em->ram_bytes = ram_size;
  900. em->bdev = root->fs_info->fs_devices->latest_bdev;
  901. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  902. em->generation = -1;
  903. while (1) {
  904. write_lock(&em_tree->lock);
  905. ret = add_extent_mapping(em_tree, em, 1);
  906. write_unlock(&em_tree->lock);
  907. if (ret != -EEXIST) {
  908. free_extent_map(em);
  909. break;
  910. }
  911. btrfs_drop_extent_cache(inode, start,
  912. start + ram_size - 1, 0);
  913. }
  914. if (ret)
  915. goto out_reserve;
  916. cur_alloc_size = ins.offset;
  917. ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
  918. ram_size, cur_alloc_size, 0);
  919. if (ret)
  920. goto out_drop_extent_cache;
  921. if (root->root_key.objectid ==
  922. BTRFS_DATA_RELOC_TREE_OBJECTID) {
  923. ret = btrfs_reloc_clone_csums(inode, start,
  924. cur_alloc_size);
  925. if (ret)
  926. goto out_drop_extent_cache;
  927. }
  928. if (disk_num_bytes < cur_alloc_size)
  929. break;
  930. /* we're not doing compressed IO, don't unlock the first
  931. * page (which the caller expects to stay locked), don't
  932. * clear any dirty bits and don't set any writeback bits
  933. *
  934. * Do set the Private2 bit so we know this page was properly
  935. * setup for writepage
  936. */
  937. op = unlock ? PAGE_UNLOCK : 0;
  938. op |= PAGE_SET_PRIVATE2;
  939. extent_clear_unlock_delalloc(inode, start,
  940. start + ram_size - 1, locked_page,
  941. EXTENT_LOCKED | EXTENT_DELALLOC,
  942. op);
  943. disk_num_bytes -= cur_alloc_size;
  944. num_bytes -= cur_alloc_size;
  945. alloc_hint = ins.objectid + ins.offset;
  946. start += cur_alloc_size;
  947. }
  948. out:
  949. return ret;
  950. out_drop_extent_cache:
  951. btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
  952. out_reserve:
  953. btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
  954. out_unlock:
  955. extent_clear_unlock_delalloc(inode, start, end, locked_page,
  956. EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
  957. EXTENT_DELALLOC | EXTENT_DEFRAG,
  958. PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
  959. PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
  960. goto out;
  961. }
  962. /*
  963. * work queue call back to started compression on a file and pages
  964. */
  965. static noinline void async_cow_start(struct btrfs_work *work)
  966. {
  967. struct async_cow *async_cow;
  968. int num_added = 0;
  969. async_cow = container_of(work, struct async_cow, work);
  970. compress_file_range(async_cow->inode, async_cow->locked_page,
  971. async_cow->start, async_cow->end, async_cow,
  972. &num_added);
  973. if (num_added == 0) {
  974. btrfs_add_delayed_iput(async_cow->inode);
  975. async_cow->inode = NULL;
  976. }
  977. }
  978. /*
  979. * work queue call back to submit previously compressed pages
  980. */
  981. static noinline void async_cow_submit(struct btrfs_work *work)
  982. {
  983. struct async_cow *async_cow;
  984. struct btrfs_root *root;
  985. unsigned long nr_pages;
  986. async_cow = container_of(work, struct async_cow, work);
  987. root = async_cow->root;
  988. nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
  989. PAGE_CACHE_SHIFT;
  990. if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
  991. 5 * 1024 * 1024 &&
  992. waitqueue_active(&root->fs_info->async_submit_wait))
  993. wake_up(&root->fs_info->async_submit_wait);
  994. if (async_cow->inode)
  995. submit_compressed_extents(async_cow->inode, async_cow);
  996. }
  997. static noinline void async_cow_free(struct btrfs_work *work)
  998. {
  999. struct async_cow *async_cow;
  1000. async_cow = container_of(work, struct async_cow, work);
  1001. if (async_cow->inode)
  1002. btrfs_add_delayed_iput(async_cow->inode);
  1003. kfree(async_cow);
  1004. }
  1005. static int cow_file_range_async(struct inode *inode, struct page *locked_page,
  1006. u64 start, u64 end, int *page_started,
  1007. unsigned long *nr_written)
  1008. {
  1009. struct async_cow *async_cow;
  1010. struct btrfs_root *root = BTRFS_I(inode)->root;
  1011. unsigned long nr_pages;
  1012. u64 cur_end;
  1013. int limit = 10 * 1024 * 1024;
  1014. clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
  1015. 1, 0, NULL, GFP_NOFS);
  1016. while (start < end) {
  1017. async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
  1018. BUG_ON(!async_cow); /* -ENOMEM */
  1019. async_cow->inode = igrab(inode);
  1020. async_cow->root = root;
  1021. async_cow->locked_page = locked_page;
  1022. async_cow->start = start;
  1023. if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
  1024. !btrfs_test_opt(root, FORCE_COMPRESS))
  1025. cur_end = end;
  1026. else
  1027. cur_end = min(end, start + 512 * 1024 - 1);
  1028. async_cow->end = cur_end;
  1029. INIT_LIST_HEAD(&async_cow->extents);
  1030. btrfs_init_work(&async_cow->work,
  1031. btrfs_delalloc_helper,
  1032. async_cow_start, async_cow_submit,
  1033. async_cow_free);
  1034. nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
  1035. PAGE_CACHE_SHIFT;
  1036. atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
  1037. btrfs_queue_work(root->fs_info->delalloc_workers,
  1038. &async_cow->work);
  1039. if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
  1040. wait_event(root->fs_info->async_submit_wait,
  1041. (atomic_read(&root->fs_info->async_delalloc_pages) <
  1042. limit));
  1043. }
  1044. while (atomic_read(&root->fs_info->async_submit_draining) &&
  1045. atomic_read(&root->fs_info->async_delalloc_pages)) {
  1046. wait_event(root->fs_info->async_submit_wait,
  1047. (atomic_read(&root->fs_info->async_delalloc_pages) ==
  1048. 0));
  1049. }
  1050. *nr_written += nr_pages;
  1051. start = cur_end + 1;
  1052. }
  1053. *page_started = 1;
  1054. return 0;
  1055. }
  1056. static noinline int csum_exist_in_range(struct btrfs_root *root,
  1057. u64 bytenr, u64 num_bytes)
  1058. {
  1059. int ret;
  1060. struct btrfs_ordered_sum *sums;
  1061. LIST_HEAD(list);
  1062. ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
  1063. bytenr + num_bytes - 1, &list, 0);
  1064. if (ret == 0 && list_empty(&list))
  1065. return 0;
  1066. while (!list_empty(&list)) {
  1067. sums = list_entry(list.next, struct btrfs_ordered_sum, list);
  1068. list_del(&sums->list);
  1069. kfree(sums);
  1070. }
  1071. return 1;
  1072. }
  1073. /*
  1074. * when nowcow writeback call back. This checks for snapshots or COW copies
  1075. * of the extents that exist in the file, and COWs the file as required.
  1076. *
  1077. * If no cow copies or snapshots exist, we write directly to the existing
  1078. * blocks on disk
  1079. */
  1080. static noinline int run_delalloc_nocow(struct inode *inode,
  1081. struct page *locked_page,
  1082. u64 start, u64 end, int *page_started, int force,
  1083. unsigned long *nr_written)
  1084. {
  1085. struct btrfs_root *root = BTRFS_I(inode)->root;
  1086. struct btrfs_trans_handle *trans;
  1087. struct extent_buffer *leaf;
  1088. struct btrfs_path *path;
  1089. struct btrfs_file_extent_item *fi;
  1090. struct btrfs_key found_key;
  1091. u64 cow_start;
  1092. u64 cur_offset;
  1093. u64 extent_end;
  1094. u64 extent_offset;
  1095. u64 disk_bytenr;
  1096. u64 num_bytes;
  1097. u64 disk_num_bytes;
  1098. u64 ram_bytes;
  1099. int extent_type;
  1100. int ret, err;
  1101. int type;
  1102. int nocow;
  1103. int check_prev = 1;
  1104. bool nolock;
  1105. u64 ino = btrfs_ino(inode);
  1106. path = btrfs_alloc_path();
  1107. if (!path) {
  1108. extent_clear_unlock_delalloc(inode, start, end, locked_page,
  1109. EXTENT_LOCKED | EXTENT_DELALLOC |
  1110. EXTENT_DO_ACCOUNTING |
  1111. EXTENT_DEFRAG, PAGE_UNLOCK |
  1112. PAGE_CLEAR_DIRTY |
  1113. PAGE_SET_WRITEBACK |
  1114. PAGE_END_WRITEBACK);
  1115. return -ENOMEM;
  1116. }
  1117. nolock = btrfs_is_free_space_inode(inode);
  1118. if (nolock)
  1119. trans = btrfs_join_transaction_nolock(root);
  1120. else
  1121. trans = btrfs_join_transaction(root);
  1122. if (IS_ERR(trans)) {
  1123. extent_clear_unlock_delalloc(inode, start, end, locked_page,
  1124. EXTENT_LOCKED | EXTENT_DELALLOC |
  1125. EXTENT_DO_ACCOUNTING |
  1126. EXTENT_DEFRAG, PAGE_UNLOCK |
  1127. PAGE_CLEAR_DIRTY |
  1128. PAGE_SET_WRITEBACK |
  1129. PAGE_END_WRITEBACK);
  1130. btrfs_free_path(path);
  1131. return PTR_ERR(trans);
  1132. }
  1133. trans->block_rsv = &root->fs_info->delalloc_block_rsv;
  1134. cow_start = (u64)-1;
  1135. cur_offset = start;
  1136. while (1) {
  1137. ret = btrfs_lookup_file_extent(trans, root, path, ino,
  1138. cur_offset, 0);
  1139. if (ret < 0)
  1140. goto error;
  1141. if (ret > 0 && path->slots[0] > 0 && check_prev) {
  1142. leaf = path->nodes[0];
  1143. btrfs_item_key_to_cpu(leaf, &found_key,
  1144. path->slots[0] - 1);
  1145. if (found_key.objectid == ino &&
  1146. found_key.type == BTRFS_EXTENT_DATA_KEY)
  1147. path->slots[0]--;
  1148. }
  1149. check_prev = 0;
  1150. next_slot:
  1151. leaf = path->nodes[0];
  1152. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  1153. ret = btrfs_next_leaf(root, path);
  1154. if (ret < 0)
  1155. goto error;
  1156. if (ret > 0)
  1157. break;
  1158. leaf = path->nodes[0];
  1159. }
  1160. nocow = 0;
  1161. disk_bytenr = 0;
  1162. num_bytes = 0;
  1163. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  1164. if (found_key.objectid > ino ||
  1165. found_key.type > BTRFS_EXTENT_DATA_KEY ||
  1166. found_key.offset > end)
  1167. break;
  1168. if (found_key.offset > cur_offset) {
  1169. extent_end = found_key.offset;
  1170. extent_type = 0;
  1171. goto out_check;
  1172. }
  1173. fi = btrfs_item_ptr(leaf, path->slots[0],
  1174. struct btrfs_file_extent_item);
  1175. extent_type = btrfs_file_extent_type(leaf, fi);
  1176. ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
  1177. if (extent_type == BTRFS_FILE_EXTENT_REG ||
  1178. extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
  1179. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  1180. extent_offset = btrfs_file_extent_offset(leaf, fi);
  1181. extent_end = found_key.offset +
  1182. btrfs_file_extent_num_bytes(leaf, fi);
  1183. disk_num_bytes =
  1184. btrfs_file_extent_disk_num_bytes(leaf, fi);
  1185. if (extent_end <= start) {
  1186. path->slots[0]++;
  1187. goto next_slot;
  1188. }
  1189. if (disk_bytenr == 0)
  1190. goto out_check;
  1191. if (btrfs_file_extent_compression(leaf, fi) ||
  1192. btrfs_file_extent_encryption(leaf, fi) ||
  1193. btrfs_file_extent_other_encoding(leaf, fi))
  1194. goto out_check;
  1195. if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
  1196. goto out_check;
  1197. if (btrfs_extent_readonly(root, disk_bytenr))
  1198. goto out_check;
  1199. if (btrfs_cross_ref_exist(trans, root, ino,
  1200. found_key.offset -
  1201. extent_offset, disk_bytenr))
  1202. goto out_check;
  1203. disk_bytenr += extent_offset;
  1204. disk_bytenr += cur_offset - found_key.offset;
  1205. num_bytes = min(end + 1, extent_end) - cur_offset;
  1206. /*
  1207. * if there are pending snapshots for this root,
  1208. * we fall into common COW way.
  1209. */
  1210. if (!nolock) {
  1211. err = btrfs_start_write_no_snapshoting(root);
  1212. if (!err)
  1213. goto out_check;
  1214. }
  1215. /*
  1216. * force cow if csum exists in the range.
  1217. * this ensure that csum for a given extent are
  1218. * either valid or do not exist.
  1219. */
  1220. if (csum_exist_in_range(root, disk_bytenr, num_bytes))
  1221. goto out_check;
  1222. nocow = 1;
  1223. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  1224. extent_end = found_key.offset +
  1225. btrfs_file_extent_inline_len(leaf,
  1226. path->slots[0], fi);
  1227. extent_end = ALIGN(extent_end, root->sectorsize);
  1228. } else {
  1229. BUG_ON(1);
  1230. }
  1231. out_check:
  1232. if (extent_end <= start) {
  1233. path->slots[0]++;
  1234. if (!nolock && nocow)
  1235. btrfs_end_write_no_snapshoting(root);
  1236. goto next_slot;
  1237. }
  1238. if (!nocow) {
  1239. if (cow_start == (u64)-1)
  1240. cow_start = cur_offset;
  1241. cur_offset = extent_end;
  1242. if (cur_offset > end)
  1243. break;
  1244. path->slots[0]++;
  1245. goto next_slot;
  1246. }
  1247. btrfs_release_path(path);
  1248. if (cow_start != (u64)-1) {
  1249. ret = cow_file_range(inode, locked_page,
  1250. cow_start, found_key.offset - 1,
  1251. page_started, nr_written, 1);
  1252. if (ret) {
  1253. if (!nolock && nocow)
  1254. btrfs_end_write_no_snapshoting(root);
  1255. goto error;
  1256. }
  1257. cow_start = (u64)-1;
  1258. }
  1259. if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
  1260. struct extent_map *em;
  1261. struct extent_map_tree *em_tree;
  1262. em_tree = &BTRFS_I(inode)->extent_tree;
  1263. em = alloc_extent_map();
  1264. BUG_ON(!em); /* -ENOMEM */
  1265. em->start = cur_offset;
  1266. em->orig_start = found_key.offset - extent_offset;
  1267. em->len = num_bytes;
  1268. em->block_len = num_bytes;
  1269. em->block_start = disk_bytenr;
  1270. em->orig_block_len = disk_num_bytes;
  1271. em->ram_bytes = ram_bytes;
  1272. em->bdev = root->fs_info->fs_devices->latest_bdev;
  1273. em->mod_start = em->start;
  1274. em->mod_len = em->len;
  1275. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  1276. set_bit(EXTENT_FLAG_FILLING, &em->flags);
  1277. em->generation = -1;
  1278. while (1) {
  1279. write_lock(&em_tree->lock);
  1280. ret = add_extent_mapping(em_tree, em, 1);
  1281. write_unlock(&em_tree->lock);
  1282. if (ret != -EEXIST) {
  1283. free_extent_map(em);
  1284. break;
  1285. }
  1286. btrfs_drop_extent_cache(inode, em->start,
  1287. em->start + em->len - 1, 0);
  1288. }
  1289. type = BTRFS_ORDERED_PREALLOC;
  1290. } else {
  1291. type = BTRFS_ORDERED_NOCOW;
  1292. }
  1293. ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
  1294. num_bytes, num_bytes, type);
  1295. BUG_ON(ret); /* -ENOMEM */
  1296. if (root->root_key.objectid ==
  1297. BTRFS_DATA_RELOC_TREE_OBJECTID) {
  1298. ret = btrfs_reloc_clone_csums(inode, cur_offset,
  1299. num_bytes);
  1300. if (ret) {
  1301. if (!nolock && nocow)
  1302. btrfs_end_write_no_snapshoting(root);
  1303. goto error;
  1304. }
  1305. }
  1306. extent_clear_unlock_delalloc(inode, cur_offset,
  1307. cur_offset + num_bytes - 1,
  1308. locked_page, EXTENT_LOCKED |
  1309. EXTENT_DELALLOC, PAGE_UNLOCK |
  1310. PAGE_SET_PRIVATE2);
  1311. if (!nolock && nocow)
  1312. btrfs_end_write_no_snapshoting(root);
  1313. cur_offset = extent_end;
  1314. if (cur_offset > end)
  1315. break;
  1316. }
  1317. btrfs_release_path(path);
  1318. if (cur_offset <= end && cow_start == (u64)-1) {
  1319. cow_start = cur_offset;
  1320. cur_offset = end;
  1321. }
  1322. if (cow_start != (u64)-1) {
  1323. ret = cow_file_range(inode, locked_page, cow_start, end,
  1324. page_started, nr_written, 1);
  1325. if (ret)
  1326. goto error;
  1327. }
  1328. error:
  1329. err = btrfs_end_transaction(trans, root);
  1330. if (!ret)
  1331. ret = err;
  1332. if (ret && cur_offset < end)
  1333. extent_clear_unlock_delalloc(inode, cur_offset, end,
  1334. locked_page, EXTENT_LOCKED |
  1335. EXTENT_DELALLOC | EXTENT_DEFRAG |
  1336. EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
  1337. PAGE_CLEAR_DIRTY |
  1338. PAGE_SET_WRITEBACK |
  1339. PAGE_END_WRITEBACK);
  1340. btrfs_free_path(path);
  1341. return ret;
  1342. }
  1343. static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
  1344. {
  1345. if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
  1346. !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
  1347. return 0;
  1348. /*
  1349. * @defrag_bytes is a hint value, no spinlock held here,
  1350. * if is not zero, it means the file is defragging.
  1351. * Force cow if given extent needs to be defragged.
  1352. */
  1353. if (BTRFS_I(inode)->defrag_bytes &&
  1354. test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
  1355. EXTENT_DEFRAG, 0, NULL))
  1356. return 1;
  1357. return 0;
  1358. }
  1359. /*
  1360. * extent_io.c call back to do delayed allocation processing
  1361. */
  1362. static int run_delalloc_range(struct inode *inode, struct page *locked_page,
  1363. u64 start, u64 end, int *page_started,
  1364. unsigned long *nr_written)
  1365. {
  1366. int ret;
  1367. int force_cow = need_force_cow(inode, start, end);
  1368. if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
  1369. ret = run_delalloc_nocow(inode, locked_page, start, end,
  1370. page_started, 1, nr_written);
  1371. } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
  1372. ret = run_delalloc_nocow(inode, locked_page, start, end,
  1373. page_started, 0, nr_written);
  1374. } else if (!inode_need_compress(inode)) {
  1375. ret = cow_file_range(inode, locked_page, start, end,
  1376. page_started, nr_written, 1);
  1377. } else {
  1378. set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
  1379. &BTRFS_I(inode)->runtime_flags);
  1380. ret = cow_file_range_async(inode, locked_page, start, end,
  1381. page_started, nr_written);
  1382. }
  1383. return ret;
  1384. }
  1385. static void btrfs_split_extent_hook(struct inode *inode,
  1386. struct extent_state *orig, u64 split)
  1387. {
  1388. u64 size;
  1389. /* not delalloc, ignore it */
  1390. if (!(orig->state & EXTENT_DELALLOC))
  1391. return;
  1392. size = orig->end - orig->start + 1;
  1393. if (size > BTRFS_MAX_EXTENT_SIZE) {
  1394. u64 num_extents;
  1395. u64 new_size;
  1396. /*
  1397. * See the explanation in btrfs_merge_extent_hook, the same
  1398. * applies here, just in reverse.
  1399. */
  1400. new_size = orig->end - split + 1;
  1401. num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
  1402. BTRFS_MAX_EXTENT_SIZE);
  1403. new_size = split - orig->start;
  1404. num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
  1405. BTRFS_MAX_EXTENT_SIZE);
  1406. if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1,
  1407. BTRFS_MAX_EXTENT_SIZE) >= num_extents)
  1408. return;
  1409. }
  1410. spin_lock(&BTRFS_I(inode)->lock);
  1411. BTRFS_I(inode)->outstanding_extents++;
  1412. spin_unlock(&BTRFS_I(inode)->lock);
  1413. }
  1414. /*
  1415. * extent_io.c merge_extent_hook, used to track merged delayed allocation
  1416. * extents so we can keep track of new extents that are just merged onto old
  1417. * extents, such as when we are doing sequential writes, so we can properly
  1418. * account for the metadata space we'll need.
  1419. */
  1420. static void btrfs_merge_extent_hook(struct inode *inode,
  1421. struct extent_state *new,
  1422. struct extent_state *other)
  1423. {
  1424. u64 new_size, old_size;
  1425. u64 num_extents;
  1426. /* not delalloc, ignore it */
  1427. if (!(other->state & EXTENT_DELALLOC))
  1428. return;
  1429. if (new->start > other->start)
  1430. new_size = new->end - other->start + 1;
  1431. else
  1432. new_size = other->end - new->start + 1;
  1433. /* we're not bigger than the max, unreserve the space and go */
  1434. if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
  1435. spin_lock(&BTRFS_I(inode)->lock);
  1436. BTRFS_I(inode)->outstanding_extents--;
  1437. spin_unlock(&BTRFS_I(inode)->lock);
  1438. return;
  1439. }
  1440. /*
  1441. * We have to add up either side to figure out how many extents were
  1442. * accounted for before we merged into one big extent. If the number of
  1443. * extents we accounted for is <= the amount we need for the new range
  1444. * then we can return, otherwise drop. Think of it like this
  1445. *
  1446. * [ 4k][MAX_SIZE]
  1447. *
  1448. * So we've grown the extent by a MAX_SIZE extent, this would mean we
  1449. * need 2 outstanding extents, on one side we have 1 and the other side
  1450. * we have 1 so they are == and we can return. But in this case
  1451. *
  1452. * [MAX_SIZE+4k][MAX_SIZE+4k]
  1453. *
  1454. * Each range on their own accounts for 2 extents, but merged together
  1455. * they are only 3 extents worth of accounting, so we need to drop in
  1456. * this case.
  1457. */
  1458. old_size = other->end - other->start + 1;
  1459. num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
  1460. BTRFS_MAX_EXTENT_SIZE);
  1461. old_size = new->end - new->start + 1;
  1462. num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
  1463. BTRFS_MAX_EXTENT_SIZE);
  1464. if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
  1465. BTRFS_MAX_EXTENT_SIZE) >= num_extents)
  1466. return;
  1467. spin_lock(&BTRFS_I(inode)->lock);
  1468. BTRFS_I(inode)->outstanding_extents--;
  1469. spin_unlock(&BTRFS_I(inode)->lock);
  1470. }
  1471. static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
  1472. struct inode *inode)
  1473. {
  1474. spin_lock(&root->delalloc_lock);
  1475. if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
  1476. list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
  1477. &root->delalloc_inodes);
  1478. set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
  1479. &BTRFS_I(inode)->runtime_flags);
  1480. root->nr_delalloc_inodes++;
  1481. if (root->nr_delalloc_inodes == 1) {
  1482. spin_lock(&root->fs_info->delalloc_root_lock);
  1483. BUG_ON(!list_empty(&root->delalloc_root));
  1484. list_add_tail(&root->delalloc_root,
  1485. &root->fs_info->delalloc_roots);
  1486. spin_unlock(&root->fs_info->delalloc_root_lock);
  1487. }
  1488. }
  1489. spin_unlock(&root->delalloc_lock);
  1490. }
  1491. static void btrfs_del_delalloc_inode(struct btrfs_root *root,
  1492. struct inode *inode)
  1493. {
  1494. spin_lock(&root->delalloc_lock);
  1495. if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
  1496. list_del_init(&BTRFS_I(inode)->delalloc_inodes);
  1497. clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
  1498. &BTRFS_I(inode)->runtime_flags);
  1499. root->nr_delalloc_inodes--;
  1500. if (!root->nr_delalloc_inodes) {
  1501. spin_lock(&root->fs_info->delalloc_root_lock);
  1502. BUG_ON(list_empty(&root->delalloc_root));
  1503. list_del_init(&root->delalloc_root);
  1504. spin_unlock(&root->fs_info->delalloc_root_lock);
  1505. }
  1506. }
  1507. spin_unlock(&root->delalloc_lock);
  1508. }
  1509. /*
  1510. * extent_io.c set_bit_hook, used to track delayed allocation
  1511. * bytes in this file, and to maintain the list of inodes that
  1512. * have pending delalloc work to be done.
  1513. */
  1514. static void btrfs_set_bit_hook(struct inode *inode,
  1515. struct extent_state *state, unsigned *bits)
  1516. {
  1517. if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
  1518. WARN_ON(1);
  1519. /*
  1520. * set_bit and clear bit hooks normally require _irqsave/restore
  1521. * but in this case, we are only testing for the DELALLOC
  1522. * bit, which is only set or cleared with irqs on
  1523. */
  1524. if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
  1525. struct btrfs_root *root = BTRFS_I(inode)->root;
  1526. u64 len = state->end + 1 - state->start;
  1527. bool do_list = !btrfs_is_free_space_inode(inode);
  1528. if (*bits & EXTENT_FIRST_DELALLOC) {
  1529. *bits &= ~EXTENT_FIRST_DELALLOC;
  1530. } else {
  1531. spin_lock(&BTRFS_I(inode)->lock);
  1532. BTRFS_I(inode)->outstanding_extents++;
  1533. spin_unlock(&BTRFS_I(inode)->lock);
  1534. }
  1535. /* For sanity tests */
  1536. if (btrfs_test_is_dummy_root(root))
  1537. return;
  1538. __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
  1539. root->fs_info->delalloc_batch);
  1540. spin_lock(&BTRFS_I(inode)->lock);
  1541. BTRFS_I(inode)->delalloc_bytes += len;
  1542. if (*bits & EXTENT_DEFRAG)
  1543. BTRFS_I(inode)->defrag_bytes += len;
  1544. if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
  1545. &BTRFS_I(inode)->runtime_flags))
  1546. btrfs_add_delalloc_inodes(root, inode);
  1547. spin_unlock(&BTRFS_I(inode)->lock);
  1548. }
  1549. }
  1550. /*
  1551. * extent_io.c clear_bit_hook, see set_bit_hook for why
  1552. */
  1553. static void btrfs_clear_bit_hook(struct inode *inode,
  1554. struct extent_state *state,
  1555. unsigned *bits)
  1556. {
  1557. u64 len = state->end + 1 - state->start;
  1558. u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1,
  1559. BTRFS_MAX_EXTENT_SIZE);
  1560. spin_lock(&BTRFS_I(inode)->lock);
  1561. if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
  1562. BTRFS_I(inode)->defrag_bytes -= len;
  1563. spin_unlock(&BTRFS_I(inode)->lock);
  1564. /*
  1565. * set_bit and clear bit hooks normally require _irqsave/restore
  1566. * but in this case, we are only testing for the DELALLOC
  1567. * bit, which is only set or cleared with irqs on
  1568. */
  1569. if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
  1570. struct btrfs_root *root = BTRFS_I(inode)->root;
  1571. bool do_list = !btrfs_is_free_space_inode(inode);
  1572. if (*bits & EXTENT_FIRST_DELALLOC) {
  1573. *bits &= ~EXTENT_FIRST_DELALLOC;
  1574. } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
  1575. spin_lock(&BTRFS_I(inode)->lock);
  1576. BTRFS_I(inode)->outstanding_extents -= num_extents;
  1577. spin_unlock(&BTRFS_I(inode)->lock);
  1578. }
  1579. /*
  1580. * We don't reserve metadata space for space cache inodes so we
  1581. * don't need to call dellalloc_release_metadata if there is an
  1582. * error.
  1583. */
  1584. if (*bits & EXTENT_DO_ACCOUNTING &&
  1585. root != root->fs_info->tree_root)
  1586. btrfs_delalloc_release_metadata(inode, len);
  1587. /* For sanity tests. */
  1588. if (btrfs_test_is_dummy_root(root))
  1589. return;
  1590. if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
  1591. && do_list && !(state->state & EXTENT_NORESERVE))
  1592. btrfs_free_reserved_data_space(inode, len);
  1593. __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
  1594. root->fs_info->delalloc_batch);
  1595. spin_lock(&BTRFS_I(inode)->lock);
  1596. BTRFS_I(inode)->delalloc_bytes -= len;
  1597. if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
  1598. test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
  1599. &BTRFS_I(inode)->runtime_flags))
  1600. btrfs_del_delalloc_inode(root, inode);
  1601. spin_unlock(&BTRFS_I(inode)->lock);
  1602. }
  1603. }
  1604. /*
  1605. * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
  1606. * we don't create bios that span stripes or chunks
  1607. */
  1608. int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
  1609. size_t size, struct bio *bio,
  1610. unsigned long bio_flags)
  1611. {
  1612. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  1613. u64 logical = (u64)bio->bi_iter.bi_sector << 9;
  1614. u64 length = 0;
  1615. u64 map_length;
  1616. int ret;
  1617. if (bio_flags & EXTENT_BIO_COMPRESSED)
  1618. return 0;
  1619. length = bio->bi_iter.bi_size;
  1620. map_length = length;
  1621. ret = btrfs_map_block(root->fs_info, rw, logical,
  1622. &map_length, NULL, 0);
  1623. /* Will always return 0 with map_multi == NULL */
  1624. BUG_ON(ret < 0);
  1625. if (map_length < length + size)
  1626. return 1;
  1627. return 0;
  1628. }
  1629. /*
  1630. * in order to insert checksums into the metadata in large chunks,
  1631. * we wait until bio submission time. All the pages in the bio are
  1632. * checksummed and sums are attached onto the ordered extent record.
  1633. *
  1634. * At IO completion time the cums attached on the ordered extent record
  1635. * are inserted into the btree
  1636. */
  1637. static int __btrfs_submit_bio_start(struct inode *inode, int rw,
  1638. struct bio *bio, int mirror_num,
  1639. unsigned long bio_flags,
  1640. u64 bio_offset)
  1641. {
  1642. struct btrfs_root *root = BTRFS_I(inode)->root;
  1643. int ret = 0;
  1644. ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
  1645. BUG_ON(ret); /* -ENOMEM */
  1646. return 0;
  1647. }
  1648. /*
  1649. * in order to insert checksums into the metadata in large chunks,
  1650. * we wait until bio submission time. All the pages in the bio are
  1651. * checksummed and sums are attached onto the ordered extent record.
  1652. *
  1653. * At IO completion time the cums attached on the ordered extent record
  1654. * are inserted into the btree
  1655. */
  1656. static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
  1657. int mirror_num, unsigned long bio_flags,
  1658. u64 bio_offset)
  1659. {
  1660. struct btrfs_root *root = BTRFS_I(inode)->root;
  1661. int ret;
  1662. ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
  1663. if (ret) {
  1664. bio->bi_error = ret;
  1665. bio_endio(bio);
  1666. }
  1667. return ret;
  1668. }
  1669. /*
  1670. * extent_io.c submission hook. This does the right thing for csum calculation
  1671. * on write, or reading the csums from the tree before a read
  1672. */
  1673. static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
  1674. int mirror_num, unsigned long bio_flags,
  1675. u64 bio_offset)
  1676. {
  1677. struct btrfs_root *root = BTRFS_I(inode)->root;
  1678. int ret = 0;
  1679. int skip_sum;
  1680. int metadata = 0;
  1681. int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
  1682. skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  1683. if (btrfs_is_free_space_inode(inode))
  1684. metadata = 2;
  1685. if (!(rw & REQ_WRITE)) {
  1686. ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
  1687. if (ret)
  1688. goto out;
  1689. if (bio_flags & EXTENT_BIO_COMPRESSED) {
  1690. ret = btrfs_submit_compressed_read(inode, bio,
  1691. mirror_num,
  1692. bio_flags);
  1693. goto out;
  1694. } else if (!skip_sum) {
  1695. ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
  1696. if (ret)
  1697. goto out;
  1698. }
  1699. goto mapit;
  1700. } else if (async && !skip_sum) {
  1701. /* csum items have already been cloned */
  1702. if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
  1703. goto mapit;
  1704. /* we're doing a write, do the async checksumming */
  1705. ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
  1706. inode, rw, bio, mirror_num,
  1707. bio_flags, bio_offset,
  1708. __btrfs_submit_bio_start,
  1709. __btrfs_submit_bio_done);
  1710. goto out;
  1711. } else if (!skip_sum) {
  1712. ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
  1713. if (ret)
  1714. goto out;
  1715. }
  1716. mapit:
  1717. ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
  1718. out:
  1719. if (ret < 0) {
  1720. bio->bi_error = ret;
  1721. bio_endio(bio);
  1722. }
  1723. return ret;
  1724. }
  1725. /*
  1726. * given a list of ordered sums record them in the inode. This happens
  1727. * at IO completion time based on sums calculated at bio submission time.
  1728. */
  1729. static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
  1730. struct inode *inode, u64 file_offset,
  1731. struct list_head *list)
  1732. {
  1733. struct btrfs_ordered_sum *sum;
  1734. list_for_each_entry(sum, list, list) {
  1735. trans->adding_csums = 1;
  1736. btrfs_csum_file_blocks(trans,
  1737. BTRFS_I(inode)->root->fs_info->csum_root, sum);
  1738. trans->adding_csums = 0;
  1739. }
  1740. return 0;
  1741. }
  1742. int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
  1743. struct extent_state **cached_state)
  1744. {
  1745. WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
  1746. return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
  1747. cached_state, GFP_NOFS);
  1748. }
  1749. /* see btrfs_writepage_start_hook for details on why this is required */
  1750. struct btrfs_writepage_fixup {
  1751. struct page *page;
  1752. struct btrfs_work work;
  1753. };
  1754. static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
  1755. {
  1756. struct btrfs_writepage_fixup *fixup;
  1757. struct btrfs_ordered_extent *ordered;
  1758. struct extent_state *cached_state = NULL;
  1759. struct page *page;
  1760. struct inode *inode;
  1761. u64 page_start;
  1762. u64 page_end;
  1763. int ret;
  1764. fixup = container_of(work, struct btrfs_writepage_fixup, work);
  1765. page = fixup->page;
  1766. again:
  1767. lock_page(page);
  1768. if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
  1769. ClearPageChecked(page);
  1770. goto out_page;
  1771. }
  1772. inode = page->mapping->host;
  1773. page_start = page_offset(page);
  1774. page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
  1775. lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
  1776. &cached_state);
  1777. /* already ordered? We're done */
  1778. if (PagePrivate2(page))
  1779. goto out;
  1780. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  1781. if (ordered) {
  1782. unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
  1783. page_end, &cached_state, GFP_NOFS);
  1784. unlock_page(page);
  1785. btrfs_start_ordered_extent(inode, ordered, 1);
  1786. btrfs_put_ordered_extent(ordered);
  1787. goto again;
  1788. }
  1789. ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
  1790. if (ret) {
  1791. mapping_set_error(page->mapping, ret);
  1792. end_extent_writepage(page, ret, page_start, page_end);
  1793. ClearPageChecked(page);
  1794. goto out;
  1795. }
  1796. btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
  1797. ClearPageChecked(page);
  1798. set_page_dirty(page);
  1799. out:
  1800. unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
  1801. &cached_state, GFP_NOFS);
  1802. out_page:
  1803. unlock_page(page);
  1804. page_cache_release(page);
  1805. kfree(fixup);
  1806. }
  1807. /*
  1808. * There are a few paths in the higher layers of the kernel that directly
  1809. * set the page dirty bit without asking the filesystem if it is a
  1810. * good idea. This causes problems because we want to make sure COW
  1811. * properly happens and the data=ordered rules are followed.
  1812. *
  1813. * In our case any range that doesn't have the ORDERED bit set
  1814. * hasn't been properly setup for IO. We kick off an async process
  1815. * to fix it up. The async helper will wait for ordered extents, set
  1816. * the delalloc bit and make it safe to write the page.
  1817. */
  1818. static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
  1819. {
  1820. struct inode *inode = page->mapping->host;
  1821. struct btrfs_writepage_fixup *fixup;
  1822. struct btrfs_root *root = BTRFS_I(inode)->root;
  1823. /* this page is properly in the ordered list */
  1824. if (TestClearPagePrivate2(page))
  1825. return 0;
  1826. if (PageChecked(page))
  1827. return -EAGAIN;
  1828. fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
  1829. if (!fixup)
  1830. return -EAGAIN;
  1831. SetPageChecked(page);
  1832. page_cache_get(page);
  1833. btrfs_init_work(&fixup->work, btrfs_fixup_helper,
  1834. btrfs_writepage_fixup_worker, NULL, NULL);
  1835. fixup->page = page;
  1836. btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work);
  1837. return -EBUSY;
  1838. }
  1839. static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
  1840. struct inode *inode, u64 file_pos,
  1841. u64 disk_bytenr, u64 disk_num_bytes,
  1842. u64 num_bytes, u64 ram_bytes,
  1843. u8 compression, u8 encryption,
  1844. u16 other_encoding, int extent_type)
  1845. {
  1846. struct btrfs_root *root = BTRFS_I(inode)->root;
  1847. struct btrfs_file_extent_item *fi;
  1848. struct btrfs_path *path;
  1849. struct extent_buffer *leaf;
  1850. struct btrfs_key ins;
  1851. int extent_inserted = 0;
  1852. int ret;
  1853. path = btrfs_alloc_path();
  1854. if (!path)
  1855. return -ENOMEM;
  1856. /*
  1857. * we may be replacing one extent in the tree with another.
  1858. * The new extent is pinned in the extent map, and we don't want
  1859. * to drop it from the cache until it is completely in the btree.
  1860. *
  1861. * So, tell btrfs_drop_extents to leave this extent in the cache.
  1862. * the caller is expected to unpin it and allow it to be merged
  1863. * with the others.
  1864. */
  1865. ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
  1866. file_pos + num_bytes, NULL, 0,
  1867. 1, sizeof(*fi), &extent_inserted);
  1868. if (ret)
  1869. goto out;
  1870. if (!extent_inserted) {
  1871. ins.objectid = btrfs_ino(inode);
  1872. ins.offset = file_pos;
  1873. ins.type = BTRFS_EXTENT_DATA_KEY;
  1874. path->leave_spinning = 1;
  1875. ret = btrfs_insert_empty_item(trans, root, path, &ins,
  1876. sizeof(*fi));
  1877. if (ret)
  1878. goto out;
  1879. }
  1880. leaf = path->nodes[0];
  1881. fi = btrfs_item_ptr(leaf, path->slots[0],
  1882. struct btrfs_file_extent_item);
  1883. btrfs_set_file_extent_generation(leaf, fi, trans->transid);
  1884. btrfs_set_file_extent_type(leaf, fi, extent_type);
  1885. btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
  1886. btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
  1887. btrfs_set_file_extent_offset(leaf, fi, 0);
  1888. btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
  1889. btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
  1890. btrfs_set_file_extent_compression(leaf, fi, compression);
  1891. btrfs_set_file_extent_encryption(leaf, fi, encryption);
  1892. btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
  1893. btrfs_mark_buffer_dirty(leaf);
  1894. btrfs_release_path(path);
  1895. inode_add_bytes(inode, num_bytes);
  1896. ins.objectid = disk_bytenr;
  1897. ins.offset = disk_num_bytes;
  1898. ins.type = BTRFS_EXTENT_ITEM_KEY;
  1899. ret = btrfs_alloc_reserved_file_extent(trans, root,
  1900. root->root_key.objectid,
  1901. btrfs_ino(inode), file_pos, &ins);
  1902. out:
  1903. btrfs_free_path(path);
  1904. return ret;
  1905. }
  1906. /* snapshot-aware defrag */
  1907. struct sa_defrag_extent_backref {
  1908. struct rb_node node;
  1909. struct old_sa_defrag_extent *old;
  1910. u64 root_id;
  1911. u64 inum;
  1912. u64 file_pos;
  1913. u64 extent_offset;
  1914. u64 num_bytes;
  1915. u64 generation;
  1916. };
  1917. struct old_sa_defrag_extent {
  1918. struct list_head list;
  1919. struct new_sa_defrag_extent *new;
  1920. u64 extent_offset;
  1921. u64 bytenr;
  1922. u64 offset;
  1923. u64 len;
  1924. int count;
  1925. };
  1926. struct new_sa_defrag_extent {
  1927. struct rb_root root;
  1928. struct list_head head;
  1929. struct btrfs_path *path;
  1930. struct inode *inode;
  1931. u64 file_pos;
  1932. u64 len;
  1933. u64 bytenr;
  1934. u64 disk_len;
  1935. u8 compress_type;
  1936. };
  1937. static int backref_comp(struct sa_defrag_extent_backref *b1,
  1938. struct sa_defrag_extent_backref *b2)
  1939. {
  1940. if (b1->root_id < b2->root_id)
  1941. return -1;
  1942. else if (b1->root_id > b2->root_id)
  1943. return 1;
  1944. if (b1->inum < b2->inum)
  1945. return -1;
  1946. else if (b1->inum > b2->inum)
  1947. return 1;
  1948. if (b1->file_pos < b2->file_pos)
  1949. return -1;
  1950. else if (b1->file_pos > b2->file_pos)
  1951. return 1;
  1952. /*
  1953. * [------------------------------] ===> (a range of space)
  1954. * |<--->| |<---->| =============> (fs/file tree A)
  1955. * |<---------------------------->| ===> (fs/file tree B)
  1956. *
  1957. * A range of space can refer to two file extents in one tree while
  1958. * refer to only one file extent in another tree.
  1959. *
  1960. * So we may process a disk offset more than one time(two extents in A)
  1961. * and locate at the same extent(one extent in B), then insert two same
  1962. * backrefs(both refer to the extent in B).
  1963. */
  1964. return 0;
  1965. }
  1966. static void backref_insert(struct rb_root *root,
  1967. struct sa_defrag_extent_backref *backref)
  1968. {
  1969. struct rb_node **p = &root->rb_node;
  1970. struct rb_node *parent = NULL;
  1971. struct sa_defrag_extent_backref *entry;
  1972. int ret;
  1973. while (*p) {
  1974. parent = *p;
  1975. entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
  1976. ret = backref_comp(backref, entry);
  1977. if (ret < 0)
  1978. p = &(*p)->rb_left;
  1979. else
  1980. p = &(*p)->rb_right;
  1981. }
  1982. rb_link_node(&backref->node, parent, p);
  1983. rb_insert_color(&backref->node, root);
  1984. }
  1985. /*
  1986. * Note the backref might has changed, and in this case we just return 0.
  1987. */
  1988. static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
  1989. void *ctx)
  1990. {
  1991. struct btrfs_file_extent_item *extent;
  1992. struct btrfs_fs_info *fs_info;
  1993. struct old_sa_defrag_extent *old = ctx;
  1994. struct new_sa_defrag_extent *new = old->new;
  1995. struct btrfs_path *path = new->path;
  1996. struct btrfs_key key;
  1997. struct btrfs_root *root;
  1998. struct sa_defrag_extent_backref *backref;
  1999. struct extent_buffer *leaf;
  2000. struct inode *inode = new->inode;
  2001. int slot;
  2002. int ret;
  2003. u64 extent_offset;
  2004. u64 num_bytes;
  2005. if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
  2006. inum == btrfs_ino(inode))
  2007. return 0;
  2008. key.objectid = root_id;
  2009. key.type = BTRFS_ROOT_ITEM_KEY;
  2010. key.offset = (u64)-1;
  2011. fs_info = BTRFS_I(inode)->root->fs_info;
  2012. root = btrfs_read_fs_root_no_name(fs_info, &key);
  2013. if (IS_ERR(root)) {
  2014. if (PTR_ERR(root) == -ENOENT)
  2015. return 0;
  2016. WARN_ON(1);
  2017. pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
  2018. inum, offset, root_id);
  2019. return PTR_ERR(root);
  2020. }
  2021. key.objectid = inum;
  2022. key.type = BTRFS_EXTENT_DATA_KEY;
  2023. if (offset > (u64)-1 << 32)
  2024. key.offset = 0;
  2025. else
  2026. key.offset = offset;
  2027. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2028. if (WARN_ON(ret < 0))
  2029. return ret;
  2030. ret = 0;
  2031. while (1) {
  2032. cond_resched();
  2033. leaf = path->nodes[0];
  2034. slot = path->slots[0];
  2035. if (slot >= btrfs_header_nritems(leaf)) {
  2036. ret = btrfs_next_leaf(root, path);
  2037. if (ret < 0) {
  2038. goto out;
  2039. } else if (ret > 0) {
  2040. ret = 0;
  2041. goto out;
  2042. }
  2043. continue;
  2044. }
  2045. path->slots[0]++;
  2046. btrfs_item_key_to_cpu(leaf, &key, slot);
  2047. if (key.objectid > inum)
  2048. goto out;
  2049. if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
  2050. continue;
  2051. extent = btrfs_item_ptr(leaf, slot,
  2052. struct btrfs_file_extent_item);
  2053. if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
  2054. continue;
  2055. /*
  2056. * 'offset' refers to the exact key.offset,
  2057. * NOT the 'offset' field in btrfs_extent_data_ref, ie.
  2058. * (key.offset - extent_offset).
  2059. */
  2060. if (key.offset != offset)
  2061. continue;
  2062. extent_offset = btrfs_file_extent_offset(leaf, extent);
  2063. num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
  2064. if (extent_offset >= old->extent_offset + old->offset +
  2065. old->len || extent_offset + num_bytes <=
  2066. old->extent_offset + old->offset)
  2067. continue;
  2068. break;
  2069. }
  2070. backref = kmalloc(sizeof(*backref), GFP_NOFS);
  2071. if (!backref) {
  2072. ret = -ENOENT;
  2073. goto out;
  2074. }
  2075. backref->root_id = root_id;
  2076. backref->inum = inum;
  2077. backref->file_pos = offset;
  2078. backref->num_bytes = num_bytes;
  2079. backref->extent_offset = extent_offset;
  2080. backref->generation = btrfs_file_extent_generation(leaf, extent);
  2081. backref->old = old;
  2082. backref_insert(&new->root, backref);
  2083. old->count++;
  2084. out:
  2085. btrfs_release_path(path);
  2086. WARN_ON(ret);
  2087. return ret;
  2088. }
  2089. static noinline bool record_extent_backrefs(struct btrfs_path *path,
  2090. struct new_sa_defrag_extent *new)
  2091. {
  2092. struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
  2093. struct old_sa_defrag_extent *old, *tmp;
  2094. int ret;
  2095. new->path = path;
  2096. list_for_each_entry_safe(old, tmp, &new->head, list) {
  2097. ret = iterate_inodes_from_logical(old->bytenr +
  2098. old->extent_offset, fs_info,
  2099. path, record_one_backref,
  2100. old);
  2101. if (ret < 0 && ret != -ENOENT)
  2102. return false;
  2103. /* no backref to be processed for this extent */
  2104. if (!old->count) {
  2105. list_del(&old->list);
  2106. kfree(old);
  2107. }
  2108. }
  2109. if (list_empty(&new->head))
  2110. return false;
  2111. return true;
  2112. }
  2113. static int relink_is_mergable(struct extent_buffer *leaf,
  2114. struct btrfs_file_extent_item *fi,
  2115. struct new_sa_defrag_extent *new)
  2116. {
  2117. if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
  2118. return 0;
  2119. if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
  2120. return 0;
  2121. if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
  2122. return 0;
  2123. if (btrfs_file_extent_encryption(leaf, fi) ||
  2124. btrfs_file_extent_other_encoding(leaf, fi))
  2125. return 0;
  2126. return 1;
  2127. }
  2128. /*
  2129. * Note the backref might has changed, and in this case we just return 0.
  2130. */
  2131. static noinline int relink_extent_backref(struct btrfs_path *path,
  2132. struct sa_defrag_extent_backref *prev,
  2133. struct sa_defrag_extent_backref *backref)
  2134. {
  2135. struct btrfs_file_extent_item *extent;
  2136. struct btrfs_file_extent_item *item;
  2137. struct btrfs_ordered_extent *ordered;
  2138. struct btrfs_trans_handle *trans;
  2139. struct btrfs_fs_info *fs_info;
  2140. struct btrfs_root *root;
  2141. struct btrfs_key key;
  2142. struct extent_buffer *leaf;
  2143. struct old_sa_defrag_extent *old = backref->old;
  2144. struct new_sa_defrag_extent *new = old->new;
  2145. struct inode *src_inode = new->inode;
  2146. struct inode *inode;
  2147. struct extent_state *cached = NULL;
  2148. int ret = 0;
  2149. u64 start;
  2150. u64 len;
  2151. u64 lock_start;
  2152. u64 lock_end;
  2153. bool merge = false;
  2154. int index;
  2155. if (prev && prev->root_id == backref->root_id &&
  2156. prev->inum == backref->inum &&
  2157. prev->file_pos + prev->num_bytes == backref->file_pos)
  2158. merge = true;
  2159. /* step 1: get root */
  2160. key.objectid = backref->root_id;
  2161. key.type = BTRFS_ROOT_ITEM_KEY;
  2162. key.offset = (u64)-1;
  2163. fs_info = BTRFS_I(src_inode)->root->fs_info;
  2164. index = srcu_read_lock(&fs_info->subvol_srcu);
  2165. root = btrfs_read_fs_root_no_name(fs_info, &key);
  2166. if (IS_ERR(root)) {
  2167. srcu_read_unlock(&fs_info->subvol_srcu, index);
  2168. if (PTR_ERR(root) == -ENOENT)
  2169. return 0;
  2170. return PTR_ERR(root);
  2171. }
  2172. if (btrfs_root_readonly(root)) {
  2173. srcu_read_unlock(&fs_info->subvol_srcu, index);
  2174. return 0;
  2175. }
  2176. /* step 2: get inode */
  2177. key.objectid = backref->inum;
  2178. key.type = BTRFS_INODE_ITEM_KEY;
  2179. key.offset = 0;
  2180. inode = btrfs_iget(fs_info->sb, &key, root, NULL);
  2181. if (IS_ERR(inode)) {
  2182. srcu_read_unlock(&fs_info->subvol_srcu, index);
  2183. return 0;
  2184. }
  2185. srcu_read_unlock(&fs_info->subvol_srcu, index);
  2186. /* step 3: relink backref */
  2187. lock_start = backref->file_pos;
  2188. lock_end = backref->file_pos + backref->num_bytes - 1;
  2189. lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
  2190. 0, &cached);
  2191. ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
  2192. if (ordered) {
  2193. btrfs_put_ordered_extent(ordered);
  2194. goto out_unlock;
  2195. }
  2196. trans = btrfs_join_transaction(root);
  2197. if (IS_ERR(trans)) {
  2198. ret = PTR_ERR(trans);
  2199. goto out_unlock;
  2200. }
  2201. key.objectid = backref->inum;
  2202. key.type = BTRFS_EXTENT_DATA_KEY;
  2203. key.offset = backref->file_pos;
  2204. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2205. if (ret < 0) {
  2206. goto out_free_path;
  2207. } else if (ret > 0) {
  2208. ret = 0;
  2209. goto out_free_path;
  2210. }
  2211. extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2212. struct btrfs_file_extent_item);
  2213. if (btrfs_file_extent_generation(path->nodes[0], extent) !=
  2214. backref->generation)
  2215. goto out_free_path;
  2216. btrfs_release_path(path);
  2217. start = backref->file_pos;
  2218. if (backref->extent_offset < old->extent_offset + old->offset)
  2219. start += old->extent_offset + old->offset -
  2220. backref->extent_offset;
  2221. len = min(backref->extent_offset + backref->num_bytes,
  2222. old->extent_offset + old->offset + old->len);
  2223. len -= max(backref->extent_offset, old->extent_offset + old->offset);
  2224. ret = btrfs_drop_extents(trans, root, inode, start,
  2225. start + len, 1);
  2226. if (ret)
  2227. goto out_free_path;
  2228. again:
  2229. key.objectid = btrfs_ino(inode);
  2230. key.type = BTRFS_EXTENT_DATA_KEY;
  2231. key.offset = start;
  2232. path->leave_spinning = 1;
  2233. if (merge) {
  2234. struct btrfs_file_extent_item *fi;
  2235. u64 extent_len;
  2236. struct btrfs_key found_key;
  2237. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  2238. if (ret < 0)
  2239. goto out_free_path;
  2240. path->slots[0]--;
  2241. leaf = path->nodes[0];
  2242. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2243. fi = btrfs_item_ptr(leaf, path->slots[0],
  2244. struct btrfs_file_extent_item);
  2245. extent_len = btrfs_file_extent_num_bytes(leaf, fi);
  2246. if (extent_len + found_key.offset == start &&
  2247. relink_is_mergable(leaf, fi, new)) {
  2248. btrfs_set_file_extent_num_bytes(leaf, fi,
  2249. extent_len + len);
  2250. btrfs_mark_buffer_dirty(leaf);
  2251. inode_add_bytes(inode, len);
  2252. ret = 1;
  2253. goto out_free_path;
  2254. } else {
  2255. merge = false;
  2256. btrfs_release_path(path);
  2257. goto again;
  2258. }
  2259. }
  2260. ret = btrfs_insert_empty_item(trans, root, path, &key,
  2261. sizeof(*extent));
  2262. if (ret) {
  2263. btrfs_abort_transaction(trans, root, ret);
  2264. goto out_free_path;
  2265. }
  2266. leaf = path->nodes[0];
  2267. item = btrfs_item_ptr(leaf, path->slots[0],
  2268. struct btrfs_file_extent_item);
  2269. btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
  2270. btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
  2271. btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
  2272. btrfs_set_file_extent_num_bytes(leaf, item, len);
  2273. btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
  2274. btrfs_set_file_extent_generation(leaf, item, trans->transid);
  2275. btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
  2276. btrfs_set_file_extent_compression(leaf, item, new->compress_type);
  2277. btrfs_set_file_extent_encryption(leaf, item, 0);
  2278. btrfs_set_file_extent_other_encoding(leaf, item, 0);
  2279. btrfs_mark_buffer_dirty(leaf);
  2280. inode_add_bytes(inode, len);
  2281. btrfs_release_path(path);
  2282. ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
  2283. new->disk_len, 0,
  2284. backref->root_id, backref->inum,
  2285. new->file_pos, 0); /* start - extent_offset */
  2286. if (ret) {
  2287. btrfs_abort_transaction(trans, root, ret);
  2288. goto out_free_path;
  2289. }
  2290. ret = 1;
  2291. out_free_path:
  2292. btrfs_release_path(path);
  2293. path->leave_spinning = 0;
  2294. btrfs_end_transaction(trans, root);
  2295. out_unlock:
  2296. unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
  2297. &cached, GFP_NOFS);
  2298. iput(inode);
  2299. return ret;
  2300. }
  2301. static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
  2302. {
  2303. struct old_sa_defrag_extent *old, *tmp;
  2304. if (!new)
  2305. return;
  2306. list_for_each_entry_safe(old, tmp, &new->head, list) {
  2307. list_del(&old->list);
  2308. kfree(old);
  2309. }
  2310. kfree(new);
  2311. }
  2312. static void relink_file_extents(struct new_sa_defrag_extent *new)
  2313. {
  2314. struct btrfs_path *path;
  2315. struct sa_defrag_extent_backref *backref;
  2316. struct sa_defrag_extent_backref *prev = NULL;
  2317. struct inode *inode;
  2318. struct btrfs_root *root;
  2319. struct rb_node *node;
  2320. int ret;
  2321. inode = new->inode;
  2322. root = BTRFS_I(inode)->root;
  2323. path = btrfs_alloc_path();
  2324. if (!path)
  2325. return;
  2326. if (!record_extent_backrefs(path, new)) {
  2327. btrfs_free_path(path);
  2328. goto out;
  2329. }
  2330. btrfs_release_path(path);
  2331. while (1) {
  2332. node = rb_first(&new->root);
  2333. if (!node)
  2334. break;
  2335. rb_erase(node, &new->root);
  2336. backref = rb_entry(node, struct sa_defrag_extent_backref, node);
  2337. ret = relink_extent_backref(path, prev, backref);
  2338. WARN_ON(ret < 0);
  2339. kfree(prev);
  2340. if (ret == 1)
  2341. prev = backref;
  2342. else
  2343. prev = NULL;
  2344. cond_resched();
  2345. }
  2346. kfree(prev);
  2347. btrfs_free_path(path);
  2348. out:
  2349. free_sa_defrag_extent(new);
  2350. atomic_dec(&root->fs_info->defrag_running);
  2351. wake_up(&root->fs_info->transaction_wait);
  2352. }
  2353. static struct new_sa_defrag_extent *
  2354. record_old_file_extents(struct inode *inode,
  2355. struct btrfs_ordered_extent *ordered)
  2356. {
  2357. struct btrfs_root *root = BTRFS_I(inode)->root;
  2358. struct btrfs_path *path;
  2359. struct btrfs_key key;
  2360. struct old_sa_defrag_extent *old;
  2361. struct new_sa_defrag_extent *new;
  2362. int ret;
  2363. new = kmalloc(sizeof(*new), GFP_NOFS);
  2364. if (!new)
  2365. return NULL;
  2366. new->inode = inode;
  2367. new->file_pos = ordered->file_offset;
  2368. new->len = ordered->len;
  2369. new->bytenr = ordered->start;
  2370. new->disk_len = ordered->disk_len;
  2371. new->compress_type = ordered->compress_type;
  2372. new->root = RB_ROOT;
  2373. INIT_LIST_HEAD(&new->head);
  2374. path = btrfs_alloc_path();
  2375. if (!path)
  2376. goto out_kfree;
  2377. key.objectid = btrfs_ino(inode);
  2378. key.type = BTRFS_EXTENT_DATA_KEY;
  2379. key.offset = new->file_pos;
  2380. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2381. if (ret < 0)
  2382. goto out_free_path;
  2383. if (ret > 0 && path->slots[0] > 0)
  2384. path->slots[0]--;
  2385. /* find out all the old extents for the file range */
  2386. while (1) {
  2387. struct btrfs_file_extent_item *extent;
  2388. struct extent_buffer *l;
  2389. int slot;
  2390. u64 num_bytes;
  2391. u64 offset;
  2392. u64 end;
  2393. u64 disk_bytenr;
  2394. u64 extent_offset;
  2395. l = path->nodes[0];
  2396. slot = path->slots[0];
  2397. if (slot >= btrfs_header_nritems(l)) {
  2398. ret = btrfs_next_leaf(root, path);
  2399. if (ret < 0)
  2400. goto out_free_path;
  2401. else if (ret > 0)
  2402. break;
  2403. continue;
  2404. }
  2405. btrfs_item_key_to_cpu(l, &key, slot);
  2406. if (key.objectid != btrfs_ino(inode))
  2407. break;
  2408. if (key.type != BTRFS_EXTENT_DATA_KEY)
  2409. break;
  2410. if (key.offset >= new->file_pos + new->len)
  2411. break;
  2412. extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
  2413. num_bytes = btrfs_file_extent_num_bytes(l, extent);
  2414. if (key.offset + num_bytes < new->file_pos)
  2415. goto next;
  2416. disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
  2417. if (!disk_bytenr)
  2418. goto next;
  2419. extent_offset = btrfs_file_extent_offset(l, extent);
  2420. old = kmalloc(sizeof(*old), GFP_NOFS);
  2421. if (!old)
  2422. goto out_free_path;
  2423. offset = max(new->file_pos, key.offset);
  2424. end = min(new->file_pos + new->len, key.offset + num_bytes);
  2425. old->bytenr = disk_bytenr;
  2426. old->extent_offset = extent_offset;
  2427. old->offset = offset - key.offset;
  2428. old->len = end - offset;
  2429. old->new = new;
  2430. old->count = 0;
  2431. list_add_tail(&old->list, &new->head);
  2432. next:
  2433. path->slots[0]++;
  2434. cond_resched();
  2435. }
  2436. btrfs_free_path(path);
  2437. atomic_inc(&root->fs_info->defrag_running);
  2438. return new;
  2439. out_free_path:
  2440. btrfs_free_path(path);
  2441. out_kfree:
  2442. free_sa_defrag_extent(new);
  2443. return NULL;
  2444. }
  2445. static void btrfs_release_delalloc_bytes(struct btrfs_root *root,
  2446. u64 start, u64 len)
  2447. {
  2448. struct btrfs_block_group_cache *cache;
  2449. cache = btrfs_lookup_block_group(root->fs_info, start);
  2450. ASSERT(cache);
  2451. spin_lock(&cache->lock);
  2452. cache->delalloc_bytes -= len;
  2453. spin_unlock(&cache->lock);
  2454. btrfs_put_block_group(cache);
  2455. }
  2456. /* as ordered data IO finishes, this gets called so we can finish
  2457. * an ordered extent if the range of bytes in the file it covers are
  2458. * fully written.
  2459. */
  2460. static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
  2461. {
  2462. struct inode *inode = ordered_extent->inode;
  2463. struct btrfs_root *root = BTRFS_I(inode)->root;
  2464. struct btrfs_trans_handle *trans = NULL;
  2465. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  2466. struct extent_state *cached_state = NULL;
  2467. struct new_sa_defrag_extent *new = NULL;
  2468. int compress_type = 0;
  2469. int ret = 0;
  2470. u64 logical_len = ordered_extent->len;
  2471. bool nolock;
  2472. bool truncated = false;
  2473. nolock = btrfs_is_free_space_inode(inode);
  2474. if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
  2475. ret = -EIO;
  2476. goto out;
  2477. }
  2478. btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
  2479. ordered_extent->file_offset +
  2480. ordered_extent->len - 1);
  2481. if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
  2482. truncated = true;
  2483. logical_len = ordered_extent->truncated_len;
  2484. /* Truncated the entire extent, don't bother adding */
  2485. if (!logical_len)
  2486. goto out;
  2487. }
  2488. if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
  2489. BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
  2490. btrfs_ordered_update_i_size(inode, 0, ordered_extent);
  2491. if (nolock)
  2492. trans = btrfs_join_transaction_nolock(root);
  2493. else
  2494. trans = btrfs_join_transaction(root);
  2495. if (IS_ERR(trans)) {
  2496. ret = PTR_ERR(trans);
  2497. trans = NULL;
  2498. goto out;
  2499. }
  2500. trans->block_rsv = &root->fs_info->delalloc_block_rsv;
  2501. ret = btrfs_update_inode_fallback(trans, root, inode);
  2502. if (ret) /* -ENOMEM or corruption */
  2503. btrfs_abort_transaction(trans, root, ret);
  2504. goto out;
  2505. }
  2506. lock_extent_bits(io_tree, ordered_extent->file_offset,
  2507. ordered_extent->file_offset + ordered_extent->len - 1,
  2508. 0, &cached_state);
  2509. ret = test_range_bit(io_tree, ordered_extent->file_offset,
  2510. ordered_extent->file_offset + ordered_extent->len - 1,
  2511. EXTENT_DEFRAG, 1, cached_state);
  2512. if (ret) {
  2513. u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
  2514. if (0 && last_snapshot >= BTRFS_I(inode)->generation)
  2515. /* the inode is shared */
  2516. new = record_old_file_extents(inode, ordered_extent);
  2517. clear_extent_bit(io_tree, ordered_extent->file_offset,
  2518. ordered_extent->file_offset + ordered_extent->len - 1,
  2519. EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
  2520. }
  2521. if (nolock)
  2522. trans = btrfs_join_transaction_nolock(root);
  2523. else
  2524. trans = btrfs_join_transaction(root);
  2525. if (IS_ERR(trans)) {
  2526. ret = PTR_ERR(trans);
  2527. trans = NULL;
  2528. goto out_unlock;
  2529. }
  2530. trans->block_rsv = &root->fs_info->delalloc_block_rsv;
  2531. if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
  2532. compress_type = ordered_extent->compress_type;
  2533. if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
  2534. BUG_ON(compress_type);
  2535. ret = btrfs_mark_extent_written(trans, inode,
  2536. ordered_extent->file_offset,
  2537. ordered_extent->file_offset +
  2538. logical_len);
  2539. } else {
  2540. BUG_ON(root == root->fs_info->tree_root);
  2541. ret = insert_reserved_file_extent(trans, inode,
  2542. ordered_extent->file_offset,
  2543. ordered_extent->start,
  2544. ordered_extent->disk_len,
  2545. logical_len, logical_len,
  2546. compress_type, 0, 0,
  2547. BTRFS_FILE_EXTENT_REG);
  2548. if (!ret)
  2549. btrfs_release_delalloc_bytes(root,
  2550. ordered_extent->start,
  2551. ordered_extent->disk_len);
  2552. }
  2553. unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
  2554. ordered_extent->file_offset, ordered_extent->len,
  2555. trans->transid);
  2556. if (ret < 0) {
  2557. btrfs_abort_transaction(trans, root, ret);
  2558. goto out_unlock;
  2559. }
  2560. add_pending_csums(trans, inode, ordered_extent->file_offset,
  2561. &ordered_extent->list);
  2562. btrfs_ordered_update_i_size(inode, 0, ordered_extent);
  2563. ret = btrfs_update_inode_fallback(trans, root, inode);
  2564. if (ret) { /* -ENOMEM or corruption */
  2565. btrfs_abort_transaction(trans, root, ret);
  2566. goto out_unlock;
  2567. }
  2568. ret = 0;
  2569. out_unlock:
  2570. unlock_extent_cached(io_tree, ordered_extent->file_offset,
  2571. ordered_extent->file_offset +
  2572. ordered_extent->len - 1, &cached_state, GFP_NOFS);
  2573. out:
  2574. if (root != root->fs_info->tree_root)
  2575. btrfs_delalloc_release_metadata(inode, ordered_extent->len);
  2576. if (trans)
  2577. btrfs_end_transaction(trans, root);
  2578. if (ret || truncated) {
  2579. u64 start, end;
  2580. if (truncated)
  2581. start = ordered_extent->file_offset + logical_len;
  2582. else
  2583. start = ordered_extent->file_offset;
  2584. end = ordered_extent->file_offset + ordered_extent->len - 1;
  2585. clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
  2586. /* Drop the cache for the part of the extent we didn't write. */
  2587. btrfs_drop_extent_cache(inode, start, end, 0);
  2588. /*
  2589. * If the ordered extent had an IOERR or something else went
  2590. * wrong we need to return the space for this ordered extent
  2591. * back to the allocator. We only free the extent in the
  2592. * truncated case if we didn't write out the extent at all.
  2593. */
  2594. if ((ret || !logical_len) &&
  2595. !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
  2596. !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
  2597. btrfs_free_reserved_extent(root, ordered_extent->start,
  2598. ordered_extent->disk_len, 1);
  2599. }
  2600. /*
  2601. * This needs to be done to make sure anybody waiting knows we are done
  2602. * updating everything for this ordered extent.
  2603. */
  2604. btrfs_remove_ordered_extent(inode, ordered_extent);
  2605. /* for snapshot-aware defrag */
  2606. if (new) {
  2607. if (ret) {
  2608. free_sa_defrag_extent(new);
  2609. atomic_dec(&root->fs_info->defrag_running);
  2610. } else {
  2611. relink_file_extents(new);
  2612. }
  2613. }
  2614. /* once for us */
  2615. btrfs_put_ordered_extent(ordered_extent);
  2616. /* once for the tree */
  2617. btrfs_put_ordered_extent(ordered_extent);
  2618. return ret;
  2619. }
  2620. static void finish_ordered_fn(struct btrfs_work *work)
  2621. {
  2622. struct btrfs_ordered_extent *ordered_extent;
  2623. ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
  2624. btrfs_finish_ordered_io(ordered_extent);
  2625. }
  2626. static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
  2627. struct extent_state *state, int uptodate)
  2628. {
  2629. struct inode *inode = page->mapping->host;
  2630. struct btrfs_root *root = BTRFS_I(inode)->root;
  2631. struct btrfs_ordered_extent *ordered_extent = NULL;
  2632. struct btrfs_workqueue *wq;
  2633. btrfs_work_func_t func;
  2634. trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
  2635. ClearPagePrivate2(page);
  2636. if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
  2637. end - start + 1, uptodate))
  2638. return 0;
  2639. if (btrfs_is_free_space_inode(inode)) {
  2640. wq = root->fs_info->endio_freespace_worker;
  2641. func = btrfs_freespace_write_helper;
  2642. } else {
  2643. wq = root->fs_info->endio_write_workers;
  2644. func = btrfs_endio_write_helper;
  2645. }
  2646. btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
  2647. NULL);
  2648. btrfs_queue_work(wq, &ordered_extent->work);
  2649. return 0;
  2650. }
  2651. static int __readpage_endio_check(struct inode *inode,
  2652. struct btrfs_io_bio *io_bio,
  2653. int icsum, struct page *page,
  2654. int pgoff, u64 start, size_t len)
  2655. {
  2656. char *kaddr;
  2657. u32 csum_expected;
  2658. u32 csum = ~(u32)0;
  2659. static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
  2660. DEFAULT_RATELIMIT_BURST);
  2661. csum_expected = *(((u32 *)io_bio->csum) + icsum);
  2662. kaddr = kmap_atomic(page);
  2663. csum = btrfs_csum_data(kaddr + pgoff, csum, len);
  2664. btrfs_csum_final(csum, (char *)&csum);
  2665. if (csum != csum_expected)
  2666. goto zeroit;
  2667. kunmap_atomic(kaddr);
  2668. return 0;
  2669. zeroit:
  2670. if (__ratelimit(&_rs))
  2671. btrfs_warn(BTRFS_I(inode)->root->fs_info,
  2672. "csum failed ino %llu off %llu csum %u expected csum %u",
  2673. btrfs_ino(inode), start, csum, csum_expected);
  2674. memset(kaddr + pgoff, 1, len);
  2675. flush_dcache_page(page);
  2676. kunmap_atomic(kaddr);
  2677. if (csum_expected == 0)
  2678. return 0;
  2679. return -EIO;
  2680. }
  2681. /*
  2682. * when reads are done, we need to check csums to verify the data is correct
  2683. * if there's a match, we allow the bio to finish. If not, the code in
  2684. * extent_io.c will try to find good copies for us.
  2685. */
  2686. static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
  2687. u64 phy_offset, struct page *page,
  2688. u64 start, u64 end, int mirror)
  2689. {
  2690. size_t offset = start - page_offset(page);
  2691. struct inode *inode = page->mapping->host;
  2692. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  2693. struct btrfs_root *root = BTRFS_I(inode)->root;
  2694. if (PageChecked(page)) {
  2695. ClearPageChecked(page);
  2696. return 0;
  2697. }
  2698. if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
  2699. return 0;
  2700. if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
  2701. test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
  2702. clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
  2703. GFP_NOFS);
  2704. return 0;
  2705. }
  2706. phy_offset >>= inode->i_sb->s_blocksize_bits;
  2707. return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
  2708. start, (size_t)(end - start + 1));
  2709. }
  2710. struct delayed_iput {
  2711. struct list_head list;
  2712. struct inode *inode;
  2713. };
  2714. /* JDM: If this is fs-wide, why can't we add a pointer to
  2715. * btrfs_inode instead and avoid the allocation? */
  2716. void btrfs_add_delayed_iput(struct inode *inode)
  2717. {
  2718. struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
  2719. struct delayed_iput *delayed;
  2720. if (atomic_add_unless(&inode->i_count, -1, 1))
  2721. return;
  2722. delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
  2723. delayed->inode = inode;
  2724. spin_lock(&fs_info->delayed_iput_lock);
  2725. list_add_tail(&delayed->list, &fs_info->delayed_iputs);
  2726. spin_unlock(&fs_info->delayed_iput_lock);
  2727. }
  2728. void btrfs_run_delayed_iputs(struct btrfs_root *root)
  2729. {
  2730. LIST_HEAD(list);
  2731. struct btrfs_fs_info *fs_info = root->fs_info;
  2732. struct delayed_iput *delayed;
  2733. int empty;
  2734. spin_lock(&fs_info->delayed_iput_lock);
  2735. empty = list_empty(&fs_info->delayed_iputs);
  2736. spin_unlock(&fs_info->delayed_iput_lock);
  2737. if (empty)
  2738. return;
  2739. down_read(&fs_info->delayed_iput_sem);
  2740. spin_lock(&fs_info->delayed_iput_lock);
  2741. list_splice_init(&fs_info->delayed_iputs, &list);
  2742. spin_unlock(&fs_info->delayed_iput_lock);
  2743. while (!list_empty(&list)) {
  2744. delayed = list_entry(list.next, struct delayed_iput, list);
  2745. list_del(&delayed->list);
  2746. iput(delayed->inode);
  2747. kfree(delayed);
  2748. }
  2749. up_read(&root->fs_info->delayed_iput_sem);
  2750. }
  2751. /*
  2752. * This is called in transaction commit time. If there are no orphan
  2753. * files in the subvolume, it removes orphan item and frees block_rsv
  2754. * structure.
  2755. */
  2756. void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
  2757. struct btrfs_root *root)
  2758. {
  2759. struct btrfs_block_rsv *block_rsv;
  2760. int ret;
  2761. if (atomic_read(&root->orphan_inodes) ||
  2762. root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
  2763. return;
  2764. spin_lock(&root->orphan_lock);
  2765. if (atomic_read(&root->orphan_inodes)) {
  2766. spin_unlock(&root->orphan_lock);
  2767. return;
  2768. }
  2769. if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
  2770. spin_unlock(&root->orphan_lock);
  2771. return;
  2772. }
  2773. block_rsv = root->orphan_block_rsv;
  2774. root->orphan_block_rsv = NULL;
  2775. spin_unlock(&root->orphan_lock);
  2776. if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
  2777. btrfs_root_refs(&root->root_item) > 0) {
  2778. ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
  2779. root->root_key.objectid);
  2780. if (ret)
  2781. btrfs_abort_transaction(trans, root, ret);
  2782. else
  2783. clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
  2784. &root->state);
  2785. }
  2786. if (block_rsv) {
  2787. WARN_ON(block_rsv->size > 0);
  2788. btrfs_free_block_rsv(root, block_rsv);
  2789. }
  2790. }
  2791. /*
  2792. * This creates an orphan entry for the given inode in case something goes
  2793. * wrong in the middle of an unlink/truncate.
  2794. *
  2795. * NOTE: caller of this function should reserve 5 units of metadata for
  2796. * this function.
  2797. */
  2798. int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
  2799. {
  2800. struct btrfs_root *root = BTRFS_I(inode)->root;
  2801. struct btrfs_block_rsv *block_rsv = NULL;
  2802. int reserve = 0;
  2803. int insert = 0;
  2804. int ret;
  2805. if (!root->orphan_block_rsv) {
  2806. block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
  2807. if (!block_rsv)
  2808. return -ENOMEM;
  2809. }
  2810. spin_lock(&root->orphan_lock);
  2811. if (!root->orphan_block_rsv) {
  2812. root->orphan_block_rsv = block_rsv;
  2813. } else if (block_rsv) {
  2814. btrfs_free_block_rsv(root, block_rsv);
  2815. block_rsv = NULL;
  2816. }
  2817. if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
  2818. &BTRFS_I(inode)->runtime_flags)) {
  2819. #if 0
  2820. /*
  2821. * For proper ENOSPC handling, we should do orphan
  2822. * cleanup when mounting. But this introduces backward
  2823. * compatibility issue.
  2824. */
  2825. if (!xchg(&root->orphan_item_inserted, 1))
  2826. insert = 2;
  2827. else
  2828. insert = 1;
  2829. #endif
  2830. insert = 1;
  2831. atomic_inc(&root->orphan_inodes);
  2832. }
  2833. if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
  2834. &BTRFS_I(inode)->runtime_flags))
  2835. reserve = 1;
  2836. spin_unlock(&root->orphan_lock);
  2837. /* grab metadata reservation from transaction handle */
  2838. if (reserve) {
  2839. ret = btrfs_orphan_reserve_metadata(trans, inode);
  2840. BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
  2841. }
  2842. /* insert an orphan item to track this unlinked/truncated file */
  2843. if (insert >= 1) {
  2844. ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
  2845. if (ret) {
  2846. atomic_dec(&root->orphan_inodes);
  2847. if (reserve) {
  2848. clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
  2849. &BTRFS_I(inode)->runtime_flags);
  2850. btrfs_orphan_release_metadata(inode);
  2851. }
  2852. if (ret != -EEXIST) {
  2853. clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
  2854. &BTRFS_I(inode)->runtime_flags);
  2855. btrfs_abort_transaction(trans, root, ret);
  2856. return ret;
  2857. }
  2858. }
  2859. ret = 0;
  2860. }
  2861. /* insert an orphan item to track subvolume contains orphan files */
  2862. if (insert >= 2) {
  2863. ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
  2864. root->root_key.objectid);
  2865. if (ret && ret != -EEXIST) {
  2866. btrfs_abort_transaction(trans, root, ret);
  2867. return ret;
  2868. }
  2869. }
  2870. return 0;
  2871. }
  2872. /*
  2873. * We have done the truncate/delete so we can go ahead and remove the orphan
  2874. * item for this particular inode.
  2875. */
  2876. static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
  2877. struct inode *inode)
  2878. {
  2879. struct btrfs_root *root = BTRFS_I(inode)->root;
  2880. int delete_item = 0;
  2881. int release_rsv = 0;
  2882. int ret = 0;
  2883. spin_lock(&root->orphan_lock);
  2884. if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
  2885. &BTRFS_I(inode)->runtime_flags))
  2886. delete_item = 1;
  2887. if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
  2888. &BTRFS_I(inode)->runtime_flags))
  2889. release_rsv = 1;
  2890. spin_unlock(&root->orphan_lock);
  2891. if (delete_item) {
  2892. atomic_dec(&root->orphan_inodes);
  2893. if (trans)
  2894. ret = btrfs_del_orphan_item(trans, root,
  2895. btrfs_ino(inode));
  2896. }
  2897. if (release_rsv)
  2898. btrfs_orphan_release_metadata(inode);
  2899. return ret;
  2900. }
  2901. /*
  2902. * this cleans up any orphans that may be left on the list from the last use
  2903. * of this root.
  2904. */
  2905. int btrfs_orphan_cleanup(struct btrfs_root *root)
  2906. {
  2907. struct btrfs_path *path;
  2908. struct extent_buffer *leaf;
  2909. struct btrfs_key key, found_key;
  2910. struct btrfs_trans_handle *trans;
  2911. struct inode *inode;
  2912. u64 last_objectid = 0;
  2913. int ret = 0, nr_unlink = 0, nr_truncate = 0;
  2914. if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
  2915. return 0;
  2916. path = btrfs_alloc_path();
  2917. if (!path) {
  2918. ret = -ENOMEM;
  2919. goto out;
  2920. }
  2921. path->reada = -1;
  2922. key.objectid = BTRFS_ORPHAN_OBJECTID;
  2923. key.type = BTRFS_ORPHAN_ITEM_KEY;
  2924. key.offset = (u64)-1;
  2925. while (1) {
  2926. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2927. if (ret < 0)
  2928. goto out;
  2929. /*
  2930. * if ret == 0 means we found what we were searching for, which
  2931. * is weird, but possible, so only screw with path if we didn't
  2932. * find the key and see if we have stuff that matches
  2933. */
  2934. if (ret > 0) {
  2935. ret = 0;
  2936. if (path->slots[0] == 0)
  2937. break;
  2938. path->slots[0]--;
  2939. }
  2940. /* pull out the item */
  2941. leaf = path->nodes[0];
  2942. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2943. /* make sure the item matches what we want */
  2944. if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
  2945. break;
  2946. if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
  2947. break;
  2948. /* release the path since we're done with it */
  2949. btrfs_release_path(path);
  2950. /*
  2951. * this is where we are basically btrfs_lookup, without the
  2952. * crossing root thing. we store the inode number in the
  2953. * offset of the orphan item.
  2954. */
  2955. if (found_key.offset == last_objectid) {
  2956. btrfs_err(root->fs_info,
  2957. "Error removing orphan entry, stopping orphan cleanup");
  2958. ret = -EINVAL;
  2959. goto out;
  2960. }
  2961. last_objectid = found_key.offset;
  2962. found_key.objectid = found_key.offset;
  2963. found_key.type = BTRFS_INODE_ITEM_KEY;
  2964. found_key.offset = 0;
  2965. inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
  2966. ret = PTR_ERR_OR_ZERO(inode);
  2967. if (ret && ret != -ESTALE)
  2968. goto out;
  2969. if (ret == -ESTALE && root == root->fs_info->tree_root) {
  2970. struct btrfs_root *dead_root;
  2971. struct btrfs_fs_info *fs_info = root->fs_info;
  2972. int is_dead_root = 0;
  2973. /*
  2974. * this is an orphan in the tree root. Currently these
  2975. * could come from 2 sources:
  2976. * a) a snapshot deletion in progress
  2977. * b) a free space cache inode
  2978. * We need to distinguish those two, as the snapshot
  2979. * orphan must not get deleted.
  2980. * find_dead_roots already ran before us, so if this
  2981. * is a snapshot deletion, we should find the root
  2982. * in the dead_roots list
  2983. */
  2984. spin_lock(&fs_info->trans_lock);
  2985. list_for_each_entry(dead_root, &fs_info->dead_roots,
  2986. root_list) {
  2987. if (dead_root->root_key.objectid ==
  2988. found_key.objectid) {
  2989. is_dead_root = 1;
  2990. break;
  2991. }
  2992. }
  2993. spin_unlock(&fs_info->trans_lock);
  2994. if (is_dead_root) {
  2995. /* prevent this orphan from being found again */
  2996. key.offset = found_key.objectid - 1;
  2997. continue;
  2998. }
  2999. }
  3000. /*
  3001. * Inode is already gone but the orphan item is still there,
  3002. * kill the orphan item.
  3003. */
  3004. if (ret == -ESTALE) {
  3005. trans = btrfs_start_transaction(root, 1);
  3006. if (IS_ERR(trans)) {
  3007. ret = PTR_ERR(trans);
  3008. goto out;
  3009. }
  3010. btrfs_debug(root->fs_info, "auto deleting %Lu",
  3011. found_key.objectid);
  3012. ret = btrfs_del_orphan_item(trans, root,
  3013. found_key.objectid);
  3014. btrfs_end_transaction(trans, root);
  3015. if (ret)
  3016. goto out;
  3017. continue;
  3018. }
  3019. /*
  3020. * add this inode to the orphan list so btrfs_orphan_del does
  3021. * the proper thing when we hit it
  3022. */
  3023. set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
  3024. &BTRFS_I(inode)->runtime_flags);
  3025. atomic_inc(&root->orphan_inodes);
  3026. /* if we have links, this was a truncate, lets do that */
  3027. if (inode->i_nlink) {
  3028. if (WARN_ON(!S_ISREG(inode->i_mode))) {
  3029. iput(inode);
  3030. continue;
  3031. }
  3032. nr_truncate++;
  3033. /* 1 for the orphan item deletion. */
  3034. trans = btrfs_start_transaction(root, 1);
  3035. if (IS_ERR(trans)) {
  3036. iput(inode);
  3037. ret = PTR_ERR(trans);
  3038. goto out;
  3039. }
  3040. ret = btrfs_orphan_add(trans, inode);
  3041. btrfs_end_transaction(trans, root);
  3042. if (ret) {
  3043. iput(inode);
  3044. goto out;
  3045. }
  3046. ret = btrfs_truncate(inode);
  3047. if (ret)
  3048. btrfs_orphan_del(NULL, inode);
  3049. } else {
  3050. nr_unlink++;
  3051. }
  3052. /* this will do delete_inode and everything for us */
  3053. iput(inode);
  3054. if (ret)
  3055. goto out;
  3056. }
  3057. /* release the path since we're done with it */
  3058. btrfs_release_path(path);
  3059. root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
  3060. if (root->orphan_block_rsv)
  3061. btrfs_block_rsv_release(root, root->orphan_block_rsv,
  3062. (u64)-1);
  3063. if (root->orphan_block_rsv ||
  3064. test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
  3065. trans = btrfs_join_transaction(root);
  3066. if (!IS_ERR(trans))
  3067. btrfs_end_transaction(trans, root);
  3068. }
  3069. if (nr_unlink)
  3070. btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
  3071. if (nr_truncate)
  3072. btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
  3073. out:
  3074. if (ret)
  3075. btrfs_err(root->fs_info,
  3076. "could not do orphan cleanup %d", ret);
  3077. btrfs_free_path(path);
  3078. return ret;
  3079. }
  3080. /*
  3081. * very simple check to peek ahead in the leaf looking for xattrs. If we
  3082. * don't find any xattrs, we know there can't be any acls.
  3083. *
  3084. * slot is the slot the inode is in, objectid is the objectid of the inode
  3085. */
  3086. static noinline int acls_after_inode_item(struct extent_buffer *leaf,
  3087. int slot, u64 objectid,
  3088. int *first_xattr_slot)
  3089. {
  3090. u32 nritems = btrfs_header_nritems(leaf);
  3091. struct btrfs_key found_key;
  3092. static u64 xattr_access = 0;
  3093. static u64 xattr_default = 0;
  3094. int scanned = 0;
  3095. if (!xattr_access) {
  3096. xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
  3097. strlen(POSIX_ACL_XATTR_ACCESS));
  3098. xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
  3099. strlen(POSIX_ACL_XATTR_DEFAULT));
  3100. }
  3101. slot++;
  3102. *first_xattr_slot = -1;
  3103. while (slot < nritems) {
  3104. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  3105. /* we found a different objectid, there must not be acls */
  3106. if (found_key.objectid != objectid)
  3107. return 0;
  3108. /* we found an xattr, assume we've got an acl */
  3109. if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
  3110. if (*first_xattr_slot == -1)
  3111. *first_xattr_slot = slot;
  3112. if (found_key.offset == xattr_access ||
  3113. found_key.offset == xattr_default)
  3114. return 1;
  3115. }
  3116. /*
  3117. * we found a key greater than an xattr key, there can't
  3118. * be any acls later on
  3119. */
  3120. if (found_key.type > BTRFS_XATTR_ITEM_KEY)
  3121. return 0;
  3122. slot++;
  3123. scanned++;
  3124. /*
  3125. * it goes inode, inode backrefs, xattrs, extents,
  3126. * so if there are a ton of hard links to an inode there can
  3127. * be a lot of backrefs. Don't waste time searching too hard,
  3128. * this is just an optimization
  3129. */
  3130. if (scanned >= 8)
  3131. break;
  3132. }
  3133. /* we hit the end of the leaf before we found an xattr or
  3134. * something larger than an xattr. We have to assume the inode
  3135. * has acls
  3136. */
  3137. if (*first_xattr_slot == -1)
  3138. *first_xattr_slot = slot;
  3139. return 1;
  3140. }
  3141. /*
  3142. * read an inode from the btree into the in-memory inode
  3143. */
  3144. static void btrfs_read_locked_inode(struct inode *inode)
  3145. {
  3146. struct btrfs_path *path;
  3147. struct extent_buffer *leaf;
  3148. struct btrfs_inode_item *inode_item;
  3149. struct btrfs_root *root = BTRFS_I(inode)->root;
  3150. struct btrfs_key location;
  3151. unsigned long ptr;
  3152. int maybe_acls;
  3153. u32 rdev;
  3154. int ret;
  3155. bool filled = false;
  3156. int first_xattr_slot;
  3157. ret = btrfs_fill_inode(inode, &rdev);
  3158. if (!ret)
  3159. filled = true;
  3160. path = btrfs_alloc_path();
  3161. if (!path)
  3162. goto make_bad;
  3163. memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
  3164. ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
  3165. if (ret)
  3166. goto make_bad;
  3167. leaf = path->nodes[0];
  3168. if (filled)
  3169. goto cache_index;
  3170. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  3171. struct btrfs_inode_item);
  3172. inode->i_mode = btrfs_inode_mode(leaf, inode_item);
  3173. set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
  3174. i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
  3175. i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
  3176. btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
  3177. inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
  3178. inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
  3179. inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
  3180. inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
  3181. inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
  3182. inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
  3183. BTRFS_I(inode)->i_otime.tv_sec =
  3184. btrfs_timespec_sec(leaf, &inode_item->otime);
  3185. BTRFS_I(inode)->i_otime.tv_nsec =
  3186. btrfs_timespec_nsec(leaf, &inode_item->otime);
  3187. inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
  3188. BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
  3189. BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
  3190. inode->i_version = btrfs_inode_sequence(leaf, inode_item);
  3191. inode->i_generation = BTRFS_I(inode)->generation;
  3192. inode->i_rdev = 0;
  3193. rdev = btrfs_inode_rdev(leaf, inode_item);
  3194. BTRFS_I(inode)->index_cnt = (u64)-1;
  3195. BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
  3196. cache_index:
  3197. /*
  3198. * If we were modified in the current generation and evicted from memory
  3199. * and then re-read we need to do a full sync since we don't have any
  3200. * idea about which extents were modified before we were evicted from
  3201. * cache.
  3202. *
  3203. * This is required for both inode re-read from disk and delayed inode
  3204. * in delayed_nodes_tree.
  3205. */
  3206. if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
  3207. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  3208. &BTRFS_I(inode)->runtime_flags);
  3209. /*
  3210. * We don't persist the id of the transaction where an unlink operation
  3211. * against the inode was last made. So here we assume the inode might
  3212. * have been evicted, and therefore the exact value of last_unlink_trans
  3213. * lost, and set it to last_trans to avoid metadata inconsistencies
  3214. * between the inode and its parent if the inode is fsync'ed and the log
  3215. * replayed. For example, in the scenario:
  3216. *
  3217. * touch mydir/foo
  3218. * ln mydir/foo mydir/bar
  3219. * sync
  3220. * unlink mydir/bar
  3221. * echo 2 > /proc/sys/vm/drop_caches # evicts inode
  3222. * xfs_io -c fsync mydir/foo
  3223. * <power failure>
  3224. * mount fs, triggers fsync log replay
  3225. *
  3226. * We must make sure that when we fsync our inode foo we also log its
  3227. * parent inode, otherwise after log replay the parent still has the
  3228. * dentry with the "bar" name but our inode foo has a link count of 1
  3229. * and doesn't have an inode ref with the name "bar" anymore.
  3230. *
  3231. * Setting last_unlink_trans to last_trans is a pessimistic approach,
  3232. * but it guarantees correctness at the expense of ocassional full
  3233. * transaction commits on fsync if our inode is a directory, or if our
  3234. * inode is not a directory, logging its parent unnecessarily.
  3235. */
  3236. BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
  3237. path->slots[0]++;
  3238. if (inode->i_nlink != 1 ||
  3239. path->slots[0] >= btrfs_header_nritems(leaf))
  3240. goto cache_acl;
  3241. btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
  3242. if (location.objectid != btrfs_ino(inode))
  3243. goto cache_acl;
  3244. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  3245. if (location.type == BTRFS_INODE_REF_KEY) {
  3246. struct btrfs_inode_ref *ref;
  3247. ref = (struct btrfs_inode_ref *)ptr;
  3248. BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
  3249. } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
  3250. struct btrfs_inode_extref *extref;
  3251. extref = (struct btrfs_inode_extref *)ptr;
  3252. BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
  3253. extref);
  3254. }
  3255. cache_acl:
  3256. /*
  3257. * try to precache a NULL acl entry for files that don't have
  3258. * any xattrs or acls
  3259. */
  3260. maybe_acls = acls_after_inode_item(leaf, path->slots[0],
  3261. btrfs_ino(inode), &first_xattr_slot);
  3262. if (first_xattr_slot != -1) {
  3263. path->slots[0] = first_xattr_slot;
  3264. ret = btrfs_load_inode_props(inode, path);
  3265. if (ret)
  3266. btrfs_err(root->fs_info,
  3267. "error loading props for ino %llu (root %llu): %d",
  3268. btrfs_ino(inode),
  3269. root->root_key.objectid, ret);
  3270. }
  3271. btrfs_free_path(path);
  3272. if (!maybe_acls)
  3273. cache_no_acl(inode);
  3274. switch (inode->i_mode & S_IFMT) {
  3275. case S_IFREG:
  3276. inode->i_mapping->a_ops = &btrfs_aops;
  3277. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  3278. inode->i_fop = &btrfs_file_operations;
  3279. inode->i_op = &btrfs_file_inode_operations;
  3280. break;
  3281. case S_IFDIR:
  3282. inode->i_fop = &btrfs_dir_file_operations;
  3283. if (root == root->fs_info->tree_root)
  3284. inode->i_op = &btrfs_dir_ro_inode_operations;
  3285. else
  3286. inode->i_op = &btrfs_dir_inode_operations;
  3287. break;
  3288. case S_IFLNK:
  3289. inode->i_op = &btrfs_symlink_inode_operations;
  3290. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  3291. break;
  3292. default:
  3293. inode->i_op = &btrfs_special_inode_operations;
  3294. init_special_inode(inode, inode->i_mode, rdev);
  3295. break;
  3296. }
  3297. btrfs_update_iflags(inode);
  3298. return;
  3299. make_bad:
  3300. btrfs_free_path(path);
  3301. make_bad_inode(inode);
  3302. }
  3303. /*
  3304. * given a leaf and an inode, copy the inode fields into the leaf
  3305. */
  3306. static void fill_inode_item(struct btrfs_trans_handle *trans,
  3307. struct extent_buffer *leaf,
  3308. struct btrfs_inode_item *item,
  3309. struct inode *inode)
  3310. {
  3311. struct btrfs_map_token token;
  3312. btrfs_init_map_token(&token);
  3313. btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
  3314. btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
  3315. btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
  3316. &token);
  3317. btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
  3318. btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
  3319. btrfs_set_token_timespec_sec(leaf, &item->atime,
  3320. inode->i_atime.tv_sec, &token);
  3321. btrfs_set_token_timespec_nsec(leaf, &item->atime,
  3322. inode->i_atime.tv_nsec, &token);
  3323. btrfs_set_token_timespec_sec(leaf, &item->mtime,
  3324. inode->i_mtime.tv_sec, &token);
  3325. btrfs_set_token_timespec_nsec(leaf, &item->mtime,
  3326. inode->i_mtime.tv_nsec, &token);
  3327. btrfs_set_token_timespec_sec(leaf, &item->ctime,
  3328. inode->i_ctime.tv_sec, &token);
  3329. btrfs_set_token_timespec_nsec(leaf, &item->ctime,
  3330. inode->i_ctime.tv_nsec, &token);
  3331. btrfs_set_token_timespec_sec(leaf, &item->otime,
  3332. BTRFS_I(inode)->i_otime.tv_sec, &token);
  3333. btrfs_set_token_timespec_nsec(leaf, &item->otime,
  3334. BTRFS_I(inode)->i_otime.tv_nsec, &token);
  3335. btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
  3336. &token);
  3337. btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
  3338. &token);
  3339. btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
  3340. btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
  3341. btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
  3342. btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
  3343. btrfs_set_token_inode_block_group(leaf, item, 0, &token);
  3344. }
  3345. /*
  3346. * copy everything in the in-memory inode into the btree.
  3347. */
  3348. static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
  3349. struct btrfs_root *root, struct inode *inode)
  3350. {
  3351. struct btrfs_inode_item *inode_item;
  3352. struct btrfs_path *path;
  3353. struct extent_buffer *leaf;
  3354. int ret;
  3355. path = btrfs_alloc_path();
  3356. if (!path)
  3357. return -ENOMEM;
  3358. path->leave_spinning = 1;
  3359. ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
  3360. 1);
  3361. if (ret) {
  3362. if (ret > 0)
  3363. ret = -ENOENT;
  3364. goto failed;
  3365. }
  3366. leaf = path->nodes[0];
  3367. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  3368. struct btrfs_inode_item);
  3369. fill_inode_item(trans, leaf, inode_item, inode);
  3370. btrfs_mark_buffer_dirty(leaf);
  3371. btrfs_set_inode_last_trans(trans, inode);
  3372. ret = 0;
  3373. failed:
  3374. btrfs_free_path(path);
  3375. return ret;
  3376. }
  3377. /*
  3378. * copy everything in the in-memory inode into the btree.
  3379. */
  3380. noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
  3381. struct btrfs_root *root, struct inode *inode)
  3382. {
  3383. int ret;
  3384. /*
  3385. * If the inode is a free space inode, we can deadlock during commit
  3386. * if we put it into the delayed code.
  3387. *
  3388. * The data relocation inode should also be directly updated
  3389. * without delay
  3390. */
  3391. if (!btrfs_is_free_space_inode(inode)
  3392. && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
  3393. && !root->fs_info->log_root_recovering) {
  3394. btrfs_update_root_times(trans, root);
  3395. ret = btrfs_delayed_update_inode(trans, root, inode);
  3396. if (!ret)
  3397. btrfs_set_inode_last_trans(trans, inode);
  3398. return ret;
  3399. }
  3400. return btrfs_update_inode_item(trans, root, inode);
  3401. }
  3402. noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
  3403. struct btrfs_root *root,
  3404. struct inode *inode)
  3405. {
  3406. int ret;
  3407. ret = btrfs_update_inode(trans, root, inode);
  3408. if (ret == -ENOSPC)
  3409. return btrfs_update_inode_item(trans, root, inode);
  3410. return ret;
  3411. }
  3412. /*
  3413. * unlink helper that gets used here in inode.c and in the tree logging
  3414. * recovery code. It remove a link in a directory with a given name, and
  3415. * also drops the back refs in the inode to the directory
  3416. */
  3417. static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
  3418. struct btrfs_root *root,
  3419. struct inode *dir, struct inode *inode,
  3420. const char *name, int name_len)
  3421. {
  3422. struct btrfs_path *path;
  3423. int ret = 0;
  3424. struct extent_buffer *leaf;
  3425. struct btrfs_dir_item *di;
  3426. struct btrfs_key key;
  3427. u64 index;
  3428. u64 ino = btrfs_ino(inode);
  3429. u64 dir_ino = btrfs_ino(dir);
  3430. path = btrfs_alloc_path();
  3431. if (!path) {
  3432. ret = -ENOMEM;
  3433. goto out;
  3434. }
  3435. path->leave_spinning = 1;
  3436. di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
  3437. name, name_len, -1);
  3438. if (IS_ERR(di)) {
  3439. ret = PTR_ERR(di);
  3440. goto err;
  3441. }
  3442. if (!di) {
  3443. ret = -ENOENT;
  3444. goto err;
  3445. }
  3446. leaf = path->nodes[0];
  3447. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  3448. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  3449. if (ret)
  3450. goto err;
  3451. btrfs_release_path(path);
  3452. /*
  3453. * If we don't have dir index, we have to get it by looking up
  3454. * the inode ref, since we get the inode ref, remove it directly,
  3455. * it is unnecessary to do delayed deletion.
  3456. *
  3457. * But if we have dir index, needn't search inode ref to get it.
  3458. * Since the inode ref is close to the inode item, it is better
  3459. * that we delay to delete it, and just do this deletion when
  3460. * we update the inode item.
  3461. */
  3462. if (BTRFS_I(inode)->dir_index) {
  3463. ret = btrfs_delayed_delete_inode_ref(inode);
  3464. if (!ret) {
  3465. index = BTRFS_I(inode)->dir_index;
  3466. goto skip_backref;
  3467. }
  3468. }
  3469. ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
  3470. dir_ino, &index);
  3471. if (ret) {
  3472. btrfs_info(root->fs_info,
  3473. "failed to delete reference to %.*s, inode %llu parent %llu",
  3474. name_len, name, ino, dir_ino);
  3475. btrfs_abort_transaction(trans, root, ret);
  3476. goto err;
  3477. }
  3478. skip_backref:
  3479. ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
  3480. if (ret) {
  3481. btrfs_abort_transaction(trans, root, ret);
  3482. goto err;
  3483. }
  3484. ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
  3485. inode, dir_ino);
  3486. if (ret != 0 && ret != -ENOENT) {
  3487. btrfs_abort_transaction(trans, root, ret);
  3488. goto err;
  3489. }
  3490. ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
  3491. dir, index);
  3492. if (ret == -ENOENT)
  3493. ret = 0;
  3494. else if (ret)
  3495. btrfs_abort_transaction(trans, root, ret);
  3496. err:
  3497. btrfs_free_path(path);
  3498. if (ret)
  3499. goto out;
  3500. btrfs_i_size_write(dir, dir->i_size - name_len * 2);
  3501. inode_inc_iversion(inode);
  3502. inode_inc_iversion(dir);
  3503. inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  3504. ret = btrfs_update_inode(trans, root, dir);
  3505. out:
  3506. return ret;
  3507. }
  3508. int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
  3509. struct btrfs_root *root,
  3510. struct inode *dir, struct inode *inode,
  3511. const char *name, int name_len)
  3512. {
  3513. int ret;
  3514. ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
  3515. if (!ret) {
  3516. drop_nlink(inode);
  3517. ret = btrfs_update_inode(trans, root, inode);
  3518. }
  3519. return ret;
  3520. }
  3521. /*
  3522. * helper to start transaction for unlink and rmdir.
  3523. *
  3524. * unlink and rmdir are special in btrfs, they do not always free space, so
  3525. * if we cannot make our reservations the normal way try and see if there is
  3526. * plenty of slack room in the global reserve to migrate, otherwise we cannot
  3527. * allow the unlink to occur.
  3528. */
  3529. static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
  3530. {
  3531. struct btrfs_trans_handle *trans;
  3532. struct btrfs_root *root = BTRFS_I(dir)->root;
  3533. int ret;
  3534. /*
  3535. * 1 for the possible orphan item
  3536. * 1 for the dir item
  3537. * 1 for the dir index
  3538. * 1 for the inode ref
  3539. * 1 for the inode
  3540. */
  3541. trans = btrfs_start_transaction(root, 5);
  3542. if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
  3543. return trans;
  3544. if (PTR_ERR(trans) == -ENOSPC) {
  3545. u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
  3546. trans = btrfs_start_transaction(root, 0);
  3547. if (IS_ERR(trans))
  3548. return trans;
  3549. ret = btrfs_cond_migrate_bytes(root->fs_info,
  3550. &root->fs_info->trans_block_rsv,
  3551. num_bytes, 5);
  3552. if (ret) {
  3553. btrfs_end_transaction(trans, root);
  3554. return ERR_PTR(ret);
  3555. }
  3556. trans->block_rsv = &root->fs_info->trans_block_rsv;
  3557. trans->bytes_reserved = num_bytes;
  3558. }
  3559. return trans;
  3560. }
  3561. static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
  3562. {
  3563. struct btrfs_root *root = BTRFS_I(dir)->root;
  3564. struct btrfs_trans_handle *trans;
  3565. struct inode *inode = d_inode(dentry);
  3566. int ret;
  3567. trans = __unlink_start_trans(dir);
  3568. if (IS_ERR(trans))
  3569. return PTR_ERR(trans);
  3570. btrfs_record_unlink_dir(trans, dir, d_inode(dentry), 0);
  3571. ret = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
  3572. dentry->d_name.name, dentry->d_name.len);
  3573. if (ret)
  3574. goto out;
  3575. if (inode->i_nlink == 0) {
  3576. ret = btrfs_orphan_add(trans, inode);
  3577. if (ret)
  3578. goto out;
  3579. }
  3580. out:
  3581. btrfs_end_transaction(trans, root);
  3582. btrfs_btree_balance_dirty(root);
  3583. return ret;
  3584. }
  3585. int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
  3586. struct btrfs_root *root,
  3587. struct inode *dir, u64 objectid,
  3588. const char *name, int name_len)
  3589. {
  3590. struct btrfs_path *path;
  3591. struct extent_buffer *leaf;
  3592. struct btrfs_dir_item *di;
  3593. struct btrfs_key key;
  3594. u64 index;
  3595. int ret;
  3596. u64 dir_ino = btrfs_ino(dir);
  3597. path = btrfs_alloc_path();
  3598. if (!path)
  3599. return -ENOMEM;
  3600. di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
  3601. name, name_len, -1);
  3602. if (IS_ERR_OR_NULL(di)) {
  3603. if (!di)
  3604. ret = -ENOENT;
  3605. else
  3606. ret = PTR_ERR(di);
  3607. goto out;
  3608. }
  3609. leaf = path->nodes[0];
  3610. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  3611. WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
  3612. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  3613. if (ret) {
  3614. btrfs_abort_transaction(trans, root, ret);
  3615. goto out;
  3616. }
  3617. btrfs_release_path(path);
  3618. ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
  3619. objectid, root->root_key.objectid,
  3620. dir_ino, &index, name, name_len);
  3621. if (ret < 0) {
  3622. if (ret != -ENOENT) {
  3623. btrfs_abort_transaction(trans, root, ret);
  3624. goto out;
  3625. }
  3626. di = btrfs_search_dir_index_item(root, path, dir_ino,
  3627. name, name_len);
  3628. if (IS_ERR_OR_NULL(di)) {
  3629. if (!di)
  3630. ret = -ENOENT;
  3631. else
  3632. ret = PTR_ERR(di);
  3633. btrfs_abort_transaction(trans, root, ret);
  3634. goto out;
  3635. }
  3636. leaf = path->nodes[0];
  3637. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  3638. btrfs_release_path(path);
  3639. index = key.offset;
  3640. }
  3641. btrfs_release_path(path);
  3642. ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
  3643. if (ret) {
  3644. btrfs_abort_transaction(trans, root, ret);
  3645. goto out;
  3646. }
  3647. btrfs_i_size_write(dir, dir->i_size - name_len * 2);
  3648. inode_inc_iversion(dir);
  3649. dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  3650. ret = btrfs_update_inode_fallback(trans, root, dir);
  3651. if (ret)
  3652. btrfs_abort_transaction(trans, root, ret);
  3653. out:
  3654. btrfs_free_path(path);
  3655. return ret;
  3656. }
  3657. static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
  3658. {
  3659. struct inode *inode = d_inode(dentry);
  3660. int err = 0;
  3661. struct btrfs_root *root = BTRFS_I(dir)->root;
  3662. struct btrfs_trans_handle *trans;
  3663. if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
  3664. return -ENOTEMPTY;
  3665. if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
  3666. return -EPERM;
  3667. trans = __unlink_start_trans(dir);
  3668. if (IS_ERR(trans))
  3669. return PTR_ERR(trans);
  3670. if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
  3671. err = btrfs_unlink_subvol(trans, root, dir,
  3672. BTRFS_I(inode)->location.objectid,
  3673. dentry->d_name.name,
  3674. dentry->d_name.len);
  3675. goto out;
  3676. }
  3677. err = btrfs_orphan_add(trans, inode);
  3678. if (err)
  3679. goto out;
  3680. /* now the directory is empty */
  3681. err = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
  3682. dentry->d_name.name, dentry->d_name.len);
  3683. if (!err)
  3684. btrfs_i_size_write(inode, 0);
  3685. out:
  3686. btrfs_end_transaction(trans, root);
  3687. btrfs_btree_balance_dirty(root);
  3688. return err;
  3689. }
  3690. static int truncate_space_check(struct btrfs_trans_handle *trans,
  3691. struct btrfs_root *root,
  3692. u64 bytes_deleted)
  3693. {
  3694. int ret;
  3695. bytes_deleted = btrfs_csum_bytes_to_leaves(root, bytes_deleted);
  3696. ret = btrfs_block_rsv_add(root, &root->fs_info->trans_block_rsv,
  3697. bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
  3698. if (!ret)
  3699. trans->bytes_reserved += bytes_deleted;
  3700. return ret;
  3701. }
  3702. /*
  3703. * this can truncate away extent items, csum items and directory items.
  3704. * It starts at a high offset and removes keys until it can't find
  3705. * any higher than new_size
  3706. *
  3707. * csum items that cross the new i_size are truncated to the new size
  3708. * as well.
  3709. *
  3710. * min_type is the minimum key type to truncate down to. If set to 0, this
  3711. * will kill all the items on this inode, including the INODE_ITEM_KEY.
  3712. */
  3713. int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
  3714. struct btrfs_root *root,
  3715. struct inode *inode,
  3716. u64 new_size, u32 min_type)
  3717. {
  3718. struct btrfs_path *path;
  3719. struct extent_buffer *leaf;
  3720. struct btrfs_file_extent_item *fi;
  3721. struct btrfs_key key;
  3722. struct btrfs_key found_key;
  3723. u64 extent_start = 0;
  3724. u64 extent_num_bytes = 0;
  3725. u64 extent_offset = 0;
  3726. u64 item_end = 0;
  3727. u64 last_size = new_size;
  3728. u32 found_type = (u8)-1;
  3729. int found_extent;
  3730. int del_item;
  3731. int pending_del_nr = 0;
  3732. int pending_del_slot = 0;
  3733. int extent_type = -1;
  3734. int ret;
  3735. int err = 0;
  3736. u64 ino = btrfs_ino(inode);
  3737. u64 bytes_deleted = 0;
  3738. bool be_nice = 0;
  3739. bool should_throttle = 0;
  3740. bool should_end = 0;
  3741. BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
  3742. /*
  3743. * for non-free space inodes and ref cows, we want to back off from
  3744. * time to time
  3745. */
  3746. if (!btrfs_is_free_space_inode(inode) &&
  3747. test_bit(BTRFS_ROOT_REF_COWS, &root->state))
  3748. be_nice = 1;
  3749. path = btrfs_alloc_path();
  3750. if (!path)
  3751. return -ENOMEM;
  3752. path->reada = -1;
  3753. /*
  3754. * We want to drop from the next block forward in case this new size is
  3755. * not block aligned since we will be keeping the last block of the
  3756. * extent just the way it is.
  3757. */
  3758. if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
  3759. root == root->fs_info->tree_root)
  3760. btrfs_drop_extent_cache(inode, ALIGN(new_size,
  3761. root->sectorsize), (u64)-1, 0);
  3762. /*
  3763. * This function is also used to drop the items in the log tree before
  3764. * we relog the inode, so if root != BTRFS_I(inode)->root, it means
  3765. * it is used to drop the loged items. So we shouldn't kill the delayed
  3766. * items.
  3767. */
  3768. if (min_type == 0 && root == BTRFS_I(inode)->root)
  3769. btrfs_kill_delayed_inode_items(inode);
  3770. key.objectid = ino;
  3771. key.offset = (u64)-1;
  3772. key.type = (u8)-1;
  3773. search_again:
  3774. /*
  3775. * with a 16K leaf size and 128MB extents, you can actually queue
  3776. * up a huge file in a single leaf. Most of the time that
  3777. * bytes_deleted is > 0, it will be huge by the time we get here
  3778. */
  3779. if (be_nice && bytes_deleted > 32 * 1024 * 1024) {
  3780. if (btrfs_should_end_transaction(trans, root)) {
  3781. err = -EAGAIN;
  3782. goto error;
  3783. }
  3784. }
  3785. path->leave_spinning = 1;
  3786. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  3787. if (ret < 0) {
  3788. err = ret;
  3789. goto out;
  3790. }
  3791. if (ret > 0) {
  3792. /* there are no items in the tree for us to truncate, we're
  3793. * done
  3794. */
  3795. if (path->slots[0] == 0)
  3796. goto out;
  3797. path->slots[0]--;
  3798. }
  3799. while (1) {
  3800. fi = NULL;
  3801. leaf = path->nodes[0];
  3802. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3803. found_type = found_key.type;
  3804. if (found_key.objectid != ino)
  3805. break;
  3806. if (found_type < min_type)
  3807. break;
  3808. item_end = found_key.offset;
  3809. if (found_type == BTRFS_EXTENT_DATA_KEY) {
  3810. fi = btrfs_item_ptr(leaf, path->slots[0],
  3811. struct btrfs_file_extent_item);
  3812. extent_type = btrfs_file_extent_type(leaf, fi);
  3813. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  3814. item_end +=
  3815. btrfs_file_extent_num_bytes(leaf, fi);
  3816. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  3817. item_end += btrfs_file_extent_inline_len(leaf,
  3818. path->slots[0], fi);
  3819. }
  3820. item_end--;
  3821. }
  3822. if (found_type > min_type) {
  3823. del_item = 1;
  3824. } else {
  3825. if (item_end < new_size)
  3826. break;
  3827. if (found_key.offset >= new_size)
  3828. del_item = 1;
  3829. else
  3830. del_item = 0;
  3831. }
  3832. found_extent = 0;
  3833. /* FIXME, shrink the extent if the ref count is only 1 */
  3834. if (found_type != BTRFS_EXTENT_DATA_KEY)
  3835. goto delete;
  3836. if (del_item)
  3837. last_size = found_key.offset;
  3838. else
  3839. last_size = new_size;
  3840. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  3841. u64 num_dec;
  3842. extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
  3843. if (!del_item) {
  3844. u64 orig_num_bytes =
  3845. btrfs_file_extent_num_bytes(leaf, fi);
  3846. extent_num_bytes = ALIGN(new_size -
  3847. found_key.offset,
  3848. root->sectorsize);
  3849. btrfs_set_file_extent_num_bytes(leaf, fi,
  3850. extent_num_bytes);
  3851. num_dec = (orig_num_bytes -
  3852. extent_num_bytes);
  3853. if (test_bit(BTRFS_ROOT_REF_COWS,
  3854. &root->state) &&
  3855. extent_start != 0)
  3856. inode_sub_bytes(inode, num_dec);
  3857. btrfs_mark_buffer_dirty(leaf);
  3858. } else {
  3859. extent_num_bytes =
  3860. btrfs_file_extent_disk_num_bytes(leaf,
  3861. fi);
  3862. extent_offset = found_key.offset -
  3863. btrfs_file_extent_offset(leaf, fi);
  3864. /* FIXME blocksize != 4096 */
  3865. num_dec = btrfs_file_extent_num_bytes(leaf, fi);
  3866. if (extent_start != 0) {
  3867. found_extent = 1;
  3868. if (test_bit(BTRFS_ROOT_REF_COWS,
  3869. &root->state))
  3870. inode_sub_bytes(inode, num_dec);
  3871. }
  3872. }
  3873. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  3874. /*
  3875. * we can't truncate inline items that have had
  3876. * special encodings
  3877. */
  3878. if (!del_item &&
  3879. btrfs_file_extent_compression(leaf, fi) == 0 &&
  3880. btrfs_file_extent_encryption(leaf, fi) == 0 &&
  3881. btrfs_file_extent_other_encoding(leaf, fi) == 0) {
  3882. u32 size = new_size - found_key.offset;
  3883. if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
  3884. inode_sub_bytes(inode, item_end + 1 -
  3885. new_size);
  3886. /*
  3887. * update the ram bytes to properly reflect
  3888. * the new size of our item
  3889. */
  3890. btrfs_set_file_extent_ram_bytes(leaf, fi, size);
  3891. size =
  3892. btrfs_file_extent_calc_inline_size(size);
  3893. btrfs_truncate_item(root, path, size, 1);
  3894. } else if (test_bit(BTRFS_ROOT_REF_COWS,
  3895. &root->state)) {
  3896. inode_sub_bytes(inode, item_end + 1 -
  3897. found_key.offset);
  3898. }
  3899. }
  3900. delete:
  3901. if (del_item) {
  3902. if (!pending_del_nr) {
  3903. /* no pending yet, add ourselves */
  3904. pending_del_slot = path->slots[0];
  3905. pending_del_nr = 1;
  3906. } else if (pending_del_nr &&
  3907. path->slots[0] + 1 == pending_del_slot) {
  3908. /* hop on the pending chunk */
  3909. pending_del_nr++;
  3910. pending_del_slot = path->slots[0];
  3911. } else {
  3912. BUG();
  3913. }
  3914. } else {
  3915. break;
  3916. }
  3917. should_throttle = 0;
  3918. if (found_extent &&
  3919. (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
  3920. root == root->fs_info->tree_root)) {
  3921. btrfs_set_path_blocking(path);
  3922. bytes_deleted += extent_num_bytes;
  3923. ret = btrfs_free_extent(trans, root, extent_start,
  3924. extent_num_bytes, 0,
  3925. btrfs_header_owner(leaf),
  3926. ino, extent_offset, 0);
  3927. BUG_ON(ret);
  3928. if (btrfs_should_throttle_delayed_refs(trans, root))
  3929. btrfs_async_run_delayed_refs(root,
  3930. trans->delayed_ref_updates * 2, 0);
  3931. if (be_nice) {
  3932. if (truncate_space_check(trans, root,
  3933. extent_num_bytes)) {
  3934. should_end = 1;
  3935. }
  3936. if (btrfs_should_throttle_delayed_refs(trans,
  3937. root)) {
  3938. should_throttle = 1;
  3939. }
  3940. }
  3941. }
  3942. if (found_type == BTRFS_INODE_ITEM_KEY)
  3943. break;
  3944. if (path->slots[0] == 0 ||
  3945. path->slots[0] != pending_del_slot ||
  3946. should_throttle || should_end) {
  3947. if (pending_del_nr) {
  3948. ret = btrfs_del_items(trans, root, path,
  3949. pending_del_slot,
  3950. pending_del_nr);
  3951. if (ret) {
  3952. btrfs_abort_transaction(trans,
  3953. root, ret);
  3954. goto error;
  3955. }
  3956. pending_del_nr = 0;
  3957. }
  3958. btrfs_release_path(path);
  3959. if (should_throttle) {
  3960. unsigned long updates = trans->delayed_ref_updates;
  3961. if (updates) {
  3962. trans->delayed_ref_updates = 0;
  3963. ret = btrfs_run_delayed_refs(trans, root, updates * 2);
  3964. if (ret && !err)
  3965. err = ret;
  3966. }
  3967. }
  3968. /*
  3969. * if we failed to refill our space rsv, bail out
  3970. * and let the transaction restart
  3971. */
  3972. if (should_end) {
  3973. err = -EAGAIN;
  3974. goto error;
  3975. }
  3976. goto search_again;
  3977. } else {
  3978. path->slots[0]--;
  3979. }
  3980. }
  3981. out:
  3982. if (pending_del_nr) {
  3983. ret = btrfs_del_items(trans, root, path, pending_del_slot,
  3984. pending_del_nr);
  3985. if (ret)
  3986. btrfs_abort_transaction(trans, root, ret);
  3987. }
  3988. error:
  3989. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  3990. btrfs_ordered_update_i_size(inode, last_size, NULL);
  3991. btrfs_free_path(path);
  3992. if (be_nice && bytes_deleted > 32 * 1024 * 1024) {
  3993. unsigned long updates = trans->delayed_ref_updates;
  3994. if (updates) {
  3995. trans->delayed_ref_updates = 0;
  3996. ret = btrfs_run_delayed_refs(trans, root, updates * 2);
  3997. if (ret && !err)
  3998. err = ret;
  3999. }
  4000. }
  4001. return err;
  4002. }
  4003. /*
  4004. * btrfs_truncate_page - read, zero a chunk and write a page
  4005. * @inode - inode that we're zeroing
  4006. * @from - the offset to start zeroing
  4007. * @len - the length to zero, 0 to zero the entire range respective to the
  4008. * offset
  4009. * @front - zero up to the offset instead of from the offset on
  4010. *
  4011. * This will find the page for the "from" offset and cow the page and zero the
  4012. * part we want to zero. This is used with truncate and hole punching.
  4013. */
  4014. int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
  4015. int front)
  4016. {
  4017. struct address_space *mapping = inode->i_mapping;
  4018. struct btrfs_root *root = BTRFS_I(inode)->root;
  4019. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  4020. struct btrfs_ordered_extent *ordered;
  4021. struct extent_state *cached_state = NULL;
  4022. char *kaddr;
  4023. u32 blocksize = root->sectorsize;
  4024. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  4025. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  4026. struct page *page;
  4027. gfp_t mask = btrfs_alloc_write_mask(mapping);
  4028. int ret = 0;
  4029. u64 page_start;
  4030. u64 page_end;
  4031. if ((offset & (blocksize - 1)) == 0 &&
  4032. (!len || ((len & (blocksize - 1)) == 0)))
  4033. goto out;
  4034. ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
  4035. if (ret)
  4036. goto out;
  4037. again:
  4038. page = find_or_create_page(mapping, index, mask);
  4039. if (!page) {
  4040. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  4041. ret = -ENOMEM;
  4042. goto out;
  4043. }
  4044. page_start = page_offset(page);
  4045. page_end = page_start + PAGE_CACHE_SIZE - 1;
  4046. if (!PageUptodate(page)) {
  4047. ret = btrfs_readpage(NULL, page);
  4048. lock_page(page);
  4049. if (page->mapping != mapping) {
  4050. unlock_page(page);
  4051. page_cache_release(page);
  4052. goto again;
  4053. }
  4054. if (!PageUptodate(page)) {
  4055. ret = -EIO;
  4056. goto out_unlock;
  4057. }
  4058. }
  4059. wait_on_page_writeback(page);
  4060. lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
  4061. set_page_extent_mapped(page);
  4062. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  4063. if (ordered) {
  4064. unlock_extent_cached(io_tree, page_start, page_end,
  4065. &cached_state, GFP_NOFS);
  4066. unlock_page(page);
  4067. page_cache_release(page);
  4068. btrfs_start_ordered_extent(inode, ordered, 1);
  4069. btrfs_put_ordered_extent(ordered);
  4070. goto again;
  4071. }
  4072. clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
  4073. EXTENT_DIRTY | EXTENT_DELALLOC |
  4074. EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
  4075. 0, 0, &cached_state, GFP_NOFS);
  4076. ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
  4077. &cached_state);
  4078. if (ret) {
  4079. unlock_extent_cached(io_tree, page_start, page_end,
  4080. &cached_state, GFP_NOFS);
  4081. goto out_unlock;
  4082. }
  4083. if (offset != PAGE_CACHE_SIZE) {
  4084. if (!len)
  4085. len = PAGE_CACHE_SIZE - offset;
  4086. kaddr = kmap(page);
  4087. if (front)
  4088. memset(kaddr, 0, offset);
  4089. else
  4090. memset(kaddr + offset, 0, len);
  4091. flush_dcache_page(page);
  4092. kunmap(page);
  4093. }
  4094. ClearPageChecked(page);
  4095. set_page_dirty(page);
  4096. unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
  4097. GFP_NOFS);
  4098. out_unlock:
  4099. if (ret)
  4100. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  4101. unlock_page(page);
  4102. page_cache_release(page);
  4103. out:
  4104. return ret;
  4105. }
  4106. static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
  4107. u64 offset, u64 len)
  4108. {
  4109. struct btrfs_trans_handle *trans;
  4110. int ret;
  4111. /*
  4112. * Still need to make sure the inode looks like it's been updated so
  4113. * that any holes get logged if we fsync.
  4114. */
  4115. if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
  4116. BTRFS_I(inode)->last_trans = root->fs_info->generation;
  4117. BTRFS_I(inode)->last_sub_trans = root->log_transid;
  4118. BTRFS_I(inode)->last_log_commit = root->last_log_commit;
  4119. return 0;
  4120. }
  4121. /*
  4122. * 1 - for the one we're dropping
  4123. * 1 - for the one we're adding
  4124. * 1 - for updating the inode.
  4125. */
  4126. trans = btrfs_start_transaction(root, 3);
  4127. if (IS_ERR(trans))
  4128. return PTR_ERR(trans);
  4129. ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
  4130. if (ret) {
  4131. btrfs_abort_transaction(trans, root, ret);
  4132. btrfs_end_transaction(trans, root);
  4133. return ret;
  4134. }
  4135. ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
  4136. 0, 0, len, 0, len, 0, 0, 0);
  4137. if (ret)
  4138. btrfs_abort_transaction(trans, root, ret);
  4139. else
  4140. btrfs_update_inode(trans, root, inode);
  4141. btrfs_end_transaction(trans, root);
  4142. return ret;
  4143. }
  4144. /*
  4145. * This function puts in dummy file extents for the area we're creating a hole
  4146. * for. So if we are truncating this file to a larger size we need to insert
  4147. * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
  4148. * the range between oldsize and size
  4149. */
  4150. int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
  4151. {
  4152. struct btrfs_root *root = BTRFS_I(inode)->root;
  4153. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  4154. struct extent_map *em = NULL;
  4155. struct extent_state *cached_state = NULL;
  4156. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  4157. u64 hole_start = ALIGN(oldsize, root->sectorsize);
  4158. u64 block_end = ALIGN(size, root->sectorsize);
  4159. u64 last_byte;
  4160. u64 cur_offset;
  4161. u64 hole_size;
  4162. int err = 0;
  4163. /*
  4164. * If our size started in the middle of a page we need to zero out the
  4165. * rest of the page before we expand the i_size, otherwise we could
  4166. * expose stale data.
  4167. */
  4168. err = btrfs_truncate_page(inode, oldsize, 0, 0);
  4169. if (err)
  4170. return err;
  4171. if (size <= hole_start)
  4172. return 0;
  4173. while (1) {
  4174. struct btrfs_ordered_extent *ordered;
  4175. lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
  4176. &cached_state);
  4177. ordered = btrfs_lookup_ordered_range(inode, hole_start,
  4178. block_end - hole_start);
  4179. if (!ordered)
  4180. break;
  4181. unlock_extent_cached(io_tree, hole_start, block_end - 1,
  4182. &cached_state, GFP_NOFS);
  4183. btrfs_start_ordered_extent(inode, ordered, 1);
  4184. btrfs_put_ordered_extent(ordered);
  4185. }
  4186. cur_offset = hole_start;
  4187. while (1) {
  4188. em = btrfs_get_extent(inode, NULL, 0, cur_offset,
  4189. block_end - cur_offset, 0);
  4190. if (IS_ERR(em)) {
  4191. err = PTR_ERR(em);
  4192. em = NULL;
  4193. break;
  4194. }
  4195. last_byte = min(extent_map_end(em), block_end);
  4196. last_byte = ALIGN(last_byte , root->sectorsize);
  4197. if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
  4198. struct extent_map *hole_em;
  4199. hole_size = last_byte - cur_offset;
  4200. err = maybe_insert_hole(root, inode, cur_offset,
  4201. hole_size);
  4202. if (err)
  4203. break;
  4204. btrfs_drop_extent_cache(inode, cur_offset,
  4205. cur_offset + hole_size - 1, 0);
  4206. hole_em = alloc_extent_map();
  4207. if (!hole_em) {
  4208. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  4209. &BTRFS_I(inode)->runtime_flags);
  4210. goto next;
  4211. }
  4212. hole_em->start = cur_offset;
  4213. hole_em->len = hole_size;
  4214. hole_em->orig_start = cur_offset;
  4215. hole_em->block_start = EXTENT_MAP_HOLE;
  4216. hole_em->block_len = 0;
  4217. hole_em->orig_block_len = 0;
  4218. hole_em->ram_bytes = hole_size;
  4219. hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
  4220. hole_em->compress_type = BTRFS_COMPRESS_NONE;
  4221. hole_em->generation = root->fs_info->generation;
  4222. while (1) {
  4223. write_lock(&em_tree->lock);
  4224. err = add_extent_mapping(em_tree, hole_em, 1);
  4225. write_unlock(&em_tree->lock);
  4226. if (err != -EEXIST)
  4227. break;
  4228. btrfs_drop_extent_cache(inode, cur_offset,
  4229. cur_offset +
  4230. hole_size - 1, 0);
  4231. }
  4232. free_extent_map(hole_em);
  4233. }
  4234. next:
  4235. free_extent_map(em);
  4236. em = NULL;
  4237. cur_offset = last_byte;
  4238. if (cur_offset >= block_end)
  4239. break;
  4240. }
  4241. free_extent_map(em);
  4242. unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
  4243. GFP_NOFS);
  4244. return err;
  4245. }
  4246. static int wait_snapshoting_atomic_t(atomic_t *a)
  4247. {
  4248. schedule();
  4249. return 0;
  4250. }
  4251. static void wait_for_snapshot_creation(struct btrfs_root *root)
  4252. {
  4253. while (true) {
  4254. int ret;
  4255. ret = btrfs_start_write_no_snapshoting(root);
  4256. if (ret)
  4257. break;
  4258. wait_on_atomic_t(&root->will_be_snapshoted,
  4259. wait_snapshoting_atomic_t,
  4260. TASK_UNINTERRUPTIBLE);
  4261. }
  4262. }
  4263. static int btrfs_setsize(struct inode *inode, struct iattr *attr)
  4264. {
  4265. struct btrfs_root *root = BTRFS_I(inode)->root;
  4266. struct btrfs_trans_handle *trans;
  4267. loff_t oldsize = i_size_read(inode);
  4268. loff_t newsize = attr->ia_size;
  4269. int mask = attr->ia_valid;
  4270. int ret;
  4271. /*
  4272. * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
  4273. * special case where we need to update the times despite not having
  4274. * these flags set. For all other operations the VFS set these flags
  4275. * explicitly if it wants a timestamp update.
  4276. */
  4277. if (newsize != oldsize) {
  4278. inode_inc_iversion(inode);
  4279. if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
  4280. inode->i_ctime = inode->i_mtime =
  4281. current_fs_time(inode->i_sb);
  4282. }
  4283. if (newsize > oldsize) {
  4284. truncate_pagecache(inode, newsize);
  4285. /*
  4286. * Don't do an expanding truncate while snapshoting is ongoing.
  4287. * This is to ensure the snapshot captures a fully consistent
  4288. * state of this file - if the snapshot captures this expanding
  4289. * truncation, it must capture all writes that happened before
  4290. * this truncation.
  4291. */
  4292. wait_for_snapshot_creation(root);
  4293. ret = btrfs_cont_expand(inode, oldsize, newsize);
  4294. if (ret) {
  4295. btrfs_end_write_no_snapshoting(root);
  4296. return ret;
  4297. }
  4298. trans = btrfs_start_transaction(root, 1);
  4299. if (IS_ERR(trans)) {
  4300. btrfs_end_write_no_snapshoting(root);
  4301. return PTR_ERR(trans);
  4302. }
  4303. i_size_write(inode, newsize);
  4304. btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
  4305. ret = btrfs_update_inode(trans, root, inode);
  4306. btrfs_end_write_no_snapshoting(root);
  4307. btrfs_end_transaction(trans, root);
  4308. } else {
  4309. /*
  4310. * We're truncating a file that used to have good data down to
  4311. * zero. Make sure it gets into the ordered flush list so that
  4312. * any new writes get down to disk quickly.
  4313. */
  4314. if (newsize == 0)
  4315. set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
  4316. &BTRFS_I(inode)->runtime_flags);
  4317. /*
  4318. * 1 for the orphan item we're going to add
  4319. * 1 for the orphan item deletion.
  4320. */
  4321. trans = btrfs_start_transaction(root, 2);
  4322. if (IS_ERR(trans))
  4323. return PTR_ERR(trans);
  4324. /*
  4325. * We need to do this in case we fail at _any_ point during the
  4326. * actual truncate. Once we do the truncate_setsize we could
  4327. * invalidate pages which forces any outstanding ordered io to
  4328. * be instantly completed which will give us extents that need
  4329. * to be truncated. If we fail to get an orphan inode down we
  4330. * could have left over extents that were never meant to live,
  4331. * so we need to garuntee from this point on that everything
  4332. * will be consistent.
  4333. */
  4334. ret = btrfs_orphan_add(trans, inode);
  4335. btrfs_end_transaction(trans, root);
  4336. if (ret)
  4337. return ret;
  4338. /* we don't support swapfiles, so vmtruncate shouldn't fail */
  4339. truncate_setsize(inode, newsize);
  4340. /* Disable nonlocked read DIO to avoid the end less truncate */
  4341. btrfs_inode_block_unlocked_dio(inode);
  4342. inode_dio_wait(inode);
  4343. btrfs_inode_resume_unlocked_dio(inode);
  4344. ret = btrfs_truncate(inode);
  4345. if (ret && inode->i_nlink) {
  4346. int err;
  4347. /*
  4348. * failed to truncate, disk_i_size is only adjusted down
  4349. * as we remove extents, so it should represent the true
  4350. * size of the inode, so reset the in memory size and
  4351. * delete our orphan entry.
  4352. */
  4353. trans = btrfs_join_transaction(root);
  4354. if (IS_ERR(trans)) {
  4355. btrfs_orphan_del(NULL, inode);
  4356. return ret;
  4357. }
  4358. i_size_write(inode, BTRFS_I(inode)->disk_i_size);
  4359. err = btrfs_orphan_del(trans, inode);
  4360. if (err)
  4361. btrfs_abort_transaction(trans, root, err);
  4362. btrfs_end_transaction(trans, root);
  4363. }
  4364. }
  4365. return ret;
  4366. }
  4367. static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
  4368. {
  4369. struct inode *inode = d_inode(dentry);
  4370. struct btrfs_root *root = BTRFS_I(inode)->root;
  4371. int err;
  4372. if (btrfs_root_readonly(root))
  4373. return -EROFS;
  4374. err = inode_change_ok(inode, attr);
  4375. if (err)
  4376. return err;
  4377. if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
  4378. err = btrfs_setsize(inode, attr);
  4379. if (err)
  4380. return err;
  4381. }
  4382. if (attr->ia_valid) {
  4383. setattr_copy(inode, attr);
  4384. inode_inc_iversion(inode);
  4385. err = btrfs_dirty_inode(inode);
  4386. if (!err && attr->ia_valid & ATTR_MODE)
  4387. err = posix_acl_chmod(inode, inode->i_mode);
  4388. }
  4389. return err;
  4390. }
  4391. /*
  4392. * While truncating the inode pages during eviction, we get the VFS calling
  4393. * btrfs_invalidatepage() against each page of the inode. This is slow because
  4394. * the calls to btrfs_invalidatepage() result in a huge amount of calls to
  4395. * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
  4396. * extent_state structures over and over, wasting lots of time.
  4397. *
  4398. * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
  4399. * those expensive operations on a per page basis and do only the ordered io
  4400. * finishing, while we release here the extent_map and extent_state structures,
  4401. * without the excessive merging and splitting.
  4402. */
  4403. static void evict_inode_truncate_pages(struct inode *inode)
  4404. {
  4405. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  4406. struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
  4407. struct rb_node *node;
  4408. ASSERT(inode->i_state & I_FREEING);
  4409. truncate_inode_pages_final(&inode->i_data);
  4410. write_lock(&map_tree->lock);
  4411. while (!RB_EMPTY_ROOT(&map_tree->map)) {
  4412. struct extent_map *em;
  4413. node = rb_first(&map_tree->map);
  4414. em = rb_entry(node, struct extent_map, rb_node);
  4415. clear_bit(EXTENT_FLAG_PINNED, &em->flags);
  4416. clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
  4417. remove_extent_mapping(map_tree, em);
  4418. free_extent_map(em);
  4419. if (need_resched()) {
  4420. write_unlock(&map_tree->lock);
  4421. cond_resched();
  4422. write_lock(&map_tree->lock);
  4423. }
  4424. }
  4425. write_unlock(&map_tree->lock);
  4426. /*
  4427. * Keep looping until we have no more ranges in the io tree.
  4428. * We can have ongoing bios started by readpages (called from readahead)
  4429. * that have their endio callback (extent_io.c:end_bio_extent_readpage)
  4430. * still in progress (unlocked the pages in the bio but did not yet
  4431. * unlocked the ranges in the io tree). Therefore this means some
  4432. * ranges can still be locked and eviction started because before
  4433. * submitting those bios, which are executed by a separate task (work
  4434. * queue kthread), inode references (inode->i_count) were not taken
  4435. * (which would be dropped in the end io callback of each bio).
  4436. * Therefore here we effectively end up waiting for those bios and
  4437. * anyone else holding locked ranges without having bumped the inode's
  4438. * reference count - if we don't do it, when they access the inode's
  4439. * io_tree to unlock a range it may be too late, leading to an
  4440. * use-after-free issue.
  4441. */
  4442. spin_lock(&io_tree->lock);
  4443. while (!RB_EMPTY_ROOT(&io_tree->state)) {
  4444. struct extent_state *state;
  4445. struct extent_state *cached_state = NULL;
  4446. u64 start;
  4447. u64 end;
  4448. node = rb_first(&io_tree->state);
  4449. state = rb_entry(node, struct extent_state, rb_node);
  4450. start = state->start;
  4451. end = state->end;
  4452. spin_unlock(&io_tree->lock);
  4453. lock_extent_bits(io_tree, start, end, 0, &cached_state);
  4454. clear_extent_bit(io_tree, start, end,
  4455. EXTENT_LOCKED | EXTENT_DIRTY |
  4456. EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
  4457. EXTENT_DEFRAG, 1, 1,
  4458. &cached_state, GFP_NOFS);
  4459. cond_resched();
  4460. spin_lock(&io_tree->lock);
  4461. }
  4462. spin_unlock(&io_tree->lock);
  4463. }
  4464. void btrfs_evict_inode(struct inode *inode)
  4465. {
  4466. struct btrfs_trans_handle *trans;
  4467. struct btrfs_root *root = BTRFS_I(inode)->root;
  4468. struct btrfs_block_rsv *rsv, *global_rsv;
  4469. int steal_from_global = 0;
  4470. u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
  4471. int ret;
  4472. trace_btrfs_inode_evict(inode);
  4473. evict_inode_truncate_pages(inode);
  4474. if (inode->i_nlink &&
  4475. ((btrfs_root_refs(&root->root_item) != 0 &&
  4476. root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
  4477. btrfs_is_free_space_inode(inode)))
  4478. goto no_delete;
  4479. if (is_bad_inode(inode)) {
  4480. btrfs_orphan_del(NULL, inode);
  4481. goto no_delete;
  4482. }
  4483. /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
  4484. if (!special_file(inode->i_mode))
  4485. btrfs_wait_ordered_range(inode, 0, (u64)-1);
  4486. btrfs_free_io_failure_record(inode, 0, (u64)-1);
  4487. if (root->fs_info->log_root_recovering) {
  4488. BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
  4489. &BTRFS_I(inode)->runtime_flags));
  4490. goto no_delete;
  4491. }
  4492. if (inode->i_nlink > 0) {
  4493. BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
  4494. root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
  4495. goto no_delete;
  4496. }
  4497. ret = btrfs_commit_inode_delayed_inode(inode);
  4498. if (ret) {
  4499. btrfs_orphan_del(NULL, inode);
  4500. goto no_delete;
  4501. }
  4502. rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
  4503. if (!rsv) {
  4504. btrfs_orphan_del(NULL, inode);
  4505. goto no_delete;
  4506. }
  4507. rsv->size = min_size;
  4508. rsv->failfast = 1;
  4509. global_rsv = &root->fs_info->global_block_rsv;
  4510. btrfs_i_size_write(inode, 0);
  4511. /*
  4512. * This is a bit simpler than btrfs_truncate since we've already
  4513. * reserved our space for our orphan item in the unlink, so we just
  4514. * need to reserve some slack space in case we add bytes and update
  4515. * inode item when doing the truncate.
  4516. */
  4517. while (1) {
  4518. ret = btrfs_block_rsv_refill(root, rsv, min_size,
  4519. BTRFS_RESERVE_FLUSH_LIMIT);
  4520. /*
  4521. * Try and steal from the global reserve since we will
  4522. * likely not use this space anyway, we want to try as
  4523. * hard as possible to get this to work.
  4524. */
  4525. if (ret)
  4526. steal_from_global++;
  4527. else
  4528. steal_from_global = 0;
  4529. ret = 0;
  4530. /*
  4531. * steal_from_global == 0: we reserved stuff, hooray!
  4532. * steal_from_global == 1: we didn't reserve stuff, boo!
  4533. * steal_from_global == 2: we've committed, still not a lot of
  4534. * room but maybe we'll have room in the global reserve this
  4535. * time.
  4536. * steal_from_global == 3: abandon all hope!
  4537. */
  4538. if (steal_from_global > 2) {
  4539. btrfs_warn(root->fs_info,
  4540. "Could not get space for a delete, will truncate on mount %d",
  4541. ret);
  4542. btrfs_orphan_del(NULL, inode);
  4543. btrfs_free_block_rsv(root, rsv);
  4544. goto no_delete;
  4545. }
  4546. trans = btrfs_join_transaction(root);
  4547. if (IS_ERR(trans)) {
  4548. btrfs_orphan_del(NULL, inode);
  4549. btrfs_free_block_rsv(root, rsv);
  4550. goto no_delete;
  4551. }
  4552. /*
  4553. * We can't just steal from the global reserve, we need tomake
  4554. * sure there is room to do it, if not we need to commit and try
  4555. * again.
  4556. */
  4557. if (steal_from_global) {
  4558. if (!btrfs_check_space_for_delayed_refs(trans, root))
  4559. ret = btrfs_block_rsv_migrate(global_rsv, rsv,
  4560. min_size);
  4561. else
  4562. ret = -ENOSPC;
  4563. }
  4564. /*
  4565. * Couldn't steal from the global reserve, we have too much
  4566. * pending stuff built up, commit the transaction and try it
  4567. * again.
  4568. */
  4569. if (ret) {
  4570. ret = btrfs_commit_transaction(trans, root);
  4571. if (ret) {
  4572. btrfs_orphan_del(NULL, inode);
  4573. btrfs_free_block_rsv(root, rsv);
  4574. goto no_delete;
  4575. }
  4576. continue;
  4577. } else {
  4578. steal_from_global = 0;
  4579. }
  4580. trans->block_rsv = rsv;
  4581. ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
  4582. if (ret != -ENOSPC && ret != -EAGAIN)
  4583. break;
  4584. trans->block_rsv = &root->fs_info->trans_block_rsv;
  4585. btrfs_end_transaction(trans, root);
  4586. trans = NULL;
  4587. btrfs_btree_balance_dirty(root);
  4588. }
  4589. btrfs_free_block_rsv(root, rsv);
  4590. /*
  4591. * Errors here aren't a big deal, it just means we leave orphan items
  4592. * in the tree. They will be cleaned up on the next mount.
  4593. */
  4594. if (ret == 0) {
  4595. trans->block_rsv = root->orphan_block_rsv;
  4596. btrfs_orphan_del(trans, inode);
  4597. } else {
  4598. btrfs_orphan_del(NULL, inode);
  4599. }
  4600. trans->block_rsv = &root->fs_info->trans_block_rsv;
  4601. if (!(root == root->fs_info->tree_root ||
  4602. root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
  4603. btrfs_return_ino(root, btrfs_ino(inode));
  4604. btrfs_end_transaction(trans, root);
  4605. btrfs_btree_balance_dirty(root);
  4606. no_delete:
  4607. btrfs_remove_delayed_node(inode);
  4608. clear_inode(inode);
  4609. return;
  4610. }
  4611. /*
  4612. * this returns the key found in the dir entry in the location pointer.
  4613. * If no dir entries were found, location->objectid is 0.
  4614. */
  4615. static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
  4616. struct btrfs_key *location)
  4617. {
  4618. const char *name = dentry->d_name.name;
  4619. int namelen = dentry->d_name.len;
  4620. struct btrfs_dir_item *di;
  4621. struct btrfs_path *path;
  4622. struct btrfs_root *root = BTRFS_I(dir)->root;
  4623. int ret = 0;
  4624. path = btrfs_alloc_path();
  4625. if (!path)
  4626. return -ENOMEM;
  4627. di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
  4628. namelen, 0);
  4629. if (IS_ERR(di))
  4630. ret = PTR_ERR(di);
  4631. if (IS_ERR_OR_NULL(di))
  4632. goto out_err;
  4633. btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
  4634. out:
  4635. btrfs_free_path(path);
  4636. return ret;
  4637. out_err:
  4638. location->objectid = 0;
  4639. goto out;
  4640. }
  4641. /*
  4642. * when we hit a tree root in a directory, the btrfs part of the inode
  4643. * needs to be changed to reflect the root directory of the tree root. This
  4644. * is kind of like crossing a mount point.
  4645. */
  4646. static int fixup_tree_root_location(struct btrfs_root *root,
  4647. struct inode *dir,
  4648. struct dentry *dentry,
  4649. struct btrfs_key *location,
  4650. struct btrfs_root **sub_root)
  4651. {
  4652. struct btrfs_path *path;
  4653. struct btrfs_root *new_root;
  4654. struct btrfs_root_ref *ref;
  4655. struct extent_buffer *leaf;
  4656. struct btrfs_key key;
  4657. int ret;
  4658. int err = 0;
  4659. path = btrfs_alloc_path();
  4660. if (!path) {
  4661. err = -ENOMEM;
  4662. goto out;
  4663. }
  4664. err = -ENOENT;
  4665. key.objectid = BTRFS_I(dir)->root->root_key.objectid;
  4666. key.type = BTRFS_ROOT_REF_KEY;
  4667. key.offset = location->objectid;
  4668. ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, path,
  4669. 0, 0);
  4670. if (ret) {
  4671. if (ret < 0)
  4672. err = ret;
  4673. goto out;
  4674. }
  4675. leaf = path->nodes[0];
  4676. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
  4677. if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
  4678. btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
  4679. goto out;
  4680. ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
  4681. (unsigned long)(ref + 1),
  4682. dentry->d_name.len);
  4683. if (ret)
  4684. goto out;
  4685. btrfs_release_path(path);
  4686. new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
  4687. if (IS_ERR(new_root)) {
  4688. err = PTR_ERR(new_root);
  4689. goto out;
  4690. }
  4691. *sub_root = new_root;
  4692. location->objectid = btrfs_root_dirid(&new_root->root_item);
  4693. location->type = BTRFS_INODE_ITEM_KEY;
  4694. location->offset = 0;
  4695. err = 0;
  4696. out:
  4697. btrfs_free_path(path);
  4698. return err;
  4699. }
  4700. static void inode_tree_add(struct inode *inode)
  4701. {
  4702. struct btrfs_root *root = BTRFS_I(inode)->root;
  4703. struct btrfs_inode *entry;
  4704. struct rb_node **p;
  4705. struct rb_node *parent;
  4706. struct rb_node *new = &BTRFS_I(inode)->rb_node;
  4707. u64 ino = btrfs_ino(inode);
  4708. if (inode_unhashed(inode))
  4709. return;
  4710. parent = NULL;
  4711. spin_lock(&root->inode_lock);
  4712. p = &root->inode_tree.rb_node;
  4713. while (*p) {
  4714. parent = *p;
  4715. entry = rb_entry(parent, struct btrfs_inode, rb_node);
  4716. if (ino < btrfs_ino(&entry->vfs_inode))
  4717. p = &parent->rb_left;
  4718. else if (ino > btrfs_ino(&entry->vfs_inode))
  4719. p = &parent->rb_right;
  4720. else {
  4721. WARN_ON(!(entry->vfs_inode.i_state &
  4722. (I_WILL_FREE | I_FREEING)));
  4723. rb_replace_node(parent, new, &root->inode_tree);
  4724. RB_CLEAR_NODE(parent);
  4725. spin_unlock(&root->inode_lock);
  4726. return;
  4727. }
  4728. }
  4729. rb_link_node(new, parent, p);
  4730. rb_insert_color(new, &root->inode_tree);
  4731. spin_unlock(&root->inode_lock);
  4732. }
  4733. static void inode_tree_del(struct inode *inode)
  4734. {
  4735. struct btrfs_root *root = BTRFS_I(inode)->root;
  4736. int empty = 0;
  4737. spin_lock(&root->inode_lock);
  4738. if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
  4739. rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
  4740. RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
  4741. empty = RB_EMPTY_ROOT(&root->inode_tree);
  4742. }
  4743. spin_unlock(&root->inode_lock);
  4744. if (empty && btrfs_root_refs(&root->root_item) == 0) {
  4745. synchronize_srcu(&root->fs_info->subvol_srcu);
  4746. spin_lock(&root->inode_lock);
  4747. empty = RB_EMPTY_ROOT(&root->inode_tree);
  4748. spin_unlock(&root->inode_lock);
  4749. if (empty)
  4750. btrfs_add_dead_root(root);
  4751. }
  4752. }
  4753. void btrfs_invalidate_inodes(struct btrfs_root *root)
  4754. {
  4755. struct rb_node *node;
  4756. struct rb_node *prev;
  4757. struct btrfs_inode *entry;
  4758. struct inode *inode;
  4759. u64 objectid = 0;
  4760. if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
  4761. WARN_ON(btrfs_root_refs(&root->root_item) != 0);
  4762. spin_lock(&root->inode_lock);
  4763. again:
  4764. node = root->inode_tree.rb_node;
  4765. prev = NULL;
  4766. while (node) {
  4767. prev = node;
  4768. entry = rb_entry(node, struct btrfs_inode, rb_node);
  4769. if (objectid < btrfs_ino(&entry->vfs_inode))
  4770. node = node->rb_left;
  4771. else if (objectid > btrfs_ino(&entry->vfs_inode))
  4772. node = node->rb_right;
  4773. else
  4774. break;
  4775. }
  4776. if (!node) {
  4777. while (prev) {
  4778. entry = rb_entry(prev, struct btrfs_inode, rb_node);
  4779. if (objectid <= btrfs_ino(&entry->vfs_inode)) {
  4780. node = prev;
  4781. break;
  4782. }
  4783. prev = rb_next(prev);
  4784. }
  4785. }
  4786. while (node) {
  4787. entry = rb_entry(node, struct btrfs_inode, rb_node);
  4788. objectid = btrfs_ino(&entry->vfs_inode) + 1;
  4789. inode = igrab(&entry->vfs_inode);
  4790. if (inode) {
  4791. spin_unlock(&root->inode_lock);
  4792. if (atomic_read(&inode->i_count) > 1)
  4793. d_prune_aliases(inode);
  4794. /*
  4795. * btrfs_drop_inode will have it removed from
  4796. * the inode cache when its usage count
  4797. * hits zero.
  4798. */
  4799. iput(inode);
  4800. cond_resched();
  4801. spin_lock(&root->inode_lock);
  4802. goto again;
  4803. }
  4804. if (cond_resched_lock(&root->inode_lock))
  4805. goto again;
  4806. node = rb_next(node);
  4807. }
  4808. spin_unlock(&root->inode_lock);
  4809. }
  4810. static int btrfs_init_locked_inode(struct inode *inode, void *p)
  4811. {
  4812. struct btrfs_iget_args *args = p;
  4813. inode->i_ino = args->location->objectid;
  4814. memcpy(&BTRFS_I(inode)->location, args->location,
  4815. sizeof(*args->location));
  4816. BTRFS_I(inode)->root = args->root;
  4817. return 0;
  4818. }
  4819. static int btrfs_find_actor(struct inode *inode, void *opaque)
  4820. {
  4821. struct btrfs_iget_args *args = opaque;
  4822. return args->location->objectid == BTRFS_I(inode)->location.objectid &&
  4823. args->root == BTRFS_I(inode)->root;
  4824. }
  4825. static struct inode *btrfs_iget_locked(struct super_block *s,
  4826. struct btrfs_key *location,
  4827. struct btrfs_root *root)
  4828. {
  4829. struct inode *inode;
  4830. struct btrfs_iget_args args;
  4831. unsigned long hashval = btrfs_inode_hash(location->objectid, root);
  4832. args.location = location;
  4833. args.root = root;
  4834. inode = iget5_locked(s, hashval, btrfs_find_actor,
  4835. btrfs_init_locked_inode,
  4836. (void *)&args);
  4837. return inode;
  4838. }
  4839. /* Get an inode object given its location and corresponding root.
  4840. * Returns in *is_new if the inode was read from disk
  4841. */
  4842. struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
  4843. struct btrfs_root *root, int *new)
  4844. {
  4845. struct inode *inode;
  4846. inode = btrfs_iget_locked(s, location, root);
  4847. if (!inode)
  4848. return ERR_PTR(-ENOMEM);
  4849. if (inode->i_state & I_NEW) {
  4850. btrfs_read_locked_inode(inode);
  4851. if (!is_bad_inode(inode)) {
  4852. inode_tree_add(inode);
  4853. unlock_new_inode(inode);
  4854. if (new)
  4855. *new = 1;
  4856. } else {
  4857. unlock_new_inode(inode);
  4858. iput(inode);
  4859. inode = ERR_PTR(-ESTALE);
  4860. }
  4861. }
  4862. return inode;
  4863. }
  4864. static struct inode *new_simple_dir(struct super_block *s,
  4865. struct btrfs_key *key,
  4866. struct btrfs_root *root)
  4867. {
  4868. struct inode *inode = new_inode(s);
  4869. if (!inode)
  4870. return ERR_PTR(-ENOMEM);
  4871. BTRFS_I(inode)->root = root;
  4872. memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
  4873. set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
  4874. inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
  4875. inode->i_op = &btrfs_dir_ro_inode_operations;
  4876. inode->i_fop = &simple_dir_operations;
  4877. inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
  4878. inode->i_mtime = CURRENT_TIME;
  4879. inode->i_atime = inode->i_mtime;
  4880. inode->i_ctime = inode->i_mtime;
  4881. BTRFS_I(inode)->i_otime = inode->i_mtime;
  4882. return inode;
  4883. }
  4884. struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
  4885. {
  4886. struct inode *inode;
  4887. struct btrfs_root *root = BTRFS_I(dir)->root;
  4888. struct btrfs_root *sub_root = root;
  4889. struct btrfs_key location;
  4890. int index;
  4891. int ret = 0;
  4892. if (dentry->d_name.len > BTRFS_NAME_LEN)
  4893. return ERR_PTR(-ENAMETOOLONG);
  4894. ret = btrfs_inode_by_name(dir, dentry, &location);
  4895. if (ret < 0)
  4896. return ERR_PTR(ret);
  4897. if (location.objectid == 0)
  4898. return ERR_PTR(-ENOENT);
  4899. if (location.type == BTRFS_INODE_ITEM_KEY) {
  4900. inode = btrfs_iget(dir->i_sb, &location, root, NULL);
  4901. return inode;
  4902. }
  4903. BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
  4904. index = srcu_read_lock(&root->fs_info->subvol_srcu);
  4905. ret = fixup_tree_root_location(root, dir, dentry,
  4906. &location, &sub_root);
  4907. if (ret < 0) {
  4908. if (ret != -ENOENT)
  4909. inode = ERR_PTR(ret);
  4910. else
  4911. inode = new_simple_dir(dir->i_sb, &location, sub_root);
  4912. } else {
  4913. inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
  4914. }
  4915. srcu_read_unlock(&root->fs_info->subvol_srcu, index);
  4916. if (!IS_ERR(inode) && root != sub_root) {
  4917. down_read(&root->fs_info->cleanup_work_sem);
  4918. if (!(inode->i_sb->s_flags & MS_RDONLY))
  4919. ret = btrfs_orphan_cleanup(sub_root);
  4920. up_read(&root->fs_info->cleanup_work_sem);
  4921. if (ret) {
  4922. iput(inode);
  4923. inode = ERR_PTR(ret);
  4924. }
  4925. }
  4926. return inode;
  4927. }
  4928. static int btrfs_dentry_delete(const struct dentry *dentry)
  4929. {
  4930. struct btrfs_root *root;
  4931. struct inode *inode = d_inode(dentry);
  4932. if (!inode && !IS_ROOT(dentry))
  4933. inode = d_inode(dentry->d_parent);
  4934. if (inode) {
  4935. root = BTRFS_I(inode)->root;
  4936. if (btrfs_root_refs(&root->root_item) == 0)
  4937. return 1;
  4938. if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
  4939. return 1;
  4940. }
  4941. return 0;
  4942. }
  4943. static void btrfs_dentry_release(struct dentry *dentry)
  4944. {
  4945. kfree(dentry->d_fsdata);
  4946. }
  4947. static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
  4948. unsigned int flags)
  4949. {
  4950. struct inode *inode;
  4951. inode = btrfs_lookup_dentry(dir, dentry);
  4952. if (IS_ERR(inode)) {
  4953. if (PTR_ERR(inode) == -ENOENT)
  4954. inode = NULL;
  4955. else
  4956. return ERR_CAST(inode);
  4957. }
  4958. return d_splice_alias(inode, dentry);
  4959. }
  4960. unsigned char btrfs_filetype_table[] = {
  4961. DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
  4962. };
  4963. static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
  4964. {
  4965. struct inode *inode = file_inode(file);
  4966. struct btrfs_root *root = BTRFS_I(inode)->root;
  4967. struct btrfs_item *item;
  4968. struct btrfs_dir_item *di;
  4969. struct btrfs_key key;
  4970. struct btrfs_key found_key;
  4971. struct btrfs_path *path;
  4972. struct list_head ins_list;
  4973. struct list_head del_list;
  4974. int ret;
  4975. struct extent_buffer *leaf;
  4976. int slot;
  4977. unsigned char d_type;
  4978. int over = 0;
  4979. u32 di_cur;
  4980. u32 di_total;
  4981. u32 di_len;
  4982. int key_type = BTRFS_DIR_INDEX_KEY;
  4983. char tmp_name[32];
  4984. char *name_ptr;
  4985. int name_len;
  4986. int is_curr = 0; /* ctx->pos points to the current index? */
  4987. /* FIXME, use a real flag for deciding about the key type */
  4988. if (root->fs_info->tree_root == root)
  4989. key_type = BTRFS_DIR_ITEM_KEY;
  4990. if (!dir_emit_dots(file, ctx))
  4991. return 0;
  4992. path = btrfs_alloc_path();
  4993. if (!path)
  4994. return -ENOMEM;
  4995. path->reada = 1;
  4996. if (key_type == BTRFS_DIR_INDEX_KEY) {
  4997. INIT_LIST_HEAD(&ins_list);
  4998. INIT_LIST_HEAD(&del_list);
  4999. btrfs_get_delayed_items(inode, &ins_list, &del_list);
  5000. }
  5001. key.type = key_type;
  5002. key.offset = ctx->pos;
  5003. key.objectid = btrfs_ino(inode);
  5004. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  5005. if (ret < 0)
  5006. goto err;
  5007. while (1) {
  5008. leaf = path->nodes[0];
  5009. slot = path->slots[0];
  5010. if (slot >= btrfs_header_nritems(leaf)) {
  5011. ret = btrfs_next_leaf(root, path);
  5012. if (ret < 0)
  5013. goto err;
  5014. else if (ret > 0)
  5015. break;
  5016. continue;
  5017. }
  5018. item = btrfs_item_nr(slot);
  5019. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  5020. if (found_key.objectid != key.objectid)
  5021. break;
  5022. if (found_key.type != key_type)
  5023. break;
  5024. if (found_key.offset < ctx->pos)
  5025. goto next;
  5026. if (key_type == BTRFS_DIR_INDEX_KEY &&
  5027. btrfs_should_delete_dir_index(&del_list,
  5028. found_key.offset))
  5029. goto next;
  5030. ctx->pos = found_key.offset;
  5031. is_curr = 1;
  5032. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  5033. di_cur = 0;
  5034. di_total = btrfs_item_size(leaf, item);
  5035. while (di_cur < di_total) {
  5036. struct btrfs_key location;
  5037. if (verify_dir_item(root, leaf, di))
  5038. break;
  5039. name_len = btrfs_dir_name_len(leaf, di);
  5040. if (name_len <= sizeof(tmp_name)) {
  5041. name_ptr = tmp_name;
  5042. } else {
  5043. name_ptr = kmalloc(name_len, GFP_NOFS);
  5044. if (!name_ptr) {
  5045. ret = -ENOMEM;
  5046. goto err;
  5047. }
  5048. }
  5049. read_extent_buffer(leaf, name_ptr,
  5050. (unsigned long)(di + 1), name_len);
  5051. d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
  5052. btrfs_dir_item_key_to_cpu(leaf, di, &location);
  5053. /* is this a reference to our own snapshot? If so
  5054. * skip it.
  5055. *
  5056. * In contrast to old kernels, we insert the snapshot's
  5057. * dir item and dir index after it has been created, so
  5058. * we won't find a reference to our own snapshot. We
  5059. * still keep the following code for backward
  5060. * compatibility.
  5061. */
  5062. if (location.type == BTRFS_ROOT_ITEM_KEY &&
  5063. location.objectid == root->root_key.objectid) {
  5064. over = 0;
  5065. goto skip;
  5066. }
  5067. over = !dir_emit(ctx, name_ptr, name_len,
  5068. location.objectid, d_type);
  5069. skip:
  5070. if (name_ptr != tmp_name)
  5071. kfree(name_ptr);
  5072. if (over)
  5073. goto nopos;
  5074. di_len = btrfs_dir_name_len(leaf, di) +
  5075. btrfs_dir_data_len(leaf, di) + sizeof(*di);
  5076. di_cur += di_len;
  5077. di = (struct btrfs_dir_item *)((char *)di + di_len);
  5078. }
  5079. next:
  5080. path->slots[0]++;
  5081. }
  5082. if (key_type == BTRFS_DIR_INDEX_KEY) {
  5083. if (is_curr)
  5084. ctx->pos++;
  5085. ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
  5086. if (ret)
  5087. goto nopos;
  5088. }
  5089. /* Reached end of directory/root. Bump pos past the last item. */
  5090. ctx->pos++;
  5091. /*
  5092. * Stop new entries from being returned after we return the last
  5093. * entry.
  5094. *
  5095. * New directory entries are assigned a strictly increasing
  5096. * offset. This means that new entries created during readdir
  5097. * are *guaranteed* to be seen in the future by that readdir.
  5098. * This has broken buggy programs which operate on names as
  5099. * they're returned by readdir. Until we re-use freed offsets
  5100. * we have this hack to stop new entries from being returned
  5101. * under the assumption that they'll never reach this huge
  5102. * offset.
  5103. *
  5104. * This is being careful not to overflow 32bit loff_t unless the
  5105. * last entry requires it because doing so has broken 32bit apps
  5106. * in the past.
  5107. */
  5108. if (key_type == BTRFS_DIR_INDEX_KEY) {
  5109. if (ctx->pos >= INT_MAX)
  5110. ctx->pos = LLONG_MAX;
  5111. else
  5112. ctx->pos = INT_MAX;
  5113. }
  5114. nopos:
  5115. ret = 0;
  5116. err:
  5117. if (key_type == BTRFS_DIR_INDEX_KEY)
  5118. btrfs_put_delayed_items(&ins_list, &del_list);
  5119. btrfs_free_path(path);
  5120. return ret;
  5121. }
  5122. int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
  5123. {
  5124. struct btrfs_root *root = BTRFS_I(inode)->root;
  5125. struct btrfs_trans_handle *trans;
  5126. int ret = 0;
  5127. bool nolock = false;
  5128. if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
  5129. return 0;
  5130. if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
  5131. nolock = true;
  5132. if (wbc->sync_mode == WB_SYNC_ALL) {
  5133. if (nolock)
  5134. trans = btrfs_join_transaction_nolock(root);
  5135. else
  5136. trans = btrfs_join_transaction(root);
  5137. if (IS_ERR(trans))
  5138. return PTR_ERR(trans);
  5139. ret = btrfs_commit_transaction(trans, root);
  5140. }
  5141. return ret;
  5142. }
  5143. /*
  5144. * This is somewhat expensive, updating the tree every time the
  5145. * inode changes. But, it is most likely to find the inode in cache.
  5146. * FIXME, needs more benchmarking...there are no reasons other than performance
  5147. * to keep or drop this code.
  5148. */
  5149. static int btrfs_dirty_inode(struct inode *inode)
  5150. {
  5151. struct btrfs_root *root = BTRFS_I(inode)->root;
  5152. struct btrfs_trans_handle *trans;
  5153. int ret;
  5154. if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
  5155. return 0;
  5156. trans = btrfs_join_transaction(root);
  5157. if (IS_ERR(trans))
  5158. return PTR_ERR(trans);
  5159. ret = btrfs_update_inode(trans, root, inode);
  5160. if (ret && ret == -ENOSPC) {
  5161. /* whoops, lets try again with the full transaction */
  5162. btrfs_end_transaction(trans, root);
  5163. trans = btrfs_start_transaction(root, 1);
  5164. if (IS_ERR(trans))
  5165. return PTR_ERR(trans);
  5166. ret = btrfs_update_inode(trans, root, inode);
  5167. }
  5168. btrfs_end_transaction(trans, root);
  5169. if (BTRFS_I(inode)->delayed_node)
  5170. btrfs_balance_delayed_items(root);
  5171. return ret;
  5172. }
  5173. /*
  5174. * This is a copy of file_update_time. We need this so we can return error on
  5175. * ENOSPC for updating the inode in the case of file write and mmap writes.
  5176. */
  5177. static int btrfs_update_time(struct inode *inode, struct timespec *now,
  5178. int flags)
  5179. {
  5180. struct btrfs_root *root = BTRFS_I(inode)->root;
  5181. if (btrfs_root_readonly(root))
  5182. return -EROFS;
  5183. if (flags & S_VERSION)
  5184. inode_inc_iversion(inode);
  5185. if (flags & S_CTIME)
  5186. inode->i_ctime = *now;
  5187. if (flags & S_MTIME)
  5188. inode->i_mtime = *now;
  5189. if (flags & S_ATIME)
  5190. inode->i_atime = *now;
  5191. return btrfs_dirty_inode(inode);
  5192. }
  5193. /*
  5194. * find the highest existing sequence number in a directory
  5195. * and then set the in-memory index_cnt variable to reflect
  5196. * free sequence numbers
  5197. */
  5198. static int btrfs_set_inode_index_count(struct inode *inode)
  5199. {
  5200. struct btrfs_root *root = BTRFS_I(inode)->root;
  5201. struct btrfs_key key, found_key;
  5202. struct btrfs_path *path;
  5203. struct extent_buffer *leaf;
  5204. int ret;
  5205. key.objectid = btrfs_ino(inode);
  5206. key.type = BTRFS_DIR_INDEX_KEY;
  5207. key.offset = (u64)-1;
  5208. path = btrfs_alloc_path();
  5209. if (!path)
  5210. return -ENOMEM;
  5211. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  5212. if (ret < 0)
  5213. goto out;
  5214. /* FIXME: we should be able to handle this */
  5215. if (ret == 0)
  5216. goto out;
  5217. ret = 0;
  5218. /*
  5219. * MAGIC NUMBER EXPLANATION:
  5220. * since we search a directory based on f_pos we have to start at 2
  5221. * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
  5222. * else has to start at 2
  5223. */
  5224. if (path->slots[0] == 0) {
  5225. BTRFS_I(inode)->index_cnt = 2;
  5226. goto out;
  5227. }
  5228. path->slots[0]--;
  5229. leaf = path->nodes[0];
  5230. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  5231. if (found_key.objectid != btrfs_ino(inode) ||
  5232. found_key.type != BTRFS_DIR_INDEX_KEY) {
  5233. BTRFS_I(inode)->index_cnt = 2;
  5234. goto out;
  5235. }
  5236. BTRFS_I(inode)->index_cnt = found_key.offset + 1;
  5237. out:
  5238. btrfs_free_path(path);
  5239. return ret;
  5240. }
  5241. /*
  5242. * helper to find a free sequence number in a given directory. This current
  5243. * code is very simple, later versions will do smarter things in the btree
  5244. */
  5245. int btrfs_set_inode_index(struct inode *dir, u64 *index)
  5246. {
  5247. int ret = 0;
  5248. if (BTRFS_I(dir)->index_cnt == (u64)-1) {
  5249. ret = btrfs_inode_delayed_dir_index_count(dir);
  5250. if (ret) {
  5251. ret = btrfs_set_inode_index_count(dir);
  5252. if (ret)
  5253. return ret;
  5254. }
  5255. }
  5256. *index = BTRFS_I(dir)->index_cnt;
  5257. BTRFS_I(dir)->index_cnt++;
  5258. return ret;
  5259. }
  5260. static int btrfs_insert_inode_locked(struct inode *inode)
  5261. {
  5262. struct btrfs_iget_args args;
  5263. args.location = &BTRFS_I(inode)->location;
  5264. args.root = BTRFS_I(inode)->root;
  5265. return insert_inode_locked4(inode,
  5266. btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
  5267. btrfs_find_actor, &args);
  5268. }
  5269. static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
  5270. struct btrfs_root *root,
  5271. struct inode *dir,
  5272. const char *name, int name_len,
  5273. u64 ref_objectid, u64 objectid,
  5274. umode_t mode, u64 *index)
  5275. {
  5276. struct inode *inode;
  5277. struct btrfs_inode_item *inode_item;
  5278. struct btrfs_key *location;
  5279. struct btrfs_path *path;
  5280. struct btrfs_inode_ref *ref;
  5281. struct btrfs_key key[2];
  5282. u32 sizes[2];
  5283. int nitems = name ? 2 : 1;
  5284. unsigned long ptr;
  5285. int ret;
  5286. path = btrfs_alloc_path();
  5287. if (!path)
  5288. return ERR_PTR(-ENOMEM);
  5289. inode = new_inode(root->fs_info->sb);
  5290. if (!inode) {
  5291. btrfs_free_path(path);
  5292. return ERR_PTR(-ENOMEM);
  5293. }
  5294. /*
  5295. * O_TMPFILE, set link count to 0, so that after this point,
  5296. * we fill in an inode item with the correct link count.
  5297. */
  5298. if (!name)
  5299. set_nlink(inode, 0);
  5300. /*
  5301. * we have to initialize this early, so we can reclaim the inode
  5302. * number if we fail afterwards in this function.
  5303. */
  5304. inode->i_ino = objectid;
  5305. if (dir && name) {
  5306. trace_btrfs_inode_request(dir);
  5307. ret = btrfs_set_inode_index(dir, index);
  5308. if (ret) {
  5309. btrfs_free_path(path);
  5310. iput(inode);
  5311. return ERR_PTR(ret);
  5312. }
  5313. } else if (dir) {
  5314. *index = 0;
  5315. }
  5316. /*
  5317. * index_cnt is ignored for everything but a dir,
  5318. * btrfs_get_inode_index_count has an explanation for the magic
  5319. * number
  5320. */
  5321. BTRFS_I(inode)->index_cnt = 2;
  5322. BTRFS_I(inode)->dir_index = *index;
  5323. BTRFS_I(inode)->root = root;
  5324. BTRFS_I(inode)->generation = trans->transid;
  5325. inode->i_generation = BTRFS_I(inode)->generation;
  5326. /*
  5327. * We could have gotten an inode number from somebody who was fsynced
  5328. * and then removed in this same transaction, so let's just set full
  5329. * sync since it will be a full sync anyway and this will blow away the
  5330. * old info in the log.
  5331. */
  5332. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
  5333. key[0].objectid = objectid;
  5334. key[0].type = BTRFS_INODE_ITEM_KEY;
  5335. key[0].offset = 0;
  5336. sizes[0] = sizeof(struct btrfs_inode_item);
  5337. if (name) {
  5338. /*
  5339. * Start new inodes with an inode_ref. This is slightly more
  5340. * efficient for small numbers of hard links since they will
  5341. * be packed into one item. Extended refs will kick in if we
  5342. * add more hard links than can fit in the ref item.
  5343. */
  5344. key[1].objectid = objectid;
  5345. key[1].type = BTRFS_INODE_REF_KEY;
  5346. key[1].offset = ref_objectid;
  5347. sizes[1] = name_len + sizeof(*ref);
  5348. }
  5349. location = &BTRFS_I(inode)->location;
  5350. location->objectid = objectid;
  5351. location->offset = 0;
  5352. location->type = BTRFS_INODE_ITEM_KEY;
  5353. ret = btrfs_insert_inode_locked(inode);
  5354. if (ret < 0)
  5355. goto fail;
  5356. path->leave_spinning = 1;
  5357. ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
  5358. if (ret != 0)
  5359. goto fail_unlock;
  5360. inode_init_owner(inode, dir, mode);
  5361. inode_set_bytes(inode, 0);
  5362. inode->i_mtime = CURRENT_TIME;
  5363. inode->i_atime = inode->i_mtime;
  5364. inode->i_ctime = inode->i_mtime;
  5365. BTRFS_I(inode)->i_otime = inode->i_mtime;
  5366. inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  5367. struct btrfs_inode_item);
  5368. memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
  5369. sizeof(*inode_item));
  5370. fill_inode_item(trans, path->nodes[0], inode_item, inode);
  5371. if (name) {
  5372. ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
  5373. struct btrfs_inode_ref);
  5374. btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
  5375. btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
  5376. ptr = (unsigned long)(ref + 1);
  5377. write_extent_buffer(path->nodes[0], name, ptr, name_len);
  5378. }
  5379. btrfs_mark_buffer_dirty(path->nodes[0]);
  5380. btrfs_free_path(path);
  5381. btrfs_inherit_iflags(inode, dir);
  5382. if (S_ISREG(mode)) {
  5383. if (btrfs_test_opt(root, NODATASUM))
  5384. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
  5385. if (btrfs_test_opt(root, NODATACOW))
  5386. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
  5387. BTRFS_INODE_NODATASUM;
  5388. }
  5389. inode_tree_add(inode);
  5390. trace_btrfs_inode_new(inode);
  5391. btrfs_set_inode_last_trans(trans, inode);
  5392. btrfs_update_root_times(trans, root);
  5393. ret = btrfs_inode_inherit_props(trans, inode, dir);
  5394. if (ret)
  5395. btrfs_err(root->fs_info,
  5396. "error inheriting props for ino %llu (root %llu): %d",
  5397. btrfs_ino(inode), root->root_key.objectid, ret);
  5398. return inode;
  5399. fail_unlock:
  5400. unlock_new_inode(inode);
  5401. fail:
  5402. if (dir && name)
  5403. BTRFS_I(dir)->index_cnt--;
  5404. btrfs_free_path(path);
  5405. iput(inode);
  5406. return ERR_PTR(ret);
  5407. }
  5408. static inline u8 btrfs_inode_type(struct inode *inode)
  5409. {
  5410. return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
  5411. }
  5412. /*
  5413. * utility function to add 'inode' into 'parent_inode' with
  5414. * a give name and a given sequence number.
  5415. * if 'add_backref' is true, also insert a backref from the
  5416. * inode to the parent directory.
  5417. */
  5418. int btrfs_add_link(struct btrfs_trans_handle *trans,
  5419. struct inode *parent_inode, struct inode *inode,
  5420. const char *name, int name_len, int add_backref, u64 index)
  5421. {
  5422. int ret = 0;
  5423. struct btrfs_key key;
  5424. struct btrfs_root *root = BTRFS_I(parent_inode)->root;
  5425. u64 ino = btrfs_ino(inode);
  5426. u64 parent_ino = btrfs_ino(parent_inode);
  5427. if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
  5428. memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
  5429. } else {
  5430. key.objectid = ino;
  5431. key.type = BTRFS_INODE_ITEM_KEY;
  5432. key.offset = 0;
  5433. }
  5434. if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
  5435. ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
  5436. key.objectid, root->root_key.objectid,
  5437. parent_ino, index, name, name_len);
  5438. } else if (add_backref) {
  5439. ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
  5440. parent_ino, index);
  5441. }
  5442. /* Nothing to clean up yet */
  5443. if (ret)
  5444. return ret;
  5445. ret = btrfs_insert_dir_item(trans, root, name, name_len,
  5446. parent_inode, &key,
  5447. btrfs_inode_type(inode), index);
  5448. if (ret == -EEXIST || ret == -EOVERFLOW)
  5449. goto fail_dir_item;
  5450. else if (ret) {
  5451. btrfs_abort_transaction(trans, root, ret);
  5452. return ret;
  5453. }
  5454. btrfs_i_size_write(parent_inode, parent_inode->i_size +
  5455. name_len * 2);
  5456. inode_inc_iversion(parent_inode);
  5457. parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
  5458. ret = btrfs_update_inode(trans, root, parent_inode);
  5459. if (ret)
  5460. btrfs_abort_transaction(trans, root, ret);
  5461. return ret;
  5462. fail_dir_item:
  5463. if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
  5464. u64 local_index;
  5465. int err;
  5466. err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
  5467. key.objectid, root->root_key.objectid,
  5468. parent_ino, &local_index, name, name_len);
  5469. } else if (add_backref) {
  5470. u64 local_index;
  5471. int err;
  5472. err = btrfs_del_inode_ref(trans, root, name, name_len,
  5473. ino, parent_ino, &local_index);
  5474. }
  5475. return ret;
  5476. }
  5477. static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
  5478. struct inode *dir, struct dentry *dentry,
  5479. struct inode *inode, int backref, u64 index)
  5480. {
  5481. int err = btrfs_add_link(trans, dir, inode,
  5482. dentry->d_name.name, dentry->d_name.len,
  5483. backref, index);
  5484. if (err > 0)
  5485. err = -EEXIST;
  5486. return err;
  5487. }
  5488. static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
  5489. umode_t mode, dev_t rdev)
  5490. {
  5491. struct btrfs_trans_handle *trans;
  5492. struct btrfs_root *root = BTRFS_I(dir)->root;
  5493. struct inode *inode = NULL;
  5494. int err;
  5495. int drop_inode = 0;
  5496. u64 objectid;
  5497. u64 index = 0;
  5498. if (!new_valid_dev(rdev))
  5499. return -EINVAL;
  5500. /*
  5501. * 2 for inode item and ref
  5502. * 2 for dir items
  5503. * 1 for xattr if selinux is on
  5504. */
  5505. trans = btrfs_start_transaction(root, 5);
  5506. if (IS_ERR(trans))
  5507. return PTR_ERR(trans);
  5508. err = btrfs_find_free_ino(root, &objectid);
  5509. if (err)
  5510. goto out_unlock;
  5511. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  5512. dentry->d_name.len, btrfs_ino(dir), objectid,
  5513. mode, &index);
  5514. if (IS_ERR(inode)) {
  5515. err = PTR_ERR(inode);
  5516. goto out_unlock;
  5517. }
  5518. /*
  5519. * If the active LSM wants to access the inode during
  5520. * d_instantiate it needs these. Smack checks to see
  5521. * if the filesystem supports xattrs by looking at the
  5522. * ops vector.
  5523. */
  5524. inode->i_op = &btrfs_special_inode_operations;
  5525. init_special_inode(inode, inode->i_mode, rdev);
  5526. err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
  5527. if (err)
  5528. goto out_unlock_inode;
  5529. err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
  5530. if (err) {
  5531. goto out_unlock_inode;
  5532. } else {
  5533. btrfs_update_inode(trans, root, inode);
  5534. unlock_new_inode(inode);
  5535. d_instantiate(dentry, inode);
  5536. }
  5537. out_unlock:
  5538. btrfs_end_transaction(trans, root);
  5539. btrfs_balance_delayed_items(root);
  5540. btrfs_btree_balance_dirty(root);
  5541. if (drop_inode) {
  5542. inode_dec_link_count(inode);
  5543. iput(inode);
  5544. }
  5545. return err;
  5546. out_unlock_inode:
  5547. drop_inode = 1;
  5548. unlock_new_inode(inode);
  5549. goto out_unlock;
  5550. }
  5551. static int btrfs_create(struct inode *dir, struct dentry *dentry,
  5552. umode_t mode, bool excl)
  5553. {
  5554. struct btrfs_trans_handle *trans;
  5555. struct btrfs_root *root = BTRFS_I(dir)->root;
  5556. struct inode *inode = NULL;
  5557. int drop_inode_on_err = 0;
  5558. int err;
  5559. u64 objectid;
  5560. u64 index = 0;
  5561. /*
  5562. * 2 for inode item and ref
  5563. * 2 for dir items
  5564. * 1 for xattr if selinux is on
  5565. */
  5566. trans = btrfs_start_transaction(root, 5);
  5567. if (IS_ERR(trans))
  5568. return PTR_ERR(trans);
  5569. err = btrfs_find_free_ino(root, &objectid);
  5570. if (err)
  5571. goto out_unlock;
  5572. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  5573. dentry->d_name.len, btrfs_ino(dir), objectid,
  5574. mode, &index);
  5575. if (IS_ERR(inode)) {
  5576. err = PTR_ERR(inode);
  5577. goto out_unlock;
  5578. }
  5579. drop_inode_on_err = 1;
  5580. /*
  5581. * If the active LSM wants to access the inode during
  5582. * d_instantiate it needs these. Smack checks to see
  5583. * if the filesystem supports xattrs by looking at the
  5584. * ops vector.
  5585. */
  5586. inode->i_fop = &btrfs_file_operations;
  5587. inode->i_op = &btrfs_file_inode_operations;
  5588. inode->i_mapping->a_ops = &btrfs_aops;
  5589. err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
  5590. if (err)
  5591. goto out_unlock_inode;
  5592. err = btrfs_update_inode(trans, root, inode);
  5593. if (err)
  5594. goto out_unlock_inode;
  5595. err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
  5596. if (err)
  5597. goto out_unlock_inode;
  5598. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  5599. unlock_new_inode(inode);
  5600. d_instantiate(dentry, inode);
  5601. out_unlock:
  5602. btrfs_end_transaction(trans, root);
  5603. if (err && drop_inode_on_err) {
  5604. inode_dec_link_count(inode);
  5605. iput(inode);
  5606. }
  5607. btrfs_balance_delayed_items(root);
  5608. btrfs_btree_balance_dirty(root);
  5609. return err;
  5610. out_unlock_inode:
  5611. unlock_new_inode(inode);
  5612. goto out_unlock;
  5613. }
  5614. static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
  5615. struct dentry *dentry)
  5616. {
  5617. struct btrfs_trans_handle *trans;
  5618. struct btrfs_root *root = BTRFS_I(dir)->root;
  5619. struct inode *inode = d_inode(old_dentry);
  5620. u64 index;
  5621. int err;
  5622. int drop_inode = 0;
  5623. /* do not allow sys_link's with other subvols of the same device */
  5624. if (root->objectid != BTRFS_I(inode)->root->objectid)
  5625. return -EXDEV;
  5626. if (inode->i_nlink >= BTRFS_LINK_MAX)
  5627. return -EMLINK;
  5628. err = btrfs_set_inode_index(dir, &index);
  5629. if (err)
  5630. goto fail;
  5631. /*
  5632. * 2 items for inode and inode ref
  5633. * 2 items for dir items
  5634. * 1 item for parent inode
  5635. */
  5636. trans = btrfs_start_transaction(root, 5);
  5637. if (IS_ERR(trans)) {
  5638. err = PTR_ERR(trans);
  5639. goto fail;
  5640. }
  5641. /* There are several dir indexes for this inode, clear the cache. */
  5642. BTRFS_I(inode)->dir_index = 0ULL;
  5643. inc_nlink(inode);
  5644. inode_inc_iversion(inode);
  5645. inode->i_ctime = CURRENT_TIME;
  5646. ihold(inode);
  5647. set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
  5648. err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
  5649. if (err) {
  5650. drop_inode = 1;
  5651. } else {
  5652. struct dentry *parent = dentry->d_parent;
  5653. err = btrfs_update_inode(trans, root, inode);
  5654. if (err)
  5655. goto fail;
  5656. if (inode->i_nlink == 1) {
  5657. /*
  5658. * If new hard link count is 1, it's a file created
  5659. * with open(2) O_TMPFILE flag.
  5660. */
  5661. err = btrfs_orphan_del(trans, inode);
  5662. if (err)
  5663. goto fail;
  5664. }
  5665. d_instantiate(dentry, inode);
  5666. btrfs_log_new_name(trans, inode, NULL, parent);
  5667. }
  5668. btrfs_end_transaction(trans, root);
  5669. btrfs_balance_delayed_items(root);
  5670. fail:
  5671. if (drop_inode) {
  5672. inode_dec_link_count(inode);
  5673. iput(inode);
  5674. }
  5675. btrfs_btree_balance_dirty(root);
  5676. return err;
  5677. }
  5678. static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  5679. {
  5680. struct inode *inode = NULL;
  5681. struct btrfs_trans_handle *trans;
  5682. struct btrfs_root *root = BTRFS_I(dir)->root;
  5683. int err = 0;
  5684. int drop_on_err = 0;
  5685. u64 objectid = 0;
  5686. u64 index = 0;
  5687. /*
  5688. * 2 items for inode and ref
  5689. * 2 items for dir items
  5690. * 1 for xattr if selinux is on
  5691. */
  5692. trans = btrfs_start_transaction(root, 5);
  5693. if (IS_ERR(trans))
  5694. return PTR_ERR(trans);
  5695. err = btrfs_find_free_ino(root, &objectid);
  5696. if (err)
  5697. goto out_fail;
  5698. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  5699. dentry->d_name.len, btrfs_ino(dir), objectid,
  5700. S_IFDIR | mode, &index);
  5701. if (IS_ERR(inode)) {
  5702. err = PTR_ERR(inode);
  5703. goto out_fail;
  5704. }
  5705. drop_on_err = 1;
  5706. /* these must be set before we unlock the inode */
  5707. inode->i_op = &btrfs_dir_inode_operations;
  5708. inode->i_fop = &btrfs_dir_file_operations;
  5709. err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
  5710. if (err)
  5711. goto out_fail_inode;
  5712. btrfs_i_size_write(inode, 0);
  5713. err = btrfs_update_inode(trans, root, inode);
  5714. if (err)
  5715. goto out_fail_inode;
  5716. err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
  5717. dentry->d_name.len, 0, index);
  5718. if (err)
  5719. goto out_fail_inode;
  5720. d_instantiate(dentry, inode);
  5721. /*
  5722. * mkdir is special. We're unlocking after we call d_instantiate
  5723. * to avoid a race with nfsd calling d_instantiate.
  5724. */
  5725. unlock_new_inode(inode);
  5726. drop_on_err = 0;
  5727. out_fail:
  5728. btrfs_end_transaction(trans, root);
  5729. if (drop_on_err) {
  5730. inode_dec_link_count(inode);
  5731. iput(inode);
  5732. }
  5733. btrfs_balance_delayed_items(root);
  5734. btrfs_btree_balance_dirty(root);
  5735. return err;
  5736. out_fail_inode:
  5737. unlock_new_inode(inode);
  5738. goto out_fail;
  5739. }
  5740. /* Find next extent map of a given extent map, caller needs to ensure locks */
  5741. static struct extent_map *next_extent_map(struct extent_map *em)
  5742. {
  5743. struct rb_node *next;
  5744. next = rb_next(&em->rb_node);
  5745. if (!next)
  5746. return NULL;
  5747. return container_of(next, struct extent_map, rb_node);
  5748. }
  5749. static struct extent_map *prev_extent_map(struct extent_map *em)
  5750. {
  5751. struct rb_node *prev;
  5752. prev = rb_prev(&em->rb_node);
  5753. if (!prev)
  5754. return NULL;
  5755. return container_of(prev, struct extent_map, rb_node);
  5756. }
  5757. /* helper for btfs_get_extent. Given an existing extent in the tree,
  5758. * the existing extent is the nearest extent to map_start,
  5759. * and an extent that you want to insert, deal with overlap and insert
  5760. * the best fitted new extent into the tree.
  5761. */
  5762. static int merge_extent_mapping(struct extent_map_tree *em_tree,
  5763. struct extent_map *existing,
  5764. struct extent_map *em,
  5765. u64 map_start)
  5766. {
  5767. struct extent_map *prev;
  5768. struct extent_map *next;
  5769. u64 start;
  5770. u64 end;
  5771. u64 start_diff;
  5772. BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
  5773. if (existing->start > map_start) {
  5774. next = existing;
  5775. prev = prev_extent_map(next);
  5776. } else {
  5777. prev = existing;
  5778. next = next_extent_map(prev);
  5779. }
  5780. start = prev ? extent_map_end(prev) : em->start;
  5781. start = max_t(u64, start, em->start);
  5782. end = next ? next->start : extent_map_end(em);
  5783. end = min_t(u64, end, extent_map_end(em));
  5784. start_diff = start - em->start;
  5785. em->start = start;
  5786. em->len = end - start;
  5787. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  5788. !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  5789. em->block_start += start_diff;
  5790. em->block_len -= start_diff;
  5791. }
  5792. return add_extent_mapping(em_tree, em, 0);
  5793. }
  5794. static noinline int uncompress_inline(struct btrfs_path *path,
  5795. struct inode *inode, struct page *page,
  5796. size_t pg_offset, u64 extent_offset,
  5797. struct btrfs_file_extent_item *item)
  5798. {
  5799. int ret;
  5800. struct extent_buffer *leaf = path->nodes[0];
  5801. char *tmp;
  5802. size_t max_size;
  5803. unsigned long inline_size;
  5804. unsigned long ptr;
  5805. int compress_type;
  5806. WARN_ON(pg_offset != 0);
  5807. compress_type = btrfs_file_extent_compression(leaf, item);
  5808. max_size = btrfs_file_extent_ram_bytes(leaf, item);
  5809. inline_size = btrfs_file_extent_inline_item_len(leaf,
  5810. btrfs_item_nr(path->slots[0]));
  5811. tmp = kmalloc(inline_size, GFP_NOFS);
  5812. if (!tmp)
  5813. return -ENOMEM;
  5814. ptr = btrfs_file_extent_inline_start(item);
  5815. read_extent_buffer(leaf, tmp, ptr, inline_size);
  5816. max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
  5817. ret = btrfs_decompress(compress_type, tmp, page,
  5818. extent_offset, inline_size, max_size);
  5819. kfree(tmp);
  5820. return ret;
  5821. }
  5822. /*
  5823. * a bit scary, this does extent mapping from logical file offset to the disk.
  5824. * the ugly parts come from merging extents from the disk with the in-ram
  5825. * representation. This gets more complex because of the data=ordered code,
  5826. * where the in-ram extents might be locked pending data=ordered completion.
  5827. *
  5828. * This also copies inline extents directly into the page.
  5829. */
  5830. struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
  5831. size_t pg_offset, u64 start, u64 len,
  5832. int create)
  5833. {
  5834. int ret;
  5835. int err = 0;
  5836. u64 extent_start = 0;
  5837. u64 extent_end = 0;
  5838. u64 objectid = btrfs_ino(inode);
  5839. u32 found_type;
  5840. struct btrfs_path *path = NULL;
  5841. struct btrfs_root *root = BTRFS_I(inode)->root;
  5842. struct btrfs_file_extent_item *item;
  5843. struct extent_buffer *leaf;
  5844. struct btrfs_key found_key;
  5845. struct extent_map *em = NULL;
  5846. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  5847. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  5848. struct btrfs_trans_handle *trans = NULL;
  5849. const bool new_inline = !page || create;
  5850. again:
  5851. read_lock(&em_tree->lock);
  5852. em = lookup_extent_mapping(em_tree, start, len);
  5853. if (em)
  5854. em->bdev = root->fs_info->fs_devices->latest_bdev;
  5855. read_unlock(&em_tree->lock);
  5856. if (em) {
  5857. if (em->start > start || em->start + em->len <= start)
  5858. free_extent_map(em);
  5859. else if (em->block_start == EXTENT_MAP_INLINE && page)
  5860. free_extent_map(em);
  5861. else
  5862. goto out;
  5863. }
  5864. em = alloc_extent_map();
  5865. if (!em) {
  5866. err = -ENOMEM;
  5867. goto out;
  5868. }
  5869. em->bdev = root->fs_info->fs_devices->latest_bdev;
  5870. em->start = EXTENT_MAP_HOLE;
  5871. em->orig_start = EXTENT_MAP_HOLE;
  5872. em->len = (u64)-1;
  5873. em->block_len = (u64)-1;
  5874. if (!path) {
  5875. path = btrfs_alloc_path();
  5876. if (!path) {
  5877. err = -ENOMEM;
  5878. goto out;
  5879. }
  5880. /*
  5881. * Chances are we'll be called again, so go ahead and do
  5882. * readahead
  5883. */
  5884. path->reada = 1;
  5885. }
  5886. ret = btrfs_lookup_file_extent(trans, root, path,
  5887. objectid, start, trans != NULL);
  5888. if (ret < 0) {
  5889. err = ret;
  5890. goto out;
  5891. }
  5892. if (ret != 0) {
  5893. if (path->slots[0] == 0)
  5894. goto not_found;
  5895. path->slots[0]--;
  5896. }
  5897. leaf = path->nodes[0];
  5898. item = btrfs_item_ptr(leaf, path->slots[0],
  5899. struct btrfs_file_extent_item);
  5900. /* are we inside the extent that was found? */
  5901. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  5902. found_type = found_key.type;
  5903. if (found_key.objectid != objectid ||
  5904. found_type != BTRFS_EXTENT_DATA_KEY) {
  5905. /*
  5906. * If we backup past the first extent we want to move forward
  5907. * and see if there is an extent in front of us, otherwise we'll
  5908. * say there is a hole for our whole search range which can
  5909. * cause problems.
  5910. */
  5911. extent_end = start;
  5912. goto next;
  5913. }
  5914. found_type = btrfs_file_extent_type(leaf, item);
  5915. extent_start = found_key.offset;
  5916. if (found_type == BTRFS_FILE_EXTENT_REG ||
  5917. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  5918. extent_end = extent_start +
  5919. btrfs_file_extent_num_bytes(leaf, item);
  5920. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  5921. size_t size;
  5922. size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
  5923. extent_end = ALIGN(extent_start + size, root->sectorsize);
  5924. }
  5925. next:
  5926. if (start >= extent_end) {
  5927. path->slots[0]++;
  5928. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  5929. ret = btrfs_next_leaf(root, path);
  5930. if (ret < 0) {
  5931. err = ret;
  5932. goto out;
  5933. }
  5934. if (ret > 0)
  5935. goto not_found;
  5936. leaf = path->nodes[0];
  5937. }
  5938. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  5939. if (found_key.objectid != objectid ||
  5940. found_key.type != BTRFS_EXTENT_DATA_KEY)
  5941. goto not_found;
  5942. if (start + len <= found_key.offset)
  5943. goto not_found;
  5944. if (start > found_key.offset)
  5945. goto next;
  5946. em->start = start;
  5947. em->orig_start = start;
  5948. em->len = found_key.offset - start;
  5949. goto not_found_em;
  5950. }
  5951. btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
  5952. if (found_type == BTRFS_FILE_EXTENT_REG ||
  5953. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  5954. goto insert;
  5955. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  5956. unsigned long ptr;
  5957. char *map;
  5958. size_t size;
  5959. size_t extent_offset;
  5960. size_t copy_size;
  5961. if (new_inline)
  5962. goto out;
  5963. size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
  5964. extent_offset = page_offset(page) + pg_offset - extent_start;
  5965. copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
  5966. size - extent_offset);
  5967. em->start = extent_start + extent_offset;
  5968. em->len = ALIGN(copy_size, root->sectorsize);
  5969. em->orig_block_len = em->len;
  5970. em->orig_start = em->start;
  5971. ptr = btrfs_file_extent_inline_start(item) + extent_offset;
  5972. if (create == 0 && !PageUptodate(page)) {
  5973. if (btrfs_file_extent_compression(leaf, item) !=
  5974. BTRFS_COMPRESS_NONE) {
  5975. ret = uncompress_inline(path, inode, page,
  5976. pg_offset,
  5977. extent_offset, item);
  5978. if (ret) {
  5979. err = ret;
  5980. goto out;
  5981. }
  5982. } else {
  5983. map = kmap(page);
  5984. read_extent_buffer(leaf, map + pg_offset, ptr,
  5985. copy_size);
  5986. if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
  5987. memset(map + pg_offset + copy_size, 0,
  5988. PAGE_CACHE_SIZE - pg_offset -
  5989. copy_size);
  5990. }
  5991. kunmap(page);
  5992. }
  5993. flush_dcache_page(page);
  5994. } else if (create && PageUptodate(page)) {
  5995. BUG();
  5996. if (!trans) {
  5997. kunmap(page);
  5998. free_extent_map(em);
  5999. em = NULL;
  6000. btrfs_release_path(path);
  6001. trans = btrfs_join_transaction(root);
  6002. if (IS_ERR(trans))
  6003. return ERR_CAST(trans);
  6004. goto again;
  6005. }
  6006. map = kmap(page);
  6007. write_extent_buffer(leaf, map + pg_offset, ptr,
  6008. copy_size);
  6009. kunmap(page);
  6010. btrfs_mark_buffer_dirty(leaf);
  6011. }
  6012. set_extent_uptodate(io_tree, em->start,
  6013. extent_map_end(em) - 1, NULL, GFP_NOFS);
  6014. goto insert;
  6015. }
  6016. not_found:
  6017. em->start = start;
  6018. em->orig_start = start;
  6019. em->len = len;
  6020. not_found_em:
  6021. em->block_start = EXTENT_MAP_HOLE;
  6022. set_bit(EXTENT_FLAG_VACANCY, &em->flags);
  6023. insert:
  6024. btrfs_release_path(path);
  6025. if (em->start > start || extent_map_end(em) <= start) {
  6026. btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
  6027. em->start, em->len, start, len);
  6028. err = -EIO;
  6029. goto out;
  6030. }
  6031. err = 0;
  6032. write_lock(&em_tree->lock);
  6033. ret = add_extent_mapping(em_tree, em, 0);
  6034. /* it is possible that someone inserted the extent into the tree
  6035. * while we had the lock dropped. It is also possible that
  6036. * an overlapping map exists in the tree
  6037. */
  6038. if (ret == -EEXIST) {
  6039. struct extent_map *existing;
  6040. ret = 0;
  6041. existing = search_extent_mapping(em_tree, start, len);
  6042. /*
  6043. * existing will always be non-NULL, since there must be
  6044. * extent causing the -EEXIST.
  6045. */
  6046. if (start >= extent_map_end(existing) ||
  6047. start <= existing->start) {
  6048. /*
  6049. * The existing extent map is the one nearest to
  6050. * the [start, start + len) range which overlaps
  6051. */
  6052. err = merge_extent_mapping(em_tree, existing,
  6053. em, start);
  6054. free_extent_map(existing);
  6055. if (err) {
  6056. free_extent_map(em);
  6057. em = NULL;
  6058. }
  6059. } else {
  6060. free_extent_map(em);
  6061. em = existing;
  6062. err = 0;
  6063. }
  6064. }
  6065. write_unlock(&em_tree->lock);
  6066. out:
  6067. trace_btrfs_get_extent(root, em);
  6068. btrfs_free_path(path);
  6069. if (trans) {
  6070. ret = btrfs_end_transaction(trans, root);
  6071. if (!err)
  6072. err = ret;
  6073. }
  6074. if (err) {
  6075. free_extent_map(em);
  6076. return ERR_PTR(err);
  6077. }
  6078. BUG_ON(!em); /* Error is always set */
  6079. return em;
  6080. }
  6081. struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
  6082. size_t pg_offset, u64 start, u64 len,
  6083. int create)
  6084. {
  6085. struct extent_map *em;
  6086. struct extent_map *hole_em = NULL;
  6087. u64 range_start = start;
  6088. u64 end;
  6089. u64 found;
  6090. u64 found_end;
  6091. int err = 0;
  6092. em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
  6093. if (IS_ERR(em))
  6094. return em;
  6095. if (em) {
  6096. /*
  6097. * if our em maps to
  6098. * - a hole or
  6099. * - a pre-alloc extent,
  6100. * there might actually be delalloc bytes behind it.
  6101. */
  6102. if (em->block_start != EXTENT_MAP_HOLE &&
  6103. !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  6104. return em;
  6105. else
  6106. hole_em = em;
  6107. }
  6108. /* check to see if we've wrapped (len == -1 or similar) */
  6109. end = start + len;
  6110. if (end < start)
  6111. end = (u64)-1;
  6112. else
  6113. end -= 1;
  6114. em = NULL;
  6115. /* ok, we didn't find anything, lets look for delalloc */
  6116. found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
  6117. end, len, EXTENT_DELALLOC, 1);
  6118. found_end = range_start + found;
  6119. if (found_end < range_start)
  6120. found_end = (u64)-1;
  6121. /*
  6122. * we didn't find anything useful, return
  6123. * the original results from get_extent()
  6124. */
  6125. if (range_start > end || found_end <= start) {
  6126. em = hole_em;
  6127. hole_em = NULL;
  6128. goto out;
  6129. }
  6130. /* adjust the range_start to make sure it doesn't
  6131. * go backwards from the start they passed in
  6132. */
  6133. range_start = max(start, range_start);
  6134. found = found_end - range_start;
  6135. if (found > 0) {
  6136. u64 hole_start = start;
  6137. u64 hole_len = len;
  6138. em = alloc_extent_map();
  6139. if (!em) {
  6140. err = -ENOMEM;
  6141. goto out;
  6142. }
  6143. /*
  6144. * when btrfs_get_extent can't find anything it
  6145. * returns one huge hole
  6146. *
  6147. * make sure what it found really fits our range, and
  6148. * adjust to make sure it is based on the start from
  6149. * the caller
  6150. */
  6151. if (hole_em) {
  6152. u64 calc_end = extent_map_end(hole_em);
  6153. if (calc_end <= start || (hole_em->start > end)) {
  6154. free_extent_map(hole_em);
  6155. hole_em = NULL;
  6156. } else {
  6157. hole_start = max(hole_em->start, start);
  6158. hole_len = calc_end - hole_start;
  6159. }
  6160. }
  6161. em->bdev = NULL;
  6162. if (hole_em && range_start > hole_start) {
  6163. /* our hole starts before our delalloc, so we
  6164. * have to return just the parts of the hole
  6165. * that go until the delalloc starts
  6166. */
  6167. em->len = min(hole_len,
  6168. range_start - hole_start);
  6169. em->start = hole_start;
  6170. em->orig_start = hole_start;
  6171. /*
  6172. * don't adjust block start at all,
  6173. * it is fixed at EXTENT_MAP_HOLE
  6174. */
  6175. em->block_start = hole_em->block_start;
  6176. em->block_len = hole_len;
  6177. if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
  6178. set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
  6179. } else {
  6180. em->start = range_start;
  6181. em->len = found;
  6182. em->orig_start = range_start;
  6183. em->block_start = EXTENT_MAP_DELALLOC;
  6184. em->block_len = found;
  6185. }
  6186. } else if (hole_em) {
  6187. return hole_em;
  6188. }
  6189. out:
  6190. free_extent_map(hole_em);
  6191. if (err) {
  6192. free_extent_map(em);
  6193. return ERR_PTR(err);
  6194. }
  6195. return em;
  6196. }
  6197. static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
  6198. u64 start, u64 len)
  6199. {
  6200. struct btrfs_root *root = BTRFS_I(inode)->root;
  6201. struct extent_map *em;
  6202. struct btrfs_key ins;
  6203. u64 alloc_hint;
  6204. int ret;
  6205. alloc_hint = get_extent_allocation_hint(inode, start, len);
  6206. ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
  6207. alloc_hint, &ins, 1, 1);
  6208. if (ret)
  6209. return ERR_PTR(ret);
  6210. em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
  6211. ins.offset, ins.offset, ins.offset, 0);
  6212. if (IS_ERR(em)) {
  6213. btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
  6214. return em;
  6215. }
  6216. ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
  6217. ins.offset, ins.offset, 0);
  6218. if (ret) {
  6219. btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
  6220. free_extent_map(em);
  6221. return ERR_PTR(ret);
  6222. }
  6223. return em;
  6224. }
  6225. /*
  6226. * returns 1 when the nocow is safe, < 1 on error, 0 if the
  6227. * block must be cow'd
  6228. */
  6229. noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
  6230. u64 *orig_start, u64 *orig_block_len,
  6231. u64 *ram_bytes)
  6232. {
  6233. struct btrfs_trans_handle *trans;
  6234. struct btrfs_path *path;
  6235. int ret;
  6236. struct extent_buffer *leaf;
  6237. struct btrfs_root *root = BTRFS_I(inode)->root;
  6238. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  6239. struct btrfs_file_extent_item *fi;
  6240. struct btrfs_key key;
  6241. u64 disk_bytenr;
  6242. u64 backref_offset;
  6243. u64 extent_end;
  6244. u64 num_bytes;
  6245. int slot;
  6246. int found_type;
  6247. bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
  6248. path = btrfs_alloc_path();
  6249. if (!path)
  6250. return -ENOMEM;
  6251. ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
  6252. offset, 0);
  6253. if (ret < 0)
  6254. goto out;
  6255. slot = path->slots[0];
  6256. if (ret == 1) {
  6257. if (slot == 0) {
  6258. /* can't find the item, must cow */
  6259. ret = 0;
  6260. goto out;
  6261. }
  6262. slot--;
  6263. }
  6264. ret = 0;
  6265. leaf = path->nodes[0];
  6266. btrfs_item_key_to_cpu(leaf, &key, slot);
  6267. if (key.objectid != btrfs_ino(inode) ||
  6268. key.type != BTRFS_EXTENT_DATA_KEY) {
  6269. /* not our file or wrong item type, must cow */
  6270. goto out;
  6271. }
  6272. if (key.offset > offset) {
  6273. /* Wrong offset, must cow */
  6274. goto out;
  6275. }
  6276. fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  6277. found_type = btrfs_file_extent_type(leaf, fi);
  6278. if (found_type != BTRFS_FILE_EXTENT_REG &&
  6279. found_type != BTRFS_FILE_EXTENT_PREALLOC) {
  6280. /* not a regular extent, must cow */
  6281. goto out;
  6282. }
  6283. if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
  6284. goto out;
  6285. extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
  6286. if (extent_end <= offset)
  6287. goto out;
  6288. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  6289. if (disk_bytenr == 0)
  6290. goto out;
  6291. if (btrfs_file_extent_compression(leaf, fi) ||
  6292. btrfs_file_extent_encryption(leaf, fi) ||
  6293. btrfs_file_extent_other_encoding(leaf, fi))
  6294. goto out;
  6295. backref_offset = btrfs_file_extent_offset(leaf, fi);
  6296. if (orig_start) {
  6297. *orig_start = key.offset - backref_offset;
  6298. *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
  6299. *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
  6300. }
  6301. if (btrfs_extent_readonly(root, disk_bytenr))
  6302. goto out;
  6303. num_bytes = min(offset + *len, extent_end) - offset;
  6304. if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  6305. u64 range_end;
  6306. range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
  6307. ret = test_range_bit(io_tree, offset, range_end,
  6308. EXTENT_DELALLOC, 0, NULL);
  6309. if (ret) {
  6310. ret = -EAGAIN;
  6311. goto out;
  6312. }
  6313. }
  6314. btrfs_release_path(path);
  6315. /*
  6316. * look for other files referencing this extent, if we
  6317. * find any we must cow
  6318. */
  6319. trans = btrfs_join_transaction(root);
  6320. if (IS_ERR(trans)) {
  6321. ret = 0;
  6322. goto out;
  6323. }
  6324. ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
  6325. key.offset - backref_offset, disk_bytenr);
  6326. btrfs_end_transaction(trans, root);
  6327. if (ret) {
  6328. ret = 0;
  6329. goto out;
  6330. }
  6331. /*
  6332. * adjust disk_bytenr and num_bytes to cover just the bytes
  6333. * in this extent we are about to write. If there
  6334. * are any csums in that range we have to cow in order
  6335. * to keep the csums correct
  6336. */
  6337. disk_bytenr += backref_offset;
  6338. disk_bytenr += offset - key.offset;
  6339. if (csum_exist_in_range(root, disk_bytenr, num_bytes))
  6340. goto out;
  6341. /*
  6342. * all of the above have passed, it is safe to overwrite this extent
  6343. * without cow
  6344. */
  6345. *len = num_bytes;
  6346. ret = 1;
  6347. out:
  6348. btrfs_free_path(path);
  6349. return ret;
  6350. }
  6351. bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
  6352. {
  6353. struct radix_tree_root *root = &inode->i_mapping->page_tree;
  6354. int found = false;
  6355. void **pagep = NULL;
  6356. struct page *page = NULL;
  6357. int start_idx;
  6358. int end_idx;
  6359. start_idx = start >> PAGE_CACHE_SHIFT;
  6360. /*
  6361. * end is the last byte in the last page. end == start is legal
  6362. */
  6363. end_idx = end >> PAGE_CACHE_SHIFT;
  6364. rcu_read_lock();
  6365. /* Most of the code in this while loop is lifted from
  6366. * find_get_page. It's been modified to begin searching from a
  6367. * page and return just the first page found in that range. If the
  6368. * found idx is less than or equal to the end idx then we know that
  6369. * a page exists. If no pages are found or if those pages are
  6370. * outside of the range then we're fine (yay!) */
  6371. while (page == NULL &&
  6372. radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
  6373. page = radix_tree_deref_slot(pagep);
  6374. if (unlikely(!page))
  6375. break;
  6376. if (radix_tree_exception(page)) {
  6377. if (radix_tree_deref_retry(page)) {
  6378. page = NULL;
  6379. continue;
  6380. }
  6381. /*
  6382. * Otherwise, shmem/tmpfs must be storing a swap entry
  6383. * here as an exceptional entry: so return it without
  6384. * attempting to raise page count.
  6385. */
  6386. page = NULL;
  6387. break; /* TODO: Is this relevant for this use case? */
  6388. }
  6389. if (!page_cache_get_speculative(page)) {
  6390. page = NULL;
  6391. continue;
  6392. }
  6393. /*
  6394. * Has the page moved?
  6395. * This is part of the lockless pagecache protocol. See
  6396. * include/linux/pagemap.h for details.
  6397. */
  6398. if (unlikely(page != *pagep)) {
  6399. page_cache_release(page);
  6400. page = NULL;
  6401. }
  6402. }
  6403. if (page) {
  6404. if (page->index <= end_idx)
  6405. found = true;
  6406. page_cache_release(page);
  6407. }
  6408. rcu_read_unlock();
  6409. return found;
  6410. }
  6411. static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
  6412. struct extent_state **cached_state, int writing)
  6413. {
  6414. struct btrfs_ordered_extent *ordered;
  6415. int ret = 0;
  6416. while (1) {
  6417. lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  6418. 0, cached_state);
  6419. /*
  6420. * We're concerned with the entire range that we're going to be
  6421. * doing DIO to, so we need to make sure theres no ordered
  6422. * extents in this range.
  6423. */
  6424. ordered = btrfs_lookup_ordered_range(inode, lockstart,
  6425. lockend - lockstart + 1);
  6426. /*
  6427. * We need to make sure there are no buffered pages in this
  6428. * range either, we could have raced between the invalidate in
  6429. * generic_file_direct_write and locking the extent. The
  6430. * invalidate needs to happen so that reads after a write do not
  6431. * get stale data.
  6432. */
  6433. if (!ordered &&
  6434. (!writing ||
  6435. !btrfs_page_exists_in_range(inode, lockstart, lockend)))
  6436. break;
  6437. unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  6438. cached_state, GFP_NOFS);
  6439. if (ordered) {
  6440. btrfs_start_ordered_extent(inode, ordered, 1);
  6441. btrfs_put_ordered_extent(ordered);
  6442. } else {
  6443. /* Screw you mmap */
  6444. ret = btrfs_fdatawrite_range(inode, lockstart, lockend);
  6445. if (ret)
  6446. break;
  6447. ret = filemap_fdatawait_range(inode->i_mapping,
  6448. lockstart,
  6449. lockend);
  6450. if (ret)
  6451. break;
  6452. /*
  6453. * If we found a page that couldn't be invalidated just
  6454. * fall back to buffered.
  6455. */
  6456. ret = invalidate_inode_pages2_range(inode->i_mapping,
  6457. lockstart >> PAGE_CACHE_SHIFT,
  6458. lockend >> PAGE_CACHE_SHIFT);
  6459. if (ret)
  6460. break;
  6461. }
  6462. cond_resched();
  6463. }
  6464. return ret;
  6465. }
  6466. static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
  6467. u64 len, u64 orig_start,
  6468. u64 block_start, u64 block_len,
  6469. u64 orig_block_len, u64 ram_bytes,
  6470. int type)
  6471. {
  6472. struct extent_map_tree *em_tree;
  6473. struct extent_map *em;
  6474. struct btrfs_root *root = BTRFS_I(inode)->root;
  6475. int ret;
  6476. em_tree = &BTRFS_I(inode)->extent_tree;
  6477. em = alloc_extent_map();
  6478. if (!em)
  6479. return ERR_PTR(-ENOMEM);
  6480. em->start = start;
  6481. em->orig_start = orig_start;
  6482. em->mod_start = start;
  6483. em->mod_len = len;
  6484. em->len = len;
  6485. em->block_len = block_len;
  6486. em->block_start = block_start;
  6487. em->bdev = root->fs_info->fs_devices->latest_bdev;
  6488. em->orig_block_len = orig_block_len;
  6489. em->ram_bytes = ram_bytes;
  6490. em->generation = -1;
  6491. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  6492. if (type == BTRFS_ORDERED_PREALLOC)
  6493. set_bit(EXTENT_FLAG_FILLING, &em->flags);
  6494. do {
  6495. btrfs_drop_extent_cache(inode, em->start,
  6496. em->start + em->len - 1, 0);
  6497. write_lock(&em_tree->lock);
  6498. ret = add_extent_mapping(em_tree, em, 1);
  6499. write_unlock(&em_tree->lock);
  6500. } while (ret == -EEXIST);
  6501. if (ret) {
  6502. free_extent_map(em);
  6503. return ERR_PTR(ret);
  6504. }
  6505. return em;
  6506. }
  6507. struct btrfs_dio_data {
  6508. u64 outstanding_extents;
  6509. u64 reserve;
  6510. };
  6511. static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
  6512. struct buffer_head *bh_result, int create)
  6513. {
  6514. struct extent_map *em;
  6515. struct btrfs_root *root = BTRFS_I(inode)->root;
  6516. struct extent_state *cached_state = NULL;
  6517. struct btrfs_dio_data *dio_data = NULL;
  6518. u64 start = iblock << inode->i_blkbits;
  6519. u64 lockstart, lockend;
  6520. u64 len = bh_result->b_size;
  6521. int unlock_bits = EXTENT_LOCKED;
  6522. int ret = 0;
  6523. if (create)
  6524. unlock_bits |= EXTENT_DIRTY;
  6525. else
  6526. len = min_t(u64, len, root->sectorsize);
  6527. lockstart = start;
  6528. lockend = start + len - 1;
  6529. if (current->journal_info) {
  6530. /*
  6531. * Need to pull our outstanding extents and set journal_info to NULL so
  6532. * that anything that needs to check if there's a transction doesn't get
  6533. * confused.
  6534. */
  6535. dio_data = current->journal_info;
  6536. current->journal_info = NULL;
  6537. }
  6538. /*
  6539. * If this errors out it's because we couldn't invalidate pagecache for
  6540. * this range and we need to fallback to buffered.
  6541. */
  6542. if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
  6543. return -ENOTBLK;
  6544. em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
  6545. if (IS_ERR(em)) {
  6546. ret = PTR_ERR(em);
  6547. goto unlock_err;
  6548. }
  6549. /*
  6550. * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
  6551. * io. INLINE is special, and we could probably kludge it in here, but
  6552. * it's still buffered so for safety lets just fall back to the generic
  6553. * buffered path.
  6554. *
  6555. * For COMPRESSED we _have_ to read the entire extent in so we can
  6556. * decompress it, so there will be buffering required no matter what we
  6557. * do, so go ahead and fallback to buffered.
  6558. *
  6559. * We return -ENOTBLK because thats what makes DIO go ahead and go back
  6560. * to buffered IO. Don't blame me, this is the price we pay for using
  6561. * the generic code.
  6562. */
  6563. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
  6564. em->block_start == EXTENT_MAP_INLINE) {
  6565. free_extent_map(em);
  6566. ret = -ENOTBLK;
  6567. goto unlock_err;
  6568. }
  6569. /* Just a good old fashioned hole, return */
  6570. if (!create && (em->block_start == EXTENT_MAP_HOLE ||
  6571. test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
  6572. free_extent_map(em);
  6573. goto unlock_err;
  6574. }
  6575. /*
  6576. * We don't allocate a new extent in the following cases
  6577. *
  6578. * 1) The inode is marked as NODATACOW. In this case we'll just use the
  6579. * existing extent.
  6580. * 2) The extent is marked as PREALLOC. We're good to go here and can
  6581. * just use the extent.
  6582. *
  6583. */
  6584. if (!create) {
  6585. len = min(len, em->len - (start - em->start));
  6586. lockstart = start + len;
  6587. goto unlock;
  6588. }
  6589. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
  6590. ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
  6591. em->block_start != EXTENT_MAP_HOLE)) {
  6592. int type;
  6593. u64 block_start, orig_start, orig_block_len, ram_bytes;
  6594. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  6595. type = BTRFS_ORDERED_PREALLOC;
  6596. else
  6597. type = BTRFS_ORDERED_NOCOW;
  6598. len = min(len, em->len - (start - em->start));
  6599. block_start = em->block_start + (start - em->start);
  6600. if (can_nocow_extent(inode, start, &len, &orig_start,
  6601. &orig_block_len, &ram_bytes) == 1) {
  6602. if (type == BTRFS_ORDERED_PREALLOC) {
  6603. free_extent_map(em);
  6604. em = create_pinned_em(inode, start, len,
  6605. orig_start,
  6606. block_start, len,
  6607. orig_block_len,
  6608. ram_bytes, type);
  6609. if (IS_ERR(em)) {
  6610. ret = PTR_ERR(em);
  6611. goto unlock_err;
  6612. }
  6613. }
  6614. ret = btrfs_add_ordered_extent_dio(inode, start,
  6615. block_start, len, len, type);
  6616. if (ret) {
  6617. free_extent_map(em);
  6618. goto unlock_err;
  6619. }
  6620. goto unlock;
  6621. }
  6622. }
  6623. /*
  6624. * this will cow the extent, reset the len in case we changed
  6625. * it above
  6626. */
  6627. len = bh_result->b_size;
  6628. free_extent_map(em);
  6629. em = btrfs_new_extent_direct(inode, start, len);
  6630. if (IS_ERR(em)) {
  6631. ret = PTR_ERR(em);
  6632. goto unlock_err;
  6633. }
  6634. len = min(len, em->len - (start - em->start));
  6635. unlock:
  6636. bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
  6637. inode->i_blkbits;
  6638. bh_result->b_size = len;
  6639. bh_result->b_bdev = em->bdev;
  6640. set_buffer_mapped(bh_result);
  6641. if (create) {
  6642. if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  6643. set_buffer_new(bh_result);
  6644. /*
  6645. * Need to update the i_size under the extent lock so buffered
  6646. * readers will get the updated i_size when we unlock.
  6647. */
  6648. if (start + len > i_size_read(inode))
  6649. i_size_write(inode, start + len);
  6650. /*
  6651. * If we have an outstanding_extents count still set then we're
  6652. * within our reservation, otherwise we need to adjust our inode
  6653. * counter appropriately.
  6654. */
  6655. if (dio_data->outstanding_extents) {
  6656. (dio_data->outstanding_extents)--;
  6657. } else {
  6658. spin_lock(&BTRFS_I(inode)->lock);
  6659. BTRFS_I(inode)->outstanding_extents++;
  6660. spin_unlock(&BTRFS_I(inode)->lock);
  6661. }
  6662. btrfs_free_reserved_data_space(inode, len);
  6663. WARN_ON(dio_data->reserve < len);
  6664. dio_data->reserve -= len;
  6665. current->journal_info = dio_data;
  6666. }
  6667. /*
  6668. * In the case of write we need to clear and unlock the entire range,
  6669. * in the case of read we need to unlock only the end area that we
  6670. * aren't using if there is any left over space.
  6671. */
  6672. if (lockstart < lockend) {
  6673. clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
  6674. lockend, unlock_bits, 1, 0,
  6675. &cached_state, GFP_NOFS);
  6676. } else {
  6677. free_extent_state(cached_state);
  6678. }
  6679. free_extent_map(em);
  6680. return 0;
  6681. unlock_err:
  6682. clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  6683. unlock_bits, 1, 0, &cached_state, GFP_NOFS);
  6684. if (dio_data)
  6685. current->journal_info = dio_data;
  6686. return ret;
  6687. }
  6688. static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
  6689. int rw, int mirror_num)
  6690. {
  6691. struct btrfs_root *root = BTRFS_I(inode)->root;
  6692. int ret;
  6693. BUG_ON(rw & REQ_WRITE);
  6694. bio_get(bio);
  6695. ret = btrfs_bio_wq_end_io(root->fs_info, bio,
  6696. BTRFS_WQ_ENDIO_DIO_REPAIR);
  6697. if (ret)
  6698. goto err;
  6699. ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
  6700. err:
  6701. bio_put(bio);
  6702. return ret;
  6703. }
  6704. static int btrfs_check_dio_repairable(struct inode *inode,
  6705. struct bio *failed_bio,
  6706. struct io_failure_record *failrec,
  6707. int failed_mirror)
  6708. {
  6709. int num_copies;
  6710. num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
  6711. failrec->logical, failrec->len);
  6712. if (num_copies == 1) {
  6713. /*
  6714. * we only have a single copy of the data, so don't bother with
  6715. * all the retry and error correction code that follows. no
  6716. * matter what the error is, it is very likely to persist.
  6717. */
  6718. pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n",
  6719. num_copies, failrec->this_mirror, failed_mirror);
  6720. return 0;
  6721. }
  6722. failrec->failed_mirror = failed_mirror;
  6723. failrec->this_mirror++;
  6724. if (failrec->this_mirror == failed_mirror)
  6725. failrec->this_mirror++;
  6726. if (failrec->this_mirror > num_copies) {
  6727. pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n",
  6728. num_copies, failrec->this_mirror, failed_mirror);
  6729. return 0;
  6730. }
  6731. return 1;
  6732. }
  6733. static int dio_read_error(struct inode *inode, struct bio *failed_bio,
  6734. struct page *page, u64 start, u64 end,
  6735. int failed_mirror, bio_end_io_t *repair_endio,
  6736. void *repair_arg)
  6737. {
  6738. struct io_failure_record *failrec;
  6739. struct bio *bio;
  6740. int isector;
  6741. int read_mode;
  6742. int ret;
  6743. BUG_ON(failed_bio->bi_rw & REQ_WRITE);
  6744. ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
  6745. if (ret)
  6746. return ret;
  6747. ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
  6748. failed_mirror);
  6749. if (!ret) {
  6750. free_io_failure(inode, failrec);
  6751. return -EIO;
  6752. }
  6753. if (failed_bio->bi_vcnt > 1)
  6754. read_mode = READ_SYNC | REQ_FAILFAST_DEV;
  6755. else
  6756. read_mode = READ_SYNC;
  6757. isector = start - btrfs_io_bio(failed_bio)->logical;
  6758. isector >>= inode->i_sb->s_blocksize_bits;
  6759. bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
  6760. 0, isector, repair_endio, repair_arg);
  6761. if (!bio) {
  6762. free_io_failure(inode, failrec);
  6763. return -EIO;
  6764. }
  6765. btrfs_debug(BTRFS_I(inode)->root->fs_info,
  6766. "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
  6767. read_mode, failrec->this_mirror, failrec->in_validation);
  6768. ret = submit_dio_repair_bio(inode, bio, read_mode,
  6769. failrec->this_mirror);
  6770. if (ret) {
  6771. free_io_failure(inode, failrec);
  6772. bio_put(bio);
  6773. }
  6774. return ret;
  6775. }
  6776. struct btrfs_retry_complete {
  6777. struct completion done;
  6778. struct inode *inode;
  6779. u64 start;
  6780. int uptodate;
  6781. };
  6782. static void btrfs_retry_endio_nocsum(struct bio *bio)
  6783. {
  6784. struct btrfs_retry_complete *done = bio->bi_private;
  6785. struct bio_vec *bvec;
  6786. int i;
  6787. if (bio->bi_error)
  6788. goto end;
  6789. done->uptodate = 1;
  6790. bio_for_each_segment_all(bvec, bio, i)
  6791. clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
  6792. end:
  6793. complete(&done->done);
  6794. bio_put(bio);
  6795. }
  6796. static int __btrfs_correct_data_nocsum(struct inode *inode,
  6797. struct btrfs_io_bio *io_bio)
  6798. {
  6799. struct bio_vec *bvec;
  6800. struct btrfs_retry_complete done;
  6801. u64 start;
  6802. int i;
  6803. int ret;
  6804. start = io_bio->logical;
  6805. done.inode = inode;
  6806. bio_for_each_segment_all(bvec, &io_bio->bio, i) {
  6807. try_again:
  6808. done.uptodate = 0;
  6809. done.start = start;
  6810. init_completion(&done.done);
  6811. ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
  6812. start + bvec->bv_len - 1,
  6813. io_bio->mirror_num,
  6814. btrfs_retry_endio_nocsum, &done);
  6815. if (ret)
  6816. return ret;
  6817. wait_for_completion(&done.done);
  6818. if (!done.uptodate) {
  6819. /* We might have another mirror, so try again */
  6820. goto try_again;
  6821. }
  6822. start += bvec->bv_len;
  6823. }
  6824. return 0;
  6825. }
  6826. static void btrfs_retry_endio(struct bio *bio)
  6827. {
  6828. struct btrfs_retry_complete *done = bio->bi_private;
  6829. struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
  6830. struct bio_vec *bvec;
  6831. int uptodate;
  6832. int ret;
  6833. int i;
  6834. if (bio->bi_error)
  6835. goto end;
  6836. uptodate = 1;
  6837. bio_for_each_segment_all(bvec, bio, i) {
  6838. ret = __readpage_endio_check(done->inode, io_bio, i,
  6839. bvec->bv_page, 0,
  6840. done->start, bvec->bv_len);
  6841. if (!ret)
  6842. clean_io_failure(done->inode, done->start,
  6843. bvec->bv_page, 0);
  6844. else
  6845. uptodate = 0;
  6846. }
  6847. done->uptodate = uptodate;
  6848. end:
  6849. complete(&done->done);
  6850. bio_put(bio);
  6851. }
  6852. static int __btrfs_subio_endio_read(struct inode *inode,
  6853. struct btrfs_io_bio *io_bio, int err)
  6854. {
  6855. struct bio_vec *bvec;
  6856. struct btrfs_retry_complete done;
  6857. u64 start;
  6858. u64 offset = 0;
  6859. int i;
  6860. int ret;
  6861. err = 0;
  6862. start = io_bio->logical;
  6863. done.inode = inode;
  6864. bio_for_each_segment_all(bvec, &io_bio->bio, i) {
  6865. ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
  6866. 0, start, bvec->bv_len);
  6867. if (likely(!ret))
  6868. goto next;
  6869. try_again:
  6870. done.uptodate = 0;
  6871. done.start = start;
  6872. init_completion(&done.done);
  6873. ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
  6874. start + bvec->bv_len - 1,
  6875. io_bio->mirror_num,
  6876. btrfs_retry_endio, &done);
  6877. if (ret) {
  6878. err = ret;
  6879. goto next;
  6880. }
  6881. wait_for_completion(&done.done);
  6882. if (!done.uptodate) {
  6883. /* We might have another mirror, so try again */
  6884. goto try_again;
  6885. }
  6886. next:
  6887. offset += bvec->bv_len;
  6888. start += bvec->bv_len;
  6889. }
  6890. return err;
  6891. }
  6892. static int btrfs_subio_endio_read(struct inode *inode,
  6893. struct btrfs_io_bio *io_bio, int err)
  6894. {
  6895. bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  6896. if (skip_csum) {
  6897. if (unlikely(err))
  6898. return __btrfs_correct_data_nocsum(inode, io_bio);
  6899. else
  6900. return 0;
  6901. } else {
  6902. return __btrfs_subio_endio_read(inode, io_bio, err);
  6903. }
  6904. }
  6905. static void btrfs_endio_direct_read(struct bio *bio)
  6906. {
  6907. struct btrfs_dio_private *dip = bio->bi_private;
  6908. struct inode *inode = dip->inode;
  6909. struct bio *dio_bio;
  6910. struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
  6911. int err = bio->bi_error;
  6912. if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
  6913. err = btrfs_subio_endio_read(inode, io_bio, err);
  6914. unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
  6915. dip->logical_offset + dip->bytes - 1);
  6916. dio_bio = dip->dio_bio;
  6917. kfree(dip);
  6918. dio_end_io(dio_bio, bio->bi_error);
  6919. if (io_bio->end_io)
  6920. io_bio->end_io(io_bio, err);
  6921. bio_put(bio);
  6922. }
  6923. static void btrfs_endio_direct_write(struct bio *bio)
  6924. {
  6925. struct btrfs_dio_private *dip = bio->bi_private;
  6926. struct inode *inode = dip->inode;
  6927. struct btrfs_root *root = BTRFS_I(inode)->root;
  6928. struct btrfs_ordered_extent *ordered = NULL;
  6929. u64 ordered_offset = dip->logical_offset;
  6930. u64 ordered_bytes = dip->bytes;
  6931. struct bio *dio_bio;
  6932. int ret;
  6933. again:
  6934. ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
  6935. &ordered_offset,
  6936. ordered_bytes,
  6937. !bio->bi_error);
  6938. if (!ret)
  6939. goto out_test;
  6940. btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
  6941. finish_ordered_fn, NULL, NULL);
  6942. btrfs_queue_work(root->fs_info->endio_write_workers,
  6943. &ordered->work);
  6944. out_test:
  6945. /*
  6946. * our bio might span multiple ordered extents. If we haven't
  6947. * completed the accounting for the whole dio, go back and try again
  6948. */
  6949. if (ordered_offset < dip->logical_offset + dip->bytes) {
  6950. ordered_bytes = dip->logical_offset + dip->bytes -
  6951. ordered_offset;
  6952. ordered = NULL;
  6953. goto again;
  6954. }
  6955. dio_bio = dip->dio_bio;
  6956. kfree(dip);
  6957. dio_end_io(dio_bio, bio->bi_error);
  6958. bio_put(bio);
  6959. }
  6960. static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
  6961. struct bio *bio, int mirror_num,
  6962. unsigned long bio_flags, u64 offset)
  6963. {
  6964. int ret;
  6965. struct btrfs_root *root = BTRFS_I(inode)->root;
  6966. ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
  6967. BUG_ON(ret); /* -ENOMEM */
  6968. return 0;
  6969. }
  6970. static void btrfs_end_dio_bio(struct bio *bio)
  6971. {
  6972. struct btrfs_dio_private *dip = bio->bi_private;
  6973. int err = bio->bi_error;
  6974. if (err)
  6975. btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
  6976. "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
  6977. btrfs_ino(dip->inode), bio->bi_rw,
  6978. (unsigned long long)bio->bi_iter.bi_sector,
  6979. bio->bi_iter.bi_size, err);
  6980. if (dip->subio_endio)
  6981. err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
  6982. if (err) {
  6983. dip->errors = 1;
  6984. /*
  6985. * before atomic variable goto zero, we must make sure
  6986. * dip->errors is perceived to be set.
  6987. */
  6988. smp_mb__before_atomic();
  6989. }
  6990. /* if there are more bios still pending for this dio, just exit */
  6991. if (!atomic_dec_and_test(&dip->pending_bios))
  6992. goto out;
  6993. if (dip->errors) {
  6994. bio_io_error(dip->orig_bio);
  6995. } else {
  6996. dip->dio_bio->bi_error = 0;
  6997. bio_endio(dip->orig_bio);
  6998. }
  6999. out:
  7000. bio_put(bio);
  7001. }
  7002. static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
  7003. u64 first_sector, gfp_t gfp_flags)
  7004. {
  7005. struct bio *bio;
  7006. bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags);
  7007. if (bio)
  7008. bio_associate_current(bio);
  7009. return bio;
  7010. }
  7011. static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root,
  7012. struct inode *inode,
  7013. struct btrfs_dio_private *dip,
  7014. struct bio *bio,
  7015. u64 file_offset)
  7016. {
  7017. struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
  7018. struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
  7019. int ret;
  7020. /*
  7021. * We load all the csum data we need when we submit
  7022. * the first bio to reduce the csum tree search and
  7023. * contention.
  7024. */
  7025. if (dip->logical_offset == file_offset) {
  7026. ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio,
  7027. file_offset);
  7028. if (ret)
  7029. return ret;
  7030. }
  7031. if (bio == dip->orig_bio)
  7032. return 0;
  7033. file_offset -= dip->logical_offset;
  7034. file_offset >>= inode->i_sb->s_blocksize_bits;
  7035. io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
  7036. return 0;
  7037. }
  7038. static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
  7039. int rw, u64 file_offset, int skip_sum,
  7040. int async_submit)
  7041. {
  7042. struct btrfs_dio_private *dip = bio->bi_private;
  7043. int write = rw & REQ_WRITE;
  7044. struct btrfs_root *root = BTRFS_I(inode)->root;
  7045. int ret;
  7046. if (async_submit)
  7047. async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
  7048. bio_get(bio);
  7049. if (!write) {
  7050. ret = btrfs_bio_wq_end_io(root->fs_info, bio,
  7051. BTRFS_WQ_ENDIO_DATA);
  7052. if (ret)
  7053. goto err;
  7054. }
  7055. if (skip_sum)
  7056. goto map;
  7057. if (write && async_submit) {
  7058. ret = btrfs_wq_submit_bio(root->fs_info,
  7059. inode, rw, bio, 0, 0,
  7060. file_offset,
  7061. __btrfs_submit_bio_start_direct_io,
  7062. __btrfs_submit_bio_done);
  7063. goto err;
  7064. } else if (write) {
  7065. /*
  7066. * If we aren't doing async submit, calculate the csum of the
  7067. * bio now.
  7068. */
  7069. ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
  7070. if (ret)
  7071. goto err;
  7072. } else {
  7073. ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio,
  7074. file_offset);
  7075. if (ret)
  7076. goto err;
  7077. }
  7078. map:
  7079. ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
  7080. err:
  7081. bio_put(bio);
  7082. return ret;
  7083. }
  7084. static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
  7085. int skip_sum)
  7086. {
  7087. struct inode *inode = dip->inode;
  7088. struct btrfs_root *root = BTRFS_I(inode)->root;
  7089. struct bio *bio;
  7090. struct bio *orig_bio = dip->orig_bio;
  7091. struct bio_vec *bvec = orig_bio->bi_io_vec;
  7092. u64 start_sector = orig_bio->bi_iter.bi_sector;
  7093. u64 file_offset = dip->logical_offset;
  7094. u64 submit_len = 0;
  7095. u64 map_length;
  7096. int nr_pages = 0;
  7097. int ret;
  7098. int async_submit = 0;
  7099. map_length = orig_bio->bi_iter.bi_size;
  7100. ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
  7101. &map_length, NULL, 0);
  7102. if (ret)
  7103. return -EIO;
  7104. if (map_length >= orig_bio->bi_iter.bi_size) {
  7105. bio = orig_bio;
  7106. dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
  7107. goto submit;
  7108. }
  7109. /* async crcs make it difficult to collect full stripe writes. */
  7110. if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
  7111. async_submit = 0;
  7112. else
  7113. async_submit = 1;
  7114. bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
  7115. if (!bio)
  7116. return -ENOMEM;
  7117. bio->bi_private = dip;
  7118. bio->bi_end_io = btrfs_end_dio_bio;
  7119. btrfs_io_bio(bio)->logical = file_offset;
  7120. atomic_inc(&dip->pending_bios);
  7121. while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
  7122. if (map_length < submit_len + bvec->bv_len ||
  7123. bio_add_page(bio, bvec->bv_page, bvec->bv_len,
  7124. bvec->bv_offset) < bvec->bv_len) {
  7125. /*
  7126. * inc the count before we submit the bio so
  7127. * we know the end IO handler won't happen before
  7128. * we inc the count. Otherwise, the dip might get freed
  7129. * before we're done setting it up
  7130. */
  7131. atomic_inc(&dip->pending_bios);
  7132. ret = __btrfs_submit_dio_bio(bio, inode, rw,
  7133. file_offset, skip_sum,
  7134. async_submit);
  7135. if (ret) {
  7136. bio_put(bio);
  7137. atomic_dec(&dip->pending_bios);
  7138. goto out_err;
  7139. }
  7140. start_sector += submit_len >> 9;
  7141. file_offset += submit_len;
  7142. submit_len = 0;
  7143. nr_pages = 0;
  7144. bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
  7145. start_sector, GFP_NOFS);
  7146. if (!bio)
  7147. goto out_err;
  7148. bio->bi_private = dip;
  7149. bio->bi_end_io = btrfs_end_dio_bio;
  7150. btrfs_io_bio(bio)->logical = file_offset;
  7151. map_length = orig_bio->bi_iter.bi_size;
  7152. ret = btrfs_map_block(root->fs_info, rw,
  7153. start_sector << 9,
  7154. &map_length, NULL, 0);
  7155. if (ret) {
  7156. bio_put(bio);
  7157. goto out_err;
  7158. }
  7159. } else {
  7160. submit_len += bvec->bv_len;
  7161. nr_pages++;
  7162. bvec++;
  7163. }
  7164. }
  7165. submit:
  7166. ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
  7167. async_submit);
  7168. if (!ret)
  7169. return 0;
  7170. bio_put(bio);
  7171. out_err:
  7172. dip->errors = 1;
  7173. /*
  7174. * before atomic variable goto zero, we must
  7175. * make sure dip->errors is perceived to be set.
  7176. */
  7177. smp_mb__before_atomic();
  7178. if (atomic_dec_and_test(&dip->pending_bios))
  7179. bio_io_error(dip->orig_bio);
  7180. /* bio_end_io() will handle error, so we needn't return it */
  7181. return 0;
  7182. }
  7183. static void btrfs_submit_direct(int rw, struct bio *dio_bio,
  7184. struct inode *inode, loff_t file_offset)
  7185. {
  7186. struct btrfs_dio_private *dip = NULL;
  7187. struct bio *io_bio = NULL;
  7188. struct btrfs_io_bio *btrfs_bio;
  7189. int skip_sum;
  7190. int write = rw & REQ_WRITE;
  7191. int ret = 0;
  7192. skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  7193. io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
  7194. if (!io_bio) {
  7195. ret = -ENOMEM;
  7196. goto free_ordered;
  7197. }
  7198. dip = kzalloc(sizeof(*dip), GFP_NOFS);
  7199. if (!dip) {
  7200. ret = -ENOMEM;
  7201. goto free_ordered;
  7202. }
  7203. dip->private = dio_bio->bi_private;
  7204. dip->inode = inode;
  7205. dip->logical_offset = file_offset;
  7206. dip->bytes = dio_bio->bi_iter.bi_size;
  7207. dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
  7208. io_bio->bi_private = dip;
  7209. dip->orig_bio = io_bio;
  7210. dip->dio_bio = dio_bio;
  7211. atomic_set(&dip->pending_bios, 0);
  7212. btrfs_bio = btrfs_io_bio(io_bio);
  7213. btrfs_bio->logical = file_offset;
  7214. if (write) {
  7215. io_bio->bi_end_io = btrfs_endio_direct_write;
  7216. } else {
  7217. io_bio->bi_end_io = btrfs_endio_direct_read;
  7218. dip->subio_endio = btrfs_subio_endio_read;
  7219. }
  7220. ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
  7221. if (!ret)
  7222. return;
  7223. if (btrfs_bio->end_io)
  7224. btrfs_bio->end_io(btrfs_bio, ret);
  7225. free_ordered:
  7226. /*
  7227. * If we arrived here it means either we failed to submit the dip
  7228. * or we either failed to clone the dio_bio or failed to allocate the
  7229. * dip. If we cloned the dio_bio and allocated the dip, we can just
  7230. * call bio_endio against our io_bio so that we get proper resource
  7231. * cleanup if we fail to submit the dip, otherwise, we must do the
  7232. * same as btrfs_endio_direct_[write|read] because we can't call these
  7233. * callbacks - they require an allocated dip and a clone of dio_bio.
  7234. */
  7235. if (io_bio && dip) {
  7236. io_bio->bi_error = -EIO;
  7237. bio_endio(io_bio);
  7238. /*
  7239. * The end io callbacks free our dip, do the final put on io_bio
  7240. * and all the cleanup and final put for dio_bio (through
  7241. * dio_end_io()).
  7242. */
  7243. dip = NULL;
  7244. io_bio = NULL;
  7245. } else {
  7246. if (write) {
  7247. struct btrfs_ordered_extent *ordered;
  7248. ordered = btrfs_lookup_ordered_extent(inode,
  7249. file_offset);
  7250. set_bit(BTRFS_ORDERED_IOERR, &ordered->flags);
  7251. /*
  7252. * Decrements our ref on the ordered extent and removes
  7253. * the ordered extent from the inode's ordered tree,
  7254. * doing all the proper resource cleanup such as for the
  7255. * reserved space and waking up any waiters for this
  7256. * ordered extent (through btrfs_remove_ordered_extent).
  7257. */
  7258. btrfs_finish_ordered_io(ordered);
  7259. } else {
  7260. unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
  7261. file_offset + dio_bio->bi_iter.bi_size - 1);
  7262. }
  7263. dio_bio->bi_error = -EIO;
  7264. /*
  7265. * Releases and cleans up our dio_bio, no need to bio_put()
  7266. * nor bio_endio()/bio_io_error() against dio_bio.
  7267. */
  7268. dio_end_io(dio_bio, ret);
  7269. }
  7270. if (io_bio)
  7271. bio_put(io_bio);
  7272. kfree(dip);
  7273. }
  7274. static ssize_t check_direct_IO(struct btrfs_root *root, struct kiocb *iocb,
  7275. const struct iov_iter *iter, loff_t offset)
  7276. {
  7277. int seg;
  7278. int i;
  7279. unsigned blocksize_mask = root->sectorsize - 1;
  7280. ssize_t retval = -EINVAL;
  7281. if (offset & blocksize_mask)
  7282. goto out;
  7283. if (iov_iter_alignment(iter) & blocksize_mask)
  7284. goto out;
  7285. /* If this is a write we don't need to check anymore */
  7286. if (iov_iter_rw(iter) == WRITE)
  7287. return 0;
  7288. /*
  7289. * Check to make sure we don't have duplicate iov_base's in this
  7290. * iovec, if so return EINVAL, otherwise we'll get csum errors
  7291. * when reading back.
  7292. */
  7293. for (seg = 0; seg < iter->nr_segs; seg++) {
  7294. for (i = seg + 1; i < iter->nr_segs; i++) {
  7295. if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
  7296. goto out;
  7297. }
  7298. }
  7299. retval = 0;
  7300. out:
  7301. return retval;
  7302. }
  7303. static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
  7304. loff_t offset)
  7305. {
  7306. struct file *file = iocb->ki_filp;
  7307. struct inode *inode = file->f_mapping->host;
  7308. struct btrfs_root *root = BTRFS_I(inode)->root;
  7309. struct btrfs_dio_data dio_data = { 0 };
  7310. size_t count = 0;
  7311. int flags = 0;
  7312. bool wakeup = true;
  7313. bool relock = false;
  7314. ssize_t ret;
  7315. if (check_direct_IO(BTRFS_I(inode)->root, iocb, iter, offset))
  7316. return 0;
  7317. inode_dio_begin(inode);
  7318. smp_mb__after_atomic();
  7319. /*
  7320. * The generic stuff only does filemap_write_and_wait_range, which
  7321. * isn't enough if we've written compressed pages to this area, so
  7322. * we need to flush the dirty pages again to make absolutely sure
  7323. * that any outstanding dirty pages are on disk.
  7324. */
  7325. count = iov_iter_count(iter);
  7326. if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
  7327. &BTRFS_I(inode)->runtime_flags))
  7328. filemap_fdatawrite_range(inode->i_mapping, offset,
  7329. offset + count - 1);
  7330. if (iov_iter_rw(iter) == WRITE) {
  7331. /*
  7332. * If the write DIO is beyond the EOF, we need update
  7333. * the isize, but it is protected by i_mutex. So we can
  7334. * not unlock the i_mutex at this case.
  7335. */
  7336. if (offset + count <= inode->i_size) {
  7337. mutex_unlock(&inode->i_mutex);
  7338. relock = true;
  7339. }
  7340. ret = btrfs_delalloc_reserve_space(inode, count);
  7341. if (ret)
  7342. goto out;
  7343. dio_data.outstanding_extents = div64_u64(count +
  7344. BTRFS_MAX_EXTENT_SIZE - 1,
  7345. BTRFS_MAX_EXTENT_SIZE);
  7346. /*
  7347. * We need to know how many extents we reserved so that we can
  7348. * do the accounting properly if we go over the number we
  7349. * originally calculated. Abuse current->journal_info for this.
  7350. */
  7351. dio_data.reserve = round_up(count, root->sectorsize);
  7352. current->journal_info = &dio_data;
  7353. } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
  7354. &BTRFS_I(inode)->runtime_flags)) {
  7355. inode_dio_end(inode);
  7356. flags = DIO_LOCKING | DIO_SKIP_HOLES;
  7357. wakeup = false;
  7358. }
  7359. ret = __blockdev_direct_IO(iocb, inode,
  7360. BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
  7361. iter, offset, btrfs_get_blocks_direct, NULL,
  7362. btrfs_submit_direct, flags);
  7363. if (iov_iter_rw(iter) == WRITE) {
  7364. current->journal_info = NULL;
  7365. if (ret < 0 && ret != -EIOCBQUEUED) {
  7366. if (dio_data.reserve)
  7367. btrfs_delalloc_release_space(inode,
  7368. dio_data.reserve);
  7369. } else if (ret >= 0 && (size_t)ret < count)
  7370. btrfs_delalloc_release_space(inode,
  7371. count - (size_t)ret);
  7372. }
  7373. out:
  7374. if (wakeup)
  7375. inode_dio_end(inode);
  7376. if (relock)
  7377. mutex_lock(&inode->i_mutex);
  7378. return ret;
  7379. }
  7380. #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
  7381. static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  7382. __u64 start, __u64 len)
  7383. {
  7384. int ret;
  7385. ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
  7386. if (ret)
  7387. return ret;
  7388. return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
  7389. }
  7390. int btrfs_readpage(struct file *file, struct page *page)
  7391. {
  7392. struct extent_io_tree *tree;
  7393. tree = &BTRFS_I(page->mapping->host)->io_tree;
  7394. return extent_read_full_page(tree, page, btrfs_get_extent, 0);
  7395. }
  7396. static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
  7397. {
  7398. struct extent_io_tree *tree;
  7399. if (current->flags & PF_MEMALLOC) {
  7400. redirty_page_for_writepage(wbc, page);
  7401. unlock_page(page);
  7402. return 0;
  7403. }
  7404. tree = &BTRFS_I(page->mapping->host)->io_tree;
  7405. return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
  7406. }
  7407. static int btrfs_writepages(struct address_space *mapping,
  7408. struct writeback_control *wbc)
  7409. {
  7410. struct extent_io_tree *tree;
  7411. tree = &BTRFS_I(mapping->host)->io_tree;
  7412. return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
  7413. }
  7414. static int
  7415. btrfs_readpages(struct file *file, struct address_space *mapping,
  7416. struct list_head *pages, unsigned nr_pages)
  7417. {
  7418. struct extent_io_tree *tree;
  7419. tree = &BTRFS_I(mapping->host)->io_tree;
  7420. return extent_readpages(tree, mapping, pages, nr_pages,
  7421. btrfs_get_extent);
  7422. }
  7423. static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  7424. {
  7425. struct extent_io_tree *tree;
  7426. struct extent_map_tree *map;
  7427. int ret;
  7428. tree = &BTRFS_I(page->mapping->host)->io_tree;
  7429. map = &BTRFS_I(page->mapping->host)->extent_tree;
  7430. ret = try_release_extent_mapping(map, tree, page, gfp_flags);
  7431. if (ret == 1) {
  7432. ClearPagePrivate(page);
  7433. set_page_private(page, 0);
  7434. page_cache_release(page);
  7435. }
  7436. return ret;
  7437. }
  7438. static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  7439. {
  7440. if (PageWriteback(page) || PageDirty(page))
  7441. return 0;
  7442. return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
  7443. }
  7444. static void btrfs_invalidatepage(struct page *page, unsigned int offset,
  7445. unsigned int length)
  7446. {
  7447. struct inode *inode = page->mapping->host;
  7448. struct extent_io_tree *tree;
  7449. struct btrfs_ordered_extent *ordered;
  7450. struct extent_state *cached_state = NULL;
  7451. u64 page_start = page_offset(page);
  7452. u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
  7453. int inode_evicting = inode->i_state & I_FREEING;
  7454. /*
  7455. * we have the page locked, so new writeback can't start,
  7456. * and the dirty bit won't be cleared while we are here.
  7457. *
  7458. * Wait for IO on this page so that we can safely clear
  7459. * the PagePrivate2 bit and do ordered accounting
  7460. */
  7461. wait_on_page_writeback(page);
  7462. tree = &BTRFS_I(inode)->io_tree;
  7463. if (offset) {
  7464. btrfs_releasepage(page, GFP_NOFS);
  7465. return;
  7466. }
  7467. if (!inode_evicting)
  7468. lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
  7469. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  7470. if (ordered) {
  7471. /*
  7472. * IO on this page will never be started, so we need
  7473. * to account for any ordered extents now
  7474. */
  7475. if (!inode_evicting)
  7476. clear_extent_bit(tree, page_start, page_end,
  7477. EXTENT_DIRTY | EXTENT_DELALLOC |
  7478. EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
  7479. EXTENT_DEFRAG, 1, 0, &cached_state,
  7480. GFP_NOFS);
  7481. /*
  7482. * whoever cleared the private bit is responsible
  7483. * for the finish_ordered_io
  7484. */
  7485. if (TestClearPagePrivate2(page)) {
  7486. struct btrfs_ordered_inode_tree *tree;
  7487. u64 new_len;
  7488. tree = &BTRFS_I(inode)->ordered_tree;
  7489. spin_lock_irq(&tree->lock);
  7490. set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
  7491. new_len = page_start - ordered->file_offset;
  7492. if (new_len < ordered->truncated_len)
  7493. ordered->truncated_len = new_len;
  7494. spin_unlock_irq(&tree->lock);
  7495. if (btrfs_dec_test_ordered_pending(inode, &ordered,
  7496. page_start,
  7497. PAGE_CACHE_SIZE, 1))
  7498. btrfs_finish_ordered_io(ordered);
  7499. }
  7500. btrfs_put_ordered_extent(ordered);
  7501. if (!inode_evicting) {
  7502. cached_state = NULL;
  7503. lock_extent_bits(tree, page_start, page_end, 0,
  7504. &cached_state);
  7505. }
  7506. }
  7507. if (!inode_evicting) {
  7508. clear_extent_bit(tree, page_start, page_end,
  7509. EXTENT_LOCKED | EXTENT_DIRTY |
  7510. EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
  7511. EXTENT_DEFRAG, 1, 1,
  7512. &cached_state, GFP_NOFS);
  7513. __btrfs_releasepage(page, GFP_NOFS);
  7514. }
  7515. ClearPageChecked(page);
  7516. if (PagePrivate(page)) {
  7517. ClearPagePrivate(page);
  7518. set_page_private(page, 0);
  7519. page_cache_release(page);
  7520. }
  7521. }
  7522. /*
  7523. * btrfs_page_mkwrite() is not allowed to change the file size as it gets
  7524. * called from a page fault handler when a page is first dirtied. Hence we must
  7525. * be careful to check for EOF conditions here. We set the page up correctly
  7526. * for a written page which means we get ENOSPC checking when writing into
  7527. * holes and correct delalloc and unwritten extent mapping on filesystems that
  7528. * support these features.
  7529. *
  7530. * We are not allowed to take the i_mutex here so we have to play games to
  7531. * protect against truncate races as the page could now be beyond EOF. Because
  7532. * vmtruncate() writes the inode size before removing pages, once we have the
  7533. * page lock we can determine safely if the page is beyond EOF. If it is not
  7534. * beyond EOF, then the page is guaranteed safe against truncation until we
  7535. * unlock the page.
  7536. */
  7537. int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  7538. {
  7539. struct page *page = vmf->page;
  7540. struct inode *inode = file_inode(vma->vm_file);
  7541. struct btrfs_root *root = BTRFS_I(inode)->root;
  7542. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  7543. struct btrfs_ordered_extent *ordered;
  7544. struct extent_state *cached_state = NULL;
  7545. char *kaddr;
  7546. unsigned long zero_start;
  7547. loff_t size;
  7548. int ret;
  7549. int reserved = 0;
  7550. u64 page_start;
  7551. u64 page_end;
  7552. sb_start_pagefault(inode->i_sb);
  7553. ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
  7554. if (!ret) {
  7555. ret = file_update_time(vma->vm_file);
  7556. reserved = 1;
  7557. }
  7558. if (ret) {
  7559. if (ret == -ENOMEM)
  7560. ret = VM_FAULT_OOM;
  7561. else /* -ENOSPC, -EIO, etc */
  7562. ret = VM_FAULT_SIGBUS;
  7563. if (reserved)
  7564. goto out;
  7565. goto out_noreserve;
  7566. }
  7567. ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
  7568. again:
  7569. lock_page(page);
  7570. size = i_size_read(inode);
  7571. page_start = page_offset(page);
  7572. page_end = page_start + PAGE_CACHE_SIZE - 1;
  7573. if ((page->mapping != inode->i_mapping) ||
  7574. (page_start >= size)) {
  7575. /* page got truncated out from underneath us */
  7576. goto out_unlock;
  7577. }
  7578. wait_on_page_writeback(page);
  7579. lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
  7580. set_page_extent_mapped(page);
  7581. /*
  7582. * we can't set the delalloc bits if there are pending ordered
  7583. * extents. Drop our locks and wait for them to finish
  7584. */
  7585. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  7586. if (ordered) {
  7587. unlock_extent_cached(io_tree, page_start, page_end,
  7588. &cached_state, GFP_NOFS);
  7589. unlock_page(page);
  7590. btrfs_start_ordered_extent(inode, ordered, 1);
  7591. btrfs_put_ordered_extent(ordered);
  7592. goto again;
  7593. }
  7594. /*
  7595. * XXX - page_mkwrite gets called every time the page is dirtied, even
  7596. * if it was already dirty, so for space accounting reasons we need to
  7597. * clear any delalloc bits for the range we are fixing to save. There
  7598. * is probably a better way to do this, but for now keep consistent with
  7599. * prepare_pages in the normal write path.
  7600. */
  7601. clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
  7602. EXTENT_DIRTY | EXTENT_DELALLOC |
  7603. EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
  7604. 0, 0, &cached_state, GFP_NOFS);
  7605. ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
  7606. &cached_state);
  7607. if (ret) {
  7608. unlock_extent_cached(io_tree, page_start, page_end,
  7609. &cached_state, GFP_NOFS);
  7610. ret = VM_FAULT_SIGBUS;
  7611. goto out_unlock;
  7612. }
  7613. ret = 0;
  7614. /* page is wholly or partially inside EOF */
  7615. if (page_start + PAGE_CACHE_SIZE > size)
  7616. zero_start = size & ~PAGE_CACHE_MASK;
  7617. else
  7618. zero_start = PAGE_CACHE_SIZE;
  7619. if (zero_start != PAGE_CACHE_SIZE) {
  7620. kaddr = kmap(page);
  7621. memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
  7622. flush_dcache_page(page);
  7623. kunmap(page);
  7624. }
  7625. ClearPageChecked(page);
  7626. set_page_dirty(page);
  7627. SetPageUptodate(page);
  7628. BTRFS_I(inode)->last_trans = root->fs_info->generation;
  7629. BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
  7630. BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
  7631. unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
  7632. out_unlock:
  7633. if (!ret) {
  7634. sb_end_pagefault(inode->i_sb);
  7635. return VM_FAULT_LOCKED;
  7636. }
  7637. unlock_page(page);
  7638. out:
  7639. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  7640. out_noreserve:
  7641. sb_end_pagefault(inode->i_sb);
  7642. return ret;
  7643. }
  7644. static int btrfs_truncate(struct inode *inode)
  7645. {
  7646. struct btrfs_root *root = BTRFS_I(inode)->root;
  7647. struct btrfs_block_rsv *rsv;
  7648. int ret = 0;
  7649. int err = 0;
  7650. struct btrfs_trans_handle *trans;
  7651. u64 mask = root->sectorsize - 1;
  7652. u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
  7653. ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
  7654. (u64)-1);
  7655. if (ret)
  7656. return ret;
  7657. /*
  7658. * Yes ladies and gentelment, this is indeed ugly. The fact is we have
  7659. * 3 things going on here
  7660. *
  7661. * 1) We need to reserve space for our orphan item and the space to
  7662. * delete our orphan item. Lord knows we don't want to have a dangling
  7663. * orphan item because we didn't reserve space to remove it.
  7664. *
  7665. * 2) We need to reserve space to update our inode.
  7666. *
  7667. * 3) We need to have something to cache all the space that is going to
  7668. * be free'd up by the truncate operation, but also have some slack
  7669. * space reserved in case it uses space during the truncate (thank you
  7670. * very much snapshotting).
  7671. *
  7672. * And we need these to all be seperate. The fact is we can use alot of
  7673. * space doing the truncate, and we have no earthly idea how much space
  7674. * we will use, so we need the truncate reservation to be seperate so it
  7675. * doesn't end up using space reserved for updating the inode or
  7676. * removing the orphan item. We also need to be able to stop the
  7677. * transaction and start a new one, which means we need to be able to
  7678. * update the inode several times, and we have no idea of knowing how
  7679. * many times that will be, so we can't just reserve 1 item for the
  7680. * entirety of the opration, so that has to be done seperately as well.
  7681. * Then there is the orphan item, which does indeed need to be held on
  7682. * to for the whole operation, and we need nobody to touch this reserved
  7683. * space except the orphan code.
  7684. *
  7685. * So that leaves us with
  7686. *
  7687. * 1) root->orphan_block_rsv - for the orphan deletion.
  7688. * 2) rsv - for the truncate reservation, which we will steal from the
  7689. * transaction reservation.
  7690. * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
  7691. * updating the inode.
  7692. */
  7693. rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
  7694. if (!rsv)
  7695. return -ENOMEM;
  7696. rsv->size = min_size;
  7697. rsv->failfast = 1;
  7698. /*
  7699. * 1 for the truncate slack space
  7700. * 1 for updating the inode.
  7701. */
  7702. trans = btrfs_start_transaction(root, 2);
  7703. if (IS_ERR(trans)) {
  7704. err = PTR_ERR(trans);
  7705. goto out;
  7706. }
  7707. /* Migrate the slack space for the truncate to our reserve */
  7708. ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
  7709. min_size);
  7710. BUG_ON(ret);
  7711. /*
  7712. * So if we truncate and then write and fsync we normally would just
  7713. * write the extents that changed, which is a problem if we need to
  7714. * first truncate that entire inode. So set this flag so we write out
  7715. * all of the extents in the inode to the sync log so we're completely
  7716. * safe.
  7717. */
  7718. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
  7719. trans->block_rsv = rsv;
  7720. while (1) {
  7721. ret = btrfs_truncate_inode_items(trans, root, inode,
  7722. inode->i_size,
  7723. BTRFS_EXTENT_DATA_KEY);
  7724. if (ret != -ENOSPC && ret != -EAGAIN) {
  7725. err = ret;
  7726. break;
  7727. }
  7728. trans->block_rsv = &root->fs_info->trans_block_rsv;
  7729. ret = btrfs_update_inode(trans, root, inode);
  7730. if (ret) {
  7731. err = ret;
  7732. break;
  7733. }
  7734. btrfs_end_transaction(trans, root);
  7735. btrfs_btree_balance_dirty(root);
  7736. trans = btrfs_start_transaction(root, 2);
  7737. if (IS_ERR(trans)) {
  7738. ret = err = PTR_ERR(trans);
  7739. trans = NULL;
  7740. break;
  7741. }
  7742. ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
  7743. rsv, min_size);
  7744. BUG_ON(ret); /* shouldn't happen */
  7745. trans->block_rsv = rsv;
  7746. }
  7747. if (ret == 0 && inode->i_nlink > 0) {
  7748. trans->block_rsv = root->orphan_block_rsv;
  7749. ret = btrfs_orphan_del(trans, inode);
  7750. if (ret)
  7751. err = ret;
  7752. }
  7753. if (trans) {
  7754. trans->block_rsv = &root->fs_info->trans_block_rsv;
  7755. ret = btrfs_update_inode(trans, root, inode);
  7756. if (ret && !err)
  7757. err = ret;
  7758. ret = btrfs_end_transaction(trans, root);
  7759. btrfs_btree_balance_dirty(root);
  7760. }
  7761. out:
  7762. btrfs_free_block_rsv(root, rsv);
  7763. if (ret && !err)
  7764. err = ret;
  7765. return err;
  7766. }
  7767. /*
  7768. * create a new subvolume directory/inode (helper for the ioctl).
  7769. */
  7770. int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
  7771. struct btrfs_root *new_root,
  7772. struct btrfs_root *parent_root,
  7773. u64 new_dirid)
  7774. {
  7775. struct inode *inode;
  7776. int err;
  7777. u64 index = 0;
  7778. inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
  7779. new_dirid, new_dirid,
  7780. S_IFDIR | (~current_umask() & S_IRWXUGO),
  7781. &index);
  7782. if (IS_ERR(inode))
  7783. return PTR_ERR(inode);
  7784. inode->i_op = &btrfs_dir_inode_operations;
  7785. inode->i_fop = &btrfs_dir_file_operations;
  7786. set_nlink(inode, 1);
  7787. btrfs_i_size_write(inode, 0);
  7788. unlock_new_inode(inode);
  7789. err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
  7790. if (err)
  7791. btrfs_err(new_root->fs_info,
  7792. "error inheriting subvolume %llu properties: %d",
  7793. new_root->root_key.objectid, err);
  7794. err = btrfs_update_inode(trans, new_root, inode);
  7795. iput(inode);
  7796. return err;
  7797. }
  7798. struct inode *btrfs_alloc_inode(struct super_block *sb)
  7799. {
  7800. struct btrfs_inode *ei;
  7801. struct inode *inode;
  7802. ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
  7803. if (!ei)
  7804. return NULL;
  7805. ei->root = NULL;
  7806. ei->generation = 0;
  7807. ei->last_trans = 0;
  7808. ei->last_sub_trans = 0;
  7809. ei->logged_trans = 0;
  7810. ei->delalloc_bytes = 0;
  7811. ei->defrag_bytes = 0;
  7812. ei->disk_i_size = 0;
  7813. ei->flags = 0;
  7814. ei->csum_bytes = 0;
  7815. ei->index_cnt = (u64)-1;
  7816. ei->dir_index = 0;
  7817. ei->last_unlink_trans = 0;
  7818. ei->last_log_commit = 0;
  7819. spin_lock_init(&ei->lock);
  7820. ei->outstanding_extents = 0;
  7821. ei->reserved_extents = 0;
  7822. ei->runtime_flags = 0;
  7823. ei->force_compress = BTRFS_COMPRESS_NONE;
  7824. ei->delayed_node = NULL;
  7825. ei->i_otime.tv_sec = 0;
  7826. ei->i_otime.tv_nsec = 0;
  7827. inode = &ei->vfs_inode;
  7828. extent_map_tree_init(&ei->extent_tree);
  7829. extent_io_tree_init(&ei->io_tree, &inode->i_data);
  7830. extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
  7831. ei->io_tree.track_uptodate = 1;
  7832. ei->io_failure_tree.track_uptodate = 1;
  7833. atomic_set(&ei->sync_writers, 0);
  7834. mutex_init(&ei->log_mutex);
  7835. mutex_init(&ei->delalloc_mutex);
  7836. btrfs_ordered_inode_tree_init(&ei->ordered_tree);
  7837. INIT_LIST_HEAD(&ei->delalloc_inodes);
  7838. RB_CLEAR_NODE(&ei->rb_node);
  7839. return inode;
  7840. }
  7841. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  7842. void btrfs_test_destroy_inode(struct inode *inode)
  7843. {
  7844. btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
  7845. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  7846. }
  7847. #endif
  7848. static void btrfs_i_callback(struct rcu_head *head)
  7849. {
  7850. struct inode *inode = container_of(head, struct inode, i_rcu);
  7851. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  7852. }
  7853. void btrfs_destroy_inode(struct inode *inode)
  7854. {
  7855. struct btrfs_ordered_extent *ordered;
  7856. struct btrfs_root *root = BTRFS_I(inode)->root;
  7857. WARN_ON(!hlist_empty(&inode->i_dentry));
  7858. WARN_ON(inode->i_data.nrpages);
  7859. WARN_ON(BTRFS_I(inode)->outstanding_extents);
  7860. WARN_ON(BTRFS_I(inode)->reserved_extents);
  7861. WARN_ON(BTRFS_I(inode)->delalloc_bytes);
  7862. WARN_ON(BTRFS_I(inode)->csum_bytes);
  7863. WARN_ON(BTRFS_I(inode)->defrag_bytes);
  7864. /*
  7865. * This can happen where we create an inode, but somebody else also
  7866. * created the same inode and we need to destroy the one we already
  7867. * created.
  7868. */
  7869. if (!root)
  7870. goto free;
  7871. if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
  7872. &BTRFS_I(inode)->runtime_flags)) {
  7873. btrfs_info(root->fs_info, "inode %llu still on the orphan list",
  7874. btrfs_ino(inode));
  7875. atomic_dec(&root->orphan_inodes);
  7876. }
  7877. while (1) {
  7878. ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
  7879. if (!ordered)
  7880. break;
  7881. else {
  7882. btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
  7883. ordered->file_offset, ordered->len);
  7884. btrfs_remove_ordered_extent(inode, ordered);
  7885. btrfs_put_ordered_extent(ordered);
  7886. btrfs_put_ordered_extent(ordered);
  7887. }
  7888. }
  7889. inode_tree_del(inode);
  7890. btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
  7891. free:
  7892. call_rcu(&inode->i_rcu, btrfs_i_callback);
  7893. }
  7894. int btrfs_drop_inode(struct inode *inode)
  7895. {
  7896. struct btrfs_root *root = BTRFS_I(inode)->root;
  7897. if (root == NULL)
  7898. return 1;
  7899. /* the snap/subvol tree is on deleting */
  7900. if (btrfs_root_refs(&root->root_item) == 0)
  7901. return 1;
  7902. else
  7903. return generic_drop_inode(inode);
  7904. }
  7905. static void init_once(void *foo)
  7906. {
  7907. struct btrfs_inode *ei = (struct btrfs_inode *) foo;
  7908. inode_init_once(&ei->vfs_inode);
  7909. }
  7910. void btrfs_destroy_cachep(void)
  7911. {
  7912. /*
  7913. * Make sure all delayed rcu free inodes are flushed before we
  7914. * destroy cache.
  7915. */
  7916. rcu_barrier();
  7917. if (btrfs_inode_cachep)
  7918. kmem_cache_destroy(btrfs_inode_cachep);
  7919. if (btrfs_trans_handle_cachep)
  7920. kmem_cache_destroy(btrfs_trans_handle_cachep);
  7921. if (btrfs_transaction_cachep)
  7922. kmem_cache_destroy(btrfs_transaction_cachep);
  7923. if (btrfs_path_cachep)
  7924. kmem_cache_destroy(btrfs_path_cachep);
  7925. if (btrfs_free_space_cachep)
  7926. kmem_cache_destroy(btrfs_free_space_cachep);
  7927. if (btrfs_delalloc_work_cachep)
  7928. kmem_cache_destroy(btrfs_delalloc_work_cachep);
  7929. }
  7930. int btrfs_init_cachep(void)
  7931. {
  7932. btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
  7933. sizeof(struct btrfs_inode), 0,
  7934. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
  7935. if (!btrfs_inode_cachep)
  7936. goto fail;
  7937. btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
  7938. sizeof(struct btrfs_trans_handle), 0,
  7939. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  7940. if (!btrfs_trans_handle_cachep)
  7941. goto fail;
  7942. btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
  7943. sizeof(struct btrfs_transaction), 0,
  7944. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  7945. if (!btrfs_transaction_cachep)
  7946. goto fail;
  7947. btrfs_path_cachep = kmem_cache_create("btrfs_path",
  7948. sizeof(struct btrfs_path), 0,
  7949. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  7950. if (!btrfs_path_cachep)
  7951. goto fail;
  7952. btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
  7953. sizeof(struct btrfs_free_space), 0,
  7954. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  7955. if (!btrfs_free_space_cachep)
  7956. goto fail;
  7957. btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
  7958. sizeof(struct btrfs_delalloc_work), 0,
  7959. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
  7960. NULL);
  7961. if (!btrfs_delalloc_work_cachep)
  7962. goto fail;
  7963. return 0;
  7964. fail:
  7965. btrfs_destroy_cachep();
  7966. return -ENOMEM;
  7967. }
  7968. static int btrfs_getattr(struct vfsmount *mnt,
  7969. struct dentry *dentry, struct kstat *stat)
  7970. {
  7971. u64 delalloc_bytes;
  7972. struct inode *inode = d_inode(dentry);
  7973. u32 blocksize = inode->i_sb->s_blocksize;
  7974. generic_fillattr(inode, stat);
  7975. stat->dev = BTRFS_I(inode)->root->anon_dev;
  7976. stat->blksize = PAGE_CACHE_SIZE;
  7977. spin_lock(&BTRFS_I(inode)->lock);
  7978. delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
  7979. spin_unlock(&BTRFS_I(inode)->lock);
  7980. stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
  7981. ALIGN(delalloc_bytes, blocksize)) >> 9;
  7982. return 0;
  7983. }
  7984. static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  7985. struct inode *new_dir, struct dentry *new_dentry)
  7986. {
  7987. struct btrfs_trans_handle *trans;
  7988. struct btrfs_root *root = BTRFS_I(old_dir)->root;
  7989. struct btrfs_root *dest = BTRFS_I(new_dir)->root;
  7990. struct inode *new_inode = d_inode(new_dentry);
  7991. struct inode *old_inode = d_inode(old_dentry);
  7992. struct timespec ctime = CURRENT_TIME;
  7993. u64 index = 0;
  7994. u64 root_objectid;
  7995. int ret;
  7996. u64 old_ino = btrfs_ino(old_inode);
  7997. if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
  7998. return -EPERM;
  7999. /* we only allow rename subvolume link between subvolumes */
  8000. if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
  8001. return -EXDEV;
  8002. if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
  8003. (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
  8004. return -ENOTEMPTY;
  8005. if (S_ISDIR(old_inode->i_mode) && new_inode &&
  8006. new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
  8007. return -ENOTEMPTY;
  8008. /* check for collisions, even if the name isn't there */
  8009. ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
  8010. new_dentry->d_name.name,
  8011. new_dentry->d_name.len);
  8012. if (ret) {
  8013. if (ret == -EEXIST) {
  8014. /* we shouldn't get
  8015. * eexist without a new_inode */
  8016. if (WARN_ON(!new_inode)) {
  8017. return ret;
  8018. }
  8019. } else {
  8020. /* maybe -EOVERFLOW */
  8021. return ret;
  8022. }
  8023. }
  8024. ret = 0;
  8025. /*
  8026. * we're using rename to replace one file with another. Start IO on it
  8027. * now so we don't add too much work to the end of the transaction
  8028. */
  8029. if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
  8030. filemap_flush(old_inode->i_mapping);
  8031. /* close the racy window with snapshot create/destroy ioctl */
  8032. if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
  8033. down_read(&root->fs_info->subvol_sem);
  8034. /*
  8035. * We want to reserve the absolute worst case amount of items. So if
  8036. * both inodes are subvols and we need to unlink them then that would
  8037. * require 4 item modifications, but if they are both normal inodes it
  8038. * would require 5 item modifications, so we'll assume their normal
  8039. * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
  8040. * should cover the worst case number of items we'll modify.
  8041. */
  8042. trans = btrfs_start_transaction(root, 11);
  8043. if (IS_ERR(trans)) {
  8044. ret = PTR_ERR(trans);
  8045. goto out_notrans;
  8046. }
  8047. if (dest != root)
  8048. btrfs_record_root_in_trans(trans, dest);
  8049. ret = btrfs_set_inode_index(new_dir, &index);
  8050. if (ret)
  8051. goto out_fail;
  8052. BTRFS_I(old_inode)->dir_index = 0ULL;
  8053. if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  8054. /* force full log commit if subvolume involved. */
  8055. btrfs_set_log_full_commit(root->fs_info, trans);
  8056. } else {
  8057. ret = btrfs_insert_inode_ref(trans, dest,
  8058. new_dentry->d_name.name,
  8059. new_dentry->d_name.len,
  8060. old_ino,
  8061. btrfs_ino(new_dir), index);
  8062. if (ret)
  8063. goto out_fail;
  8064. /*
  8065. * this is an ugly little race, but the rename is required
  8066. * to make sure that if we crash, the inode is either at the
  8067. * old name or the new one. pinning the log transaction lets
  8068. * us make sure we don't allow a log commit to come in after
  8069. * we unlink the name but before we add the new name back in.
  8070. */
  8071. btrfs_pin_log_trans(root);
  8072. }
  8073. inode_inc_iversion(old_dir);
  8074. inode_inc_iversion(new_dir);
  8075. inode_inc_iversion(old_inode);
  8076. old_dir->i_ctime = old_dir->i_mtime = ctime;
  8077. new_dir->i_ctime = new_dir->i_mtime = ctime;
  8078. old_inode->i_ctime = ctime;
  8079. if (old_dentry->d_parent != new_dentry->d_parent)
  8080. btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
  8081. if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  8082. root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
  8083. ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
  8084. old_dentry->d_name.name,
  8085. old_dentry->d_name.len);
  8086. } else {
  8087. ret = __btrfs_unlink_inode(trans, root, old_dir,
  8088. d_inode(old_dentry),
  8089. old_dentry->d_name.name,
  8090. old_dentry->d_name.len);
  8091. if (!ret)
  8092. ret = btrfs_update_inode(trans, root, old_inode);
  8093. }
  8094. if (ret) {
  8095. btrfs_abort_transaction(trans, root, ret);
  8096. goto out_fail;
  8097. }
  8098. if (new_inode) {
  8099. inode_inc_iversion(new_inode);
  8100. new_inode->i_ctime = CURRENT_TIME;
  8101. if (unlikely(btrfs_ino(new_inode) ==
  8102. BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
  8103. root_objectid = BTRFS_I(new_inode)->location.objectid;
  8104. ret = btrfs_unlink_subvol(trans, dest, new_dir,
  8105. root_objectid,
  8106. new_dentry->d_name.name,
  8107. new_dentry->d_name.len);
  8108. BUG_ON(new_inode->i_nlink == 0);
  8109. } else {
  8110. ret = btrfs_unlink_inode(trans, dest, new_dir,
  8111. d_inode(new_dentry),
  8112. new_dentry->d_name.name,
  8113. new_dentry->d_name.len);
  8114. }
  8115. if (!ret && new_inode->i_nlink == 0)
  8116. ret = btrfs_orphan_add(trans, d_inode(new_dentry));
  8117. if (ret) {
  8118. btrfs_abort_transaction(trans, root, ret);
  8119. goto out_fail;
  8120. }
  8121. }
  8122. ret = btrfs_add_link(trans, new_dir, old_inode,
  8123. new_dentry->d_name.name,
  8124. new_dentry->d_name.len, 0, index);
  8125. if (ret) {
  8126. btrfs_abort_transaction(trans, root, ret);
  8127. goto out_fail;
  8128. }
  8129. if (old_inode->i_nlink == 1)
  8130. BTRFS_I(old_inode)->dir_index = index;
  8131. if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
  8132. struct dentry *parent = new_dentry->d_parent;
  8133. btrfs_log_new_name(trans, old_inode, old_dir, parent);
  8134. btrfs_end_log_trans(root);
  8135. }
  8136. out_fail:
  8137. btrfs_end_transaction(trans, root);
  8138. out_notrans:
  8139. if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
  8140. up_read(&root->fs_info->subvol_sem);
  8141. return ret;
  8142. }
  8143. static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
  8144. struct inode *new_dir, struct dentry *new_dentry,
  8145. unsigned int flags)
  8146. {
  8147. if (flags & ~RENAME_NOREPLACE)
  8148. return -EINVAL;
  8149. return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry);
  8150. }
  8151. static void btrfs_run_delalloc_work(struct btrfs_work *work)
  8152. {
  8153. struct btrfs_delalloc_work *delalloc_work;
  8154. struct inode *inode;
  8155. delalloc_work = container_of(work, struct btrfs_delalloc_work,
  8156. work);
  8157. inode = delalloc_work->inode;
  8158. if (delalloc_work->wait) {
  8159. btrfs_wait_ordered_range(inode, 0, (u64)-1);
  8160. } else {
  8161. filemap_flush(inode->i_mapping);
  8162. if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
  8163. &BTRFS_I(inode)->runtime_flags))
  8164. filemap_flush(inode->i_mapping);
  8165. }
  8166. if (delalloc_work->delay_iput)
  8167. btrfs_add_delayed_iput(inode);
  8168. else
  8169. iput(inode);
  8170. complete(&delalloc_work->completion);
  8171. }
  8172. struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
  8173. int wait, int delay_iput)
  8174. {
  8175. struct btrfs_delalloc_work *work;
  8176. work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
  8177. if (!work)
  8178. return NULL;
  8179. init_completion(&work->completion);
  8180. INIT_LIST_HEAD(&work->list);
  8181. work->inode = inode;
  8182. work->wait = wait;
  8183. work->delay_iput = delay_iput;
  8184. WARN_ON_ONCE(!inode);
  8185. btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
  8186. btrfs_run_delalloc_work, NULL, NULL);
  8187. return work;
  8188. }
  8189. void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
  8190. {
  8191. wait_for_completion(&work->completion);
  8192. kmem_cache_free(btrfs_delalloc_work_cachep, work);
  8193. }
  8194. /*
  8195. * some fairly slow code that needs optimization. This walks the list
  8196. * of all the inodes with pending delalloc and forces them to disk.
  8197. */
  8198. static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
  8199. int nr)
  8200. {
  8201. struct btrfs_inode *binode;
  8202. struct inode *inode;
  8203. struct btrfs_delalloc_work *work, *next;
  8204. struct list_head works;
  8205. struct list_head splice;
  8206. int ret = 0;
  8207. INIT_LIST_HEAD(&works);
  8208. INIT_LIST_HEAD(&splice);
  8209. mutex_lock(&root->delalloc_mutex);
  8210. spin_lock(&root->delalloc_lock);
  8211. list_splice_init(&root->delalloc_inodes, &splice);
  8212. while (!list_empty(&splice)) {
  8213. binode = list_entry(splice.next, struct btrfs_inode,
  8214. delalloc_inodes);
  8215. list_move_tail(&binode->delalloc_inodes,
  8216. &root->delalloc_inodes);
  8217. inode = igrab(&binode->vfs_inode);
  8218. if (!inode) {
  8219. cond_resched_lock(&root->delalloc_lock);
  8220. continue;
  8221. }
  8222. spin_unlock(&root->delalloc_lock);
  8223. work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
  8224. if (!work) {
  8225. if (delay_iput)
  8226. btrfs_add_delayed_iput(inode);
  8227. else
  8228. iput(inode);
  8229. ret = -ENOMEM;
  8230. goto out;
  8231. }
  8232. list_add_tail(&work->list, &works);
  8233. btrfs_queue_work(root->fs_info->flush_workers,
  8234. &work->work);
  8235. ret++;
  8236. if (nr != -1 && ret >= nr)
  8237. goto out;
  8238. cond_resched();
  8239. spin_lock(&root->delalloc_lock);
  8240. }
  8241. spin_unlock(&root->delalloc_lock);
  8242. out:
  8243. list_for_each_entry_safe(work, next, &works, list) {
  8244. list_del_init(&work->list);
  8245. btrfs_wait_and_free_delalloc_work(work);
  8246. }
  8247. if (!list_empty_careful(&splice)) {
  8248. spin_lock(&root->delalloc_lock);
  8249. list_splice_tail(&splice, &root->delalloc_inodes);
  8250. spin_unlock(&root->delalloc_lock);
  8251. }
  8252. mutex_unlock(&root->delalloc_mutex);
  8253. return ret;
  8254. }
  8255. int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
  8256. {
  8257. int ret;
  8258. if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
  8259. return -EROFS;
  8260. ret = __start_delalloc_inodes(root, delay_iput, -1);
  8261. if (ret > 0)
  8262. ret = 0;
  8263. /*
  8264. * the filemap_flush will queue IO into the worker threads, but
  8265. * we have to make sure the IO is actually started and that
  8266. * ordered extents get created before we return
  8267. */
  8268. atomic_inc(&root->fs_info->async_submit_draining);
  8269. while (atomic_read(&root->fs_info->nr_async_submits) ||
  8270. atomic_read(&root->fs_info->async_delalloc_pages)) {
  8271. wait_event(root->fs_info->async_submit_wait,
  8272. (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
  8273. atomic_read(&root->fs_info->async_delalloc_pages) == 0));
  8274. }
  8275. atomic_dec(&root->fs_info->async_submit_draining);
  8276. return ret;
  8277. }
  8278. int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
  8279. int nr)
  8280. {
  8281. struct btrfs_root *root;
  8282. struct list_head splice;
  8283. int ret;
  8284. if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
  8285. return -EROFS;
  8286. INIT_LIST_HEAD(&splice);
  8287. mutex_lock(&fs_info->delalloc_root_mutex);
  8288. spin_lock(&fs_info->delalloc_root_lock);
  8289. list_splice_init(&fs_info->delalloc_roots, &splice);
  8290. while (!list_empty(&splice) && nr) {
  8291. root = list_first_entry(&splice, struct btrfs_root,
  8292. delalloc_root);
  8293. root = btrfs_grab_fs_root(root);
  8294. BUG_ON(!root);
  8295. list_move_tail(&root->delalloc_root,
  8296. &fs_info->delalloc_roots);
  8297. spin_unlock(&fs_info->delalloc_root_lock);
  8298. ret = __start_delalloc_inodes(root, delay_iput, nr);
  8299. btrfs_put_fs_root(root);
  8300. if (ret < 0)
  8301. goto out;
  8302. if (nr != -1) {
  8303. nr -= ret;
  8304. WARN_ON(nr < 0);
  8305. }
  8306. spin_lock(&fs_info->delalloc_root_lock);
  8307. }
  8308. spin_unlock(&fs_info->delalloc_root_lock);
  8309. ret = 0;
  8310. atomic_inc(&fs_info->async_submit_draining);
  8311. while (atomic_read(&fs_info->nr_async_submits) ||
  8312. atomic_read(&fs_info->async_delalloc_pages)) {
  8313. wait_event(fs_info->async_submit_wait,
  8314. (atomic_read(&fs_info->nr_async_submits) == 0 &&
  8315. atomic_read(&fs_info->async_delalloc_pages) == 0));
  8316. }
  8317. atomic_dec(&fs_info->async_submit_draining);
  8318. out:
  8319. if (!list_empty_careful(&splice)) {
  8320. spin_lock(&fs_info->delalloc_root_lock);
  8321. list_splice_tail(&splice, &fs_info->delalloc_roots);
  8322. spin_unlock(&fs_info->delalloc_root_lock);
  8323. }
  8324. mutex_unlock(&fs_info->delalloc_root_mutex);
  8325. return ret;
  8326. }
  8327. static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
  8328. const char *symname)
  8329. {
  8330. struct btrfs_trans_handle *trans;
  8331. struct btrfs_root *root = BTRFS_I(dir)->root;
  8332. struct btrfs_path *path;
  8333. struct btrfs_key key;
  8334. struct inode *inode = NULL;
  8335. int err;
  8336. int drop_inode = 0;
  8337. u64 objectid;
  8338. u64 index = 0;
  8339. int name_len;
  8340. int datasize;
  8341. unsigned long ptr;
  8342. struct btrfs_file_extent_item *ei;
  8343. struct extent_buffer *leaf;
  8344. name_len = strlen(symname);
  8345. if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
  8346. return -ENAMETOOLONG;
  8347. /*
  8348. * 2 items for inode item and ref
  8349. * 2 items for dir items
  8350. * 1 item for xattr if selinux is on
  8351. */
  8352. trans = btrfs_start_transaction(root, 5);
  8353. if (IS_ERR(trans))
  8354. return PTR_ERR(trans);
  8355. err = btrfs_find_free_ino(root, &objectid);
  8356. if (err)
  8357. goto out_unlock;
  8358. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  8359. dentry->d_name.len, btrfs_ino(dir), objectid,
  8360. S_IFLNK|S_IRWXUGO, &index);
  8361. if (IS_ERR(inode)) {
  8362. err = PTR_ERR(inode);
  8363. goto out_unlock;
  8364. }
  8365. /*
  8366. * If the active LSM wants to access the inode during
  8367. * d_instantiate it needs these. Smack checks to see
  8368. * if the filesystem supports xattrs by looking at the
  8369. * ops vector.
  8370. */
  8371. inode->i_fop = &btrfs_file_operations;
  8372. inode->i_op = &btrfs_file_inode_operations;
  8373. inode->i_mapping->a_ops = &btrfs_aops;
  8374. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  8375. err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
  8376. if (err)
  8377. goto out_unlock_inode;
  8378. err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
  8379. if (err)
  8380. goto out_unlock_inode;
  8381. path = btrfs_alloc_path();
  8382. if (!path) {
  8383. err = -ENOMEM;
  8384. goto out_unlock_inode;
  8385. }
  8386. key.objectid = btrfs_ino(inode);
  8387. key.offset = 0;
  8388. key.type = BTRFS_EXTENT_DATA_KEY;
  8389. datasize = btrfs_file_extent_calc_inline_size(name_len);
  8390. err = btrfs_insert_empty_item(trans, root, path, &key,
  8391. datasize);
  8392. if (err) {
  8393. btrfs_free_path(path);
  8394. goto out_unlock_inode;
  8395. }
  8396. leaf = path->nodes[0];
  8397. ei = btrfs_item_ptr(leaf, path->slots[0],
  8398. struct btrfs_file_extent_item);
  8399. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  8400. btrfs_set_file_extent_type(leaf, ei,
  8401. BTRFS_FILE_EXTENT_INLINE);
  8402. btrfs_set_file_extent_encryption(leaf, ei, 0);
  8403. btrfs_set_file_extent_compression(leaf, ei, 0);
  8404. btrfs_set_file_extent_other_encoding(leaf, ei, 0);
  8405. btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
  8406. ptr = btrfs_file_extent_inline_start(ei);
  8407. write_extent_buffer(leaf, symname, ptr, name_len);
  8408. btrfs_mark_buffer_dirty(leaf);
  8409. btrfs_free_path(path);
  8410. inode->i_op = &btrfs_symlink_inode_operations;
  8411. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  8412. inode_set_bytes(inode, name_len);
  8413. btrfs_i_size_write(inode, name_len);
  8414. err = btrfs_update_inode(trans, root, inode);
  8415. if (err) {
  8416. drop_inode = 1;
  8417. goto out_unlock_inode;
  8418. }
  8419. unlock_new_inode(inode);
  8420. d_instantiate(dentry, inode);
  8421. out_unlock:
  8422. btrfs_end_transaction(trans, root);
  8423. if (drop_inode) {
  8424. inode_dec_link_count(inode);
  8425. iput(inode);
  8426. }
  8427. btrfs_btree_balance_dirty(root);
  8428. return err;
  8429. out_unlock_inode:
  8430. drop_inode = 1;
  8431. unlock_new_inode(inode);
  8432. goto out_unlock;
  8433. }
  8434. static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
  8435. u64 start, u64 num_bytes, u64 min_size,
  8436. loff_t actual_len, u64 *alloc_hint,
  8437. struct btrfs_trans_handle *trans)
  8438. {
  8439. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  8440. struct extent_map *em;
  8441. struct btrfs_root *root = BTRFS_I(inode)->root;
  8442. struct btrfs_key ins;
  8443. u64 cur_offset = start;
  8444. u64 i_size;
  8445. u64 cur_bytes;
  8446. int ret = 0;
  8447. bool own_trans = true;
  8448. if (trans)
  8449. own_trans = false;
  8450. while (num_bytes > 0) {
  8451. if (own_trans) {
  8452. trans = btrfs_start_transaction(root, 3);
  8453. if (IS_ERR(trans)) {
  8454. ret = PTR_ERR(trans);
  8455. break;
  8456. }
  8457. }
  8458. cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
  8459. cur_bytes = max(cur_bytes, min_size);
  8460. ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
  8461. *alloc_hint, &ins, 1, 0);
  8462. if (ret) {
  8463. if (own_trans)
  8464. btrfs_end_transaction(trans, root);
  8465. break;
  8466. }
  8467. ret = insert_reserved_file_extent(trans, inode,
  8468. cur_offset, ins.objectid,
  8469. ins.offset, ins.offset,
  8470. ins.offset, 0, 0, 0,
  8471. BTRFS_FILE_EXTENT_PREALLOC);
  8472. if (ret) {
  8473. btrfs_free_reserved_extent(root, ins.objectid,
  8474. ins.offset, 0);
  8475. btrfs_abort_transaction(trans, root, ret);
  8476. if (own_trans)
  8477. btrfs_end_transaction(trans, root);
  8478. break;
  8479. }
  8480. btrfs_drop_extent_cache(inode, cur_offset,
  8481. cur_offset + ins.offset -1, 0);
  8482. em = alloc_extent_map();
  8483. if (!em) {
  8484. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  8485. &BTRFS_I(inode)->runtime_flags);
  8486. goto next;
  8487. }
  8488. em->start = cur_offset;
  8489. em->orig_start = cur_offset;
  8490. em->len = ins.offset;
  8491. em->block_start = ins.objectid;
  8492. em->block_len = ins.offset;
  8493. em->orig_block_len = ins.offset;
  8494. em->ram_bytes = ins.offset;
  8495. em->bdev = root->fs_info->fs_devices->latest_bdev;
  8496. set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
  8497. em->generation = trans->transid;
  8498. while (1) {
  8499. write_lock(&em_tree->lock);
  8500. ret = add_extent_mapping(em_tree, em, 1);
  8501. write_unlock(&em_tree->lock);
  8502. if (ret != -EEXIST)
  8503. break;
  8504. btrfs_drop_extent_cache(inode, cur_offset,
  8505. cur_offset + ins.offset - 1,
  8506. 0);
  8507. }
  8508. free_extent_map(em);
  8509. next:
  8510. num_bytes -= ins.offset;
  8511. cur_offset += ins.offset;
  8512. *alloc_hint = ins.objectid + ins.offset;
  8513. inode_inc_iversion(inode);
  8514. inode->i_ctime = CURRENT_TIME;
  8515. BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
  8516. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  8517. (actual_len > inode->i_size) &&
  8518. (cur_offset > inode->i_size)) {
  8519. if (cur_offset > actual_len)
  8520. i_size = actual_len;
  8521. else
  8522. i_size = cur_offset;
  8523. i_size_write(inode, i_size);
  8524. btrfs_ordered_update_i_size(inode, i_size, NULL);
  8525. }
  8526. ret = btrfs_update_inode(trans, root, inode);
  8527. if (ret) {
  8528. btrfs_abort_transaction(trans, root, ret);
  8529. if (own_trans)
  8530. btrfs_end_transaction(trans, root);
  8531. break;
  8532. }
  8533. if (own_trans)
  8534. btrfs_end_transaction(trans, root);
  8535. }
  8536. return ret;
  8537. }
  8538. int btrfs_prealloc_file_range(struct inode *inode, int mode,
  8539. u64 start, u64 num_bytes, u64 min_size,
  8540. loff_t actual_len, u64 *alloc_hint)
  8541. {
  8542. return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
  8543. min_size, actual_len, alloc_hint,
  8544. NULL);
  8545. }
  8546. int btrfs_prealloc_file_range_trans(struct inode *inode,
  8547. struct btrfs_trans_handle *trans, int mode,
  8548. u64 start, u64 num_bytes, u64 min_size,
  8549. loff_t actual_len, u64 *alloc_hint)
  8550. {
  8551. return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
  8552. min_size, actual_len, alloc_hint, trans);
  8553. }
  8554. static int btrfs_set_page_dirty(struct page *page)
  8555. {
  8556. return __set_page_dirty_nobuffers(page);
  8557. }
  8558. static int btrfs_permission(struct inode *inode, int mask)
  8559. {
  8560. struct btrfs_root *root = BTRFS_I(inode)->root;
  8561. umode_t mode = inode->i_mode;
  8562. if (mask & MAY_WRITE &&
  8563. (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
  8564. if (btrfs_root_readonly(root))
  8565. return -EROFS;
  8566. if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
  8567. return -EACCES;
  8568. }
  8569. return generic_permission(inode, mask);
  8570. }
  8571. static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
  8572. {
  8573. struct btrfs_trans_handle *trans;
  8574. struct btrfs_root *root = BTRFS_I(dir)->root;
  8575. struct inode *inode = NULL;
  8576. u64 objectid;
  8577. u64 index;
  8578. int ret = 0;
  8579. /*
  8580. * 5 units required for adding orphan entry
  8581. */
  8582. trans = btrfs_start_transaction(root, 5);
  8583. if (IS_ERR(trans))
  8584. return PTR_ERR(trans);
  8585. ret = btrfs_find_free_ino(root, &objectid);
  8586. if (ret)
  8587. goto out;
  8588. inode = btrfs_new_inode(trans, root, dir, NULL, 0,
  8589. btrfs_ino(dir), objectid, mode, &index);
  8590. if (IS_ERR(inode)) {
  8591. ret = PTR_ERR(inode);
  8592. inode = NULL;
  8593. goto out;
  8594. }
  8595. inode->i_fop = &btrfs_file_operations;
  8596. inode->i_op = &btrfs_file_inode_operations;
  8597. inode->i_mapping->a_ops = &btrfs_aops;
  8598. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  8599. ret = btrfs_init_inode_security(trans, inode, dir, NULL);
  8600. if (ret)
  8601. goto out_inode;
  8602. ret = btrfs_update_inode(trans, root, inode);
  8603. if (ret)
  8604. goto out_inode;
  8605. ret = btrfs_orphan_add(trans, inode);
  8606. if (ret)
  8607. goto out_inode;
  8608. /*
  8609. * We set number of links to 0 in btrfs_new_inode(), and here we set
  8610. * it to 1 because d_tmpfile() will issue a warning if the count is 0,
  8611. * through:
  8612. *
  8613. * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
  8614. */
  8615. set_nlink(inode, 1);
  8616. unlock_new_inode(inode);
  8617. d_tmpfile(dentry, inode);
  8618. mark_inode_dirty(inode);
  8619. out:
  8620. btrfs_end_transaction(trans, root);
  8621. if (ret)
  8622. iput(inode);
  8623. btrfs_balance_delayed_items(root);
  8624. btrfs_btree_balance_dirty(root);
  8625. return ret;
  8626. out_inode:
  8627. unlock_new_inode(inode);
  8628. goto out;
  8629. }
  8630. /* Inspired by filemap_check_errors() */
  8631. int btrfs_inode_check_errors(struct inode *inode)
  8632. {
  8633. int ret = 0;
  8634. if (test_bit(AS_ENOSPC, &inode->i_mapping->flags) &&
  8635. test_and_clear_bit(AS_ENOSPC, &inode->i_mapping->flags))
  8636. ret = -ENOSPC;
  8637. if (test_bit(AS_EIO, &inode->i_mapping->flags) &&
  8638. test_and_clear_bit(AS_EIO, &inode->i_mapping->flags))
  8639. ret = -EIO;
  8640. return ret;
  8641. }
  8642. static const struct inode_operations btrfs_dir_inode_operations = {
  8643. .getattr = btrfs_getattr,
  8644. .lookup = btrfs_lookup,
  8645. .create = btrfs_create,
  8646. .unlink = btrfs_unlink,
  8647. .link = btrfs_link,
  8648. .mkdir = btrfs_mkdir,
  8649. .rmdir = btrfs_rmdir,
  8650. .rename2 = btrfs_rename2,
  8651. .symlink = btrfs_symlink,
  8652. .setattr = btrfs_setattr,
  8653. .mknod = btrfs_mknod,
  8654. .setxattr = btrfs_setxattr,
  8655. .getxattr = btrfs_getxattr,
  8656. .listxattr = btrfs_listxattr,
  8657. .removexattr = btrfs_removexattr,
  8658. .permission = btrfs_permission,
  8659. .get_acl = btrfs_get_acl,
  8660. .set_acl = btrfs_set_acl,
  8661. .update_time = btrfs_update_time,
  8662. .tmpfile = btrfs_tmpfile,
  8663. };
  8664. static const struct inode_operations btrfs_dir_ro_inode_operations = {
  8665. .lookup = btrfs_lookup,
  8666. .permission = btrfs_permission,
  8667. .get_acl = btrfs_get_acl,
  8668. .set_acl = btrfs_set_acl,
  8669. .update_time = btrfs_update_time,
  8670. };
  8671. static const struct file_operations btrfs_dir_file_operations = {
  8672. .llseek = generic_file_llseek,
  8673. .read = generic_read_dir,
  8674. .iterate = btrfs_real_readdir,
  8675. .unlocked_ioctl = btrfs_ioctl,
  8676. #ifdef CONFIG_COMPAT
  8677. .compat_ioctl = btrfs_ioctl,
  8678. #endif
  8679. .release = btrfs_release_file,
  8680. .fsync = btrfs_sync_file,
  8681. };
  8682. static struct extent_io_ops btrfs_extent_io_ops = {
  8683. .fill_delalloc = run_delalloc_range,
  8684. .submit_bio_hook = btrfs_submit_bio_hook,
  8685. .merge_bio_hook = btrfs_merge_bio_hook,
  8686. .readpage_end_io_hook = btrfs_readpage_end_io_hook,
  8687. .writepage_end_io_hook = btrfs_writepage_end_io_hook,
  8688. .writepage_start_hook = btrfs_writepage_start_hook,
  8689. .set_bit_hook = btrfs_set_bit_hook,
  8690. .clear_bit_hook = btrfs_clear_bit_hook,
  8691. .merge_extent_hook = btrfs_merge_extent_hook,
  8692. .split_extent_hook = btrfs_split_extent_hook,
  8693. };
  8694. /*
  8695. * btrfs doesn't support the bmap operation because swapfiles
  8696. * use bmap to make a mapping of extents in the file. They assume
  8697. * these extents won't change over the life of the file and they
  8698. * use the bmap result to do IO directly to the drive.
  8699. *
  8700. * the btrfs bmap call would return logical addresses that aren't
  8701. * suitable for IO and they also will change frequently as COW
  8702. * operations happen. So, swapfile + btrfs == corruption.
  8703. *
  8704. * For now we're avoiding this by dropping bmap.
  8705. */
  8706. static const struct address_space_operations btrfs_aops = {
  8707. .readpage = btrfs_readpage,
  8708. .writepage = btrfs_writepage,
  8709. .writepages = btrfs_writepages,
  8710. .readpages = btrfs_readpages,
  8711. .direct_IO = btrfs_direct_IO,
  8712. .invalidatepage = btrfs_invalidatepage,
  8713. .releasepage = btrfs_releasepage,
  8714. .set_page_dirty = btrfs_set_page_dirty,
  8715. .error_remove_page = generic_error_remove_page,
  8716. };
  8717. static const struct address_space_operations btrfs_symlink_aops = {
  8718. .readpage = btrfs_readpage,
  8719. .writepage = btrfs_writepage,
  8720. .invalidatepage = btrfs_invalidatepage,
  8721. .releasepage = btrfs_releasepage,
  8722. };
  8723. static const struct inode_operations btrfs_file_inode_operations = {
  8724. .getattr = btrfs_getattr,
  8725. .setattr = btrfs_setattr,
  8726. .setxattr = btrfs_setxattr,
  8727. .getxattr = btrfs_getxattr,
  8728. .listxattr = btrfs_listxattr,
  8729. .removexattr = btrfs_removexattr,
  8730. .permission = btrfs_permission,
  8731. .fiemap = btrfs_fiemap,
  8732. .get_acl = btrfs_get_acl,
  8733. .set_acl = btrfs_set_acl,
  8734. .update_time = btrfs_update_time,
  8735. };
  8736. static const struct inode_operations btrfs_special_inode_operations = {
  8737. .getattr = btrfs_getattr,
  8738. .setattr = btrfs_setattr,
  8739. .permission = btrfs_permission,
  8740. .setxattr = btrfs_setxattr,
  8741. .getxattr = btrfs_getxattr,
  8742. .listxattr = btrfs_listxattr,
  8743. .removexattr = btrfs_removexattr,
  8744. .get_acl = btrfs_get_acl,
  8745. .set_acl = btrfs_set_acl,
  8746. .update_time = btrfs_update_time,
  8747. };
  8748. static const struct inode_operations btrfs_symlink_inode_operations = {
  8749. .readlink = generic_readlink,
  8750. .follow_link = page_follow_link_light,
  8751. .put_link = page_put_link,
  8752. .getattr = btrfs_getattr,
  8753. .setattr = btrfs_setattr,
  8754. .permission = btrfs_permission,
  8755. .setxattr = btrfs_setxattr,
  8756. .getxattr = btrfs_getxattr,
  8757. .listxattr = btrfs_listxattr,
  8758. .removexattr = btrfs_removexattr,
  8759. .update_time = btrfs_update_time,
  8760. };
  8761. const struct dentry_operations btrfs_dentry_operations = {
  8762. .d_delete = btrfs_dentry_delete,
  8763. .d_release = btrfs_dentry_release,
  8764. };