inode.c 251 KB

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