inode.c 288 KB

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