inode.c 283 KB

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