inode.c 283 KB

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