inode.c 288 KB

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