inode.c 282 KB

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