inode.c 267 KB

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