inode.c 285 KB

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