inode.c 267 KB

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