inode.c 282 KB

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