inode.c 252 KB

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