inode.c 289 KB

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