inode.c 284 KB

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