inode.c 260 KB

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