inode.c 282 KB

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