inode.c 272 KB

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