inode.c 283 KB

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