inode.c 252 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639
  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 long *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 long *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_info(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_crit(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. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  3167. inode->i_fop = &btrfs_file_operations;
  3168. inode->i_op = &btrfs_file_inode_operations;
  3169. break;
  3170. case S_IFDIR:
  3171. inode->i_fop = &btrfs_dir_file_operations;
  3172. if (root == root->fs_info->tree_root)
  3173. inode->i_op = &btrfs_dir_ro_inode_operations;
  3174. else
  3175. inode->i_op = &btrfs_dir_inode_operations;
  3176. break;
  3177. case S_IFLNK:
  3178. inode->i_op = &btrfs_symlink_inode_operations;
  3179. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  3180. break;
  3181. default:
  3182. inode->i_op = &btrfs_special_inode_operations;
  3183. init_special_inode(inode, inode->i_mode, rdev);
  3184. break;
  3185. }
  3186. btrfs_update_iflags(inode);
  3187. return;
  3188. make_bad:
  3189. btrfs_free_path(path);
  3190. make_bad_inode(inode);
  3191. }
  3192. /*
  3193. * given a leaf and an inode, copy the inode fields into the leaf
  3194. */
  3195. static void fill_inode_item(struct btrfs_trans_handle *trans,
  3196. struct extent_buffer *leaf,
  3197. struct btrfs_inode_item *item,
  3198. struct inode *inode)
  3199. {
  3200. struct btrfs_map_token token;
  3201. btrfs_init_map_token(&token);
  3202. btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
  3203. btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
  3204. btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
  3205. &token);
  3206. btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
  3207. btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
  3208. btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
  3209. inode->i_atime.tv_sec, &token);
  3210. btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
  3211. inode->i_atime.tv_nsec, &token);
  3212. btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
  3213. inode->i_mtime.tv_sec, &token);
  3214. btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
  3215. inode->i_mtime.tv_nsec, &token);
  3216. btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
  3217. inode->i_ctime.tv_sec, &token);
  3218. btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
  3219. inode->i_ctime.tv_nsec, &token);
  3220. btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
  3221. &token);
  3222. btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
  3223. &token);
  3224. btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
  3225. btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
  3226. btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
  3227. btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
  3228. btrfs_set_token_inode_block_group(leaf, item, 0, &token);
  3229. }
  3230. /*
  3231. * copy everything in the in-memory inode into the btree.
  3232. */
  3233. static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
  3234. struct btrfs_root *root, struct inode *inode)
  3235. {
  3236. struct btrfs_inode_item *inode_item;
  3237. struct btrfs_path *path;
  3238. struct extent_buffer *leaf;
  3239. int ret;
  3240. path = btrfs_alloc_path();
  3241. if (!path)
  3242. return -ENOMEM;
  3243. path->leave_spinning = 1;
  3244. ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
  3245. 1);
  3246. if (ret) {
  3247. if (ret > 0)
  3248. ret = -ENOENT;
  3249. goto failed;
  3250. }
  3251. leaf = path->nodes[0];
  3252. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  3253. struct btrfs_inode_item);
  3254. fill_inode_item(trans, leaf, inode_item, inode);
  3255. btrfs_mark_buffer_dirty(leaf);
  3256. btrfs_set_inode_last_trans(trans, inode);
  3257. ret = 0;
  3258. failed:
  3259. btrfs_free_path(path);
  3260. return ret;
  3261. }
  3262. /*
  3263. * copy everything in the in-memory inode into the btree.
  3264. */
  3265. noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
  3266. struct btrfs_root *root, struct inode *inode)
  3267. {
  3268. int ret;
  3269. /*
  3270. * If the inode is a free space inode, we can deadlock during commit
  3271. * if we put it into the delayed code.
  3272. *
  3273. * The data relocation inode should also be directly updated
  3274. * without delay
  3275. */
  3276. if (!btrfs_is_free_space_inode(inode)
  3277. && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
  3278. && !root->fs_info->log_root_recovering) {
  3279. btrfs_update_root_times(trans, root);
  3280. ret = btrfs_delayed_update_inode(trans, root, inode);
  3281. if (!ret)
  3282. btrfs_set_inode_last_trans(trans, inode);
  3283. return ret;
  3284. }
  3285. return btrfs_update_inode_item(trans, root, inode);
  3286. }
  3287. noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
  3288. struct btrfs_root *root,
  3289. struct inode *inode)
  3290. {
  3291. int ret;
  3292. ret = btrfs_update_inode(trans, root, inode);
  3293. if (ret == -ENOSPC)
  3294. return btrfs_update_inode_item(trans, root, inode);
  3295. return ret;
  3296. }
  3297. /*
  3298. * unlink helper that gets used here in inode.c and in the tree logging
  3299. * recovery code. It remove a link in a directory with a given name, and
  3300. * also drops the back refs in the inode to the directory
  3301. */
  3302. static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
  3303. struct btrfs_root *root,
  3304. struct inode *dir, struct inode *inode,
  3305. const char *name, int name_len)
  3306. {
  3307. struct btrfs_path *path;
  3308. int ret = 0;
  3309. struct extent_buffer *leaf;
  3310. struct btrfs_dir_item *di;
  3311. struct btrfs_key key;
  3312. u64 index;
  3313. u64 ino = btrfs_ino(inode);
  3314. u64 dir_ino = btrfs_ino(dir);
  3315. path = btrfs_alloc_path();
  3316. if (!path) {
  3317. ret = -ENOMEM;
  3318. goto out;
  3319. }
  3320. path->leave_spinning = 1;
  3321. di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
  3322. name, name_len, -1);
  3323. if (IS_ERR(di)) {
  3324. ret = PTR_ERR(di);
  3325. goto err;
  3326. }
  3327. if (!di) {
  3328. ret = -ENOENT;
  3329. goto err;
  3330. }
  3331. leaf = path->nodes[0];
  3332. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  3333. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  3334. if (ret)
  3335. goto err;
  3336. btrfs_release_path(path);
  3337. /*
  3338. * If we don't have dir index, we have to get it by looking up
  3339. * the inode ref, since we get the inode ref, remove it directly,
  3340. * it is unnecessary to do delayed deletion.
  3341. *
  3342. * But if we have dir index, needn't search inode ref to get it.
  3343. * Since the inode ref is close to the inode item, it is better
  3344. * that we delay to delete it, and just do this deletion when
  3345. * we update the inode item.
  3346. */
  3347. if (BTRFS_I(inode)->dir_index) {
  3348. ret = btrfs_delayed_delete_inode_ref(inode);
  3349. if (!ret) {
  3350. index = BTRFS_I(inode)->dir_index;
  3351. goto skip_backref;
  3352. }
  3353. }
  3354. ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
  3355. dir_ino, &index);
  3356. if (ret) {
  3357. btrfs_info(root->fs_info,
  3358. "failed to delete reference to %.*s, inode %llu parent %llu",
  3359. name_len, name, ino, dir_ino);
  3360. btrfs_abort_transaction(trans, root, ret);
  3361. goto err;
  3362. }
  3363. skip_backref:
  3364. ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
  3365. if (ret) {
  3366. btrfs_abort_transaction(trans, root, ret);
  3367. goto err;
  3368. }
  3369. ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
  3370. inode, dir_ino);
  3371. if (ret != 0 && ret != -ENOENT) {
  3372. btrfs_abort_transaction(trans, root, ret);
  3373. goto err;
  3374. }
  3375. ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
  3376. dir, index);
  3377. if (ret == -ENOENT)
  3378. ret = 0;
  3379. else if (ret)
  3380. btrfs_abort_transaction(trans, root, ret);
  3381. err:
  3382. btrfs_free_path(path);
  3383. if (ret)
  3384. goto out;
  3385. btrfs_i_size_write(dir, dir->i_size - name_len * 2);
  3386. inode_inc_iversion(inode);
  3387. inode_inc_iversion(dir);
  3388. inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  3389. ret = btrfs_update_inode(trans, root, dir);
  3390. out:
  3391. return ret;
  3392. }
  3393. int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
  3394. struct btrfs_root *root,
  3395. struct inode *dir, struct inode *inode,
  3396. const char *name, int name_len)
  3397. {
  3398. int ret;
  3399. ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
  3400. if (!ret) {
  3401. drop_nlink(inode);
  3402. ret = btrfs_update_inode(trans, root, inode);
  3403. }
  3404. return ret;
  3405. }
  3406. /*
  3407. * helper to start transaction for unlink and rmdir.
  3408. *
  3409. * unlink and rmdir are special in btrfs, they do not always free space, so
  3410. * if we cannot make our reservations the normal way try and see if there is
  3411. * plenty of slack room in the global reserve to migrate, otherwise we cannot
  3412. * allow the unlink to occur.
  3413. */
  3414. static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
  3415. {
  3416. struct btrfs_trans_handle *trans;
  3417. struct btrfs_root *root = BTRFS_I(dir)->root;
  3418. int ret;
  3419. /*
  3420. * 1 for the possible orphan item
  3421. * 1 for the dir item
  3422. * 1 for the dir index
  3423. * 1 for the inode ref
  3424. * 1 for the inode
  3425. */
  3426. trans = btrfs_start_transaction(root, 5);
  3427. if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
  3428. return trans;
  3429. if (PTR_ERR(trans) == -ENOSPC) {
  3430. u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
  3431. trans = btrfs_start_transaction(root, 0);
  3432. if (IS_ERR(trans))
  3433. return trans;
  3434. ret = btrfs_cond_migrate_bytes(root->fs_info,
  3435. &root->fs_info->trans_block_rsv,
  3436. num_bytes, 5);
  3437. if (ret) {
  3438. btrfs_end_transaction(trans, root);
  3439. return ERR_PTR(ret);
  3440. }
  3441. trans->block_rsv = &root->fs_info->trans_block_rsv;
  3442. trans->bytes_reserved = num_bytes;
  3443. }
  3444. return trans;
  3445. }
  3446. static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
  3447. {
  3448. struct btrfs_root *root = BTRFS_I(dir)->root;
  3449. struct btrfs_trans_handle *trans;
  3450. struct inode *inode = dentry->d_inode;
  3451. int ret;
  3452. trans = __unlink_start_trans(dir);
  3453. if (IS_ERR(trans))
  3454. return PTR_ERR(trans);
  3455. btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
  3456. ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
  3457. dentry->d_name.name, dentry->d_name.len);
  3458. if (ret)
  3459. goto out;
  3460. if (inode->i_nlink == 0) {
  3461. ret = btrfs_orphan_add(trans, inode);
  3462. if (ret)
  3463. goto out;
  3464. }
  3465. out:
  3466. btrfs_end_transaction(trans, root);
  3467. btrfs_btree_balance_dirty(root);
  3468. return ret;
  3469. }
  3470. int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
  3471. struct btrfs_root *root,
  3472. struct inode *dir, u64 objectid,
  3473. const char *name, int name_len)
  3474. {
  3475. struct btrfs_path *path;
  3476. struct extent_buffer *leaf;
  3477. struct btrfs_dir_item *di;
  3478. struct btrfs_key key;
  3479. u64 index;
  3480. int ret;
  3481. u64 dir_ino = btrfs_ino(dir);
  3482. path = btrfs_alloc_path();
  3483. if (!path)
  3484. return -ENOMEM;
  3485. di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
  3486. name, name_len, -1);
  3487. if (IS_ERR_OR_NULL(di)) {
  3488. if (!di)
  3489. ret = -ENOENT;
  3490. else
  3491. ret = PTR_ERR(di);
  3492. goto out;
  3493. }
  3494. leaf = path->nodes[0];
  3495. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  3496. WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
  3497. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  3498. if (ret) {
  3499. btrfs_abort_transaction(trans, root, ret);
  3500. goto out;
  3501. }
  3502. btrfs_release_path(path);
  3503. ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
  3504. objectid, root->root_key.objectid,
  3505. dir_ino, &index, name, name_len);
  3506. if (ret < 0) {
  3507. if (ret != -ENOENT) {
  3508. btrfs_abort_transaction(trans, root, ret);
  3509. goto out;
  3510. }
  3511. di = btrfs_search_dir_index_item(root, path, dir_ino,
  3512. name, name_len);
  3513. if (IS_ERR_OR_NULL(di)) {
  3514. if (!di)
  3515. ret = -ENOENT;
  3516. else
  3517. ret = PTR_ERR(di);
  3518. btrfs_abort_transaction(trans, root, ret);
  3519. goto out;
  3520. }
  3521. leaf = path->nodes[0];
  3522. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  3523. btrfs_release_path(path);
  3524. index = key.offset;
  3525. }
  3526. btrfs_release_path(path);
  3527. ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
  3528. if (ret) {
  3529. btrfs_abort_transaction(trans, root, ret);
  3530. goto out;
  3531. }
  3532. btrfs_i_size_write(dir, dir->i_size - name_len * 2);
  3533. inode_inc_iversion(dir);
  3534. dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  3535. ret = btrfs_update_inode_fallback(trans, root, dir);
  3536. if (ret)
  3537. btrfs_abort_transaction(trans, root, ret);
  3538. out:
  3539. btrfs_free_path(path);
  3540. return ret;
  3541. }
  3542. static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
  3543. {
  3544. struct inode *inode = dentry->d_inode;
  3545. int err = 0;
  3546. struct btrfs_root *root = BTRFS_I(dir)->root;
  3547. struct btrfs_trans_handle *trans;
  3548. if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
  3549. return -ENOTEMPTY;
  3550. if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
  3551. return -EPERM;
  3552. trans = __unlink_start_trans(dir);
  3553. if (IS_ERR(trans))
  3554. return PTR_ERR(trans);
  3555. if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
  3556. err = btrfs_unlink_subvol(trans, root, dir,
  3557. BTRFS_I(inode)->location.objectid,
  3558. dentry->d_name.name,
  3559. dentry->d_name.len);
  3560. goto out;
  3561. }
  3562. err = btrfs_orphan_add(trans, inode);
  3563. if (err)
  3564. goto out;
  3565. /* now the directory is empty */
  3566. err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
  3567. dentry->d_name.name, dentry->d_name.len);
  3568. if (!err)
  3569. btrfs_i_size_write(inode, 0);
  3570. out:
  3571. btrfs_end_transaction(trans, root);
  3572. btrfs_btree_balance_dirty(root);
  3573. return err;
  3574. }
  3575. /*
  3576. * this can truncate away extent items, csum items and directory items.
  3577. * It starts at a high offset and removes keys until it can't find
  3578. * any higher than new_size
  3579. *
  3580. * csum items that cross the new i_size are truncated to the new size
  3581. * as well.
  3582. *
  3583. * min_type is the minimum key type to truncate down to. If set to 0, this
  3584. * will kill all the items on this inode, including the INODE_ITEM_KEY.
  3585. */
  3586. int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
  3587. struct btrfs_root *root,
  3588. struct inode *inode,
  3589. u64 new_size, u32 min_type)
  3590. {
  3591. struct btrfs_path *path;
  3592. struct extent_buffer *leaf;
  3593. struct btrfs_file_extent_item *fi;
  3594. struct btrfs_key key;
  3595. struct btrfs_key found_key;
  3596. u64 extent_start = 0;
  3597. u64 extent_num_bytes = 0;
  3598. u64 extent_offset = 0;
  3599. u64 item_end = 0;
  3600. u64 last_size = (u64)-1;
  3601. u32 found_type = (u8)-1;
  3602. int found_extent;
  3603. int del_item;
  3604. int pending_del_nr = 0;
  3605. int pending_del_slot = 0;
  3606. int extent_type = -1;
  3607. int ret;
  3608. int err = 0;
  3609. u64 ino = btrfs_ino(inode);
  3610. BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
  3611. path = btrfs_alloc_path();
  3612. if (!path)
  3613. return -ENOMEM;
  3614. path->reada = -1;
  3615. /*
  3616. * We want to drop from the next block forward in case this new size is
  3617. * not block aligned since we will be keeping the last block of the
  3618. * extent just the way it is.
  3619. */
  3620. if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
  3621. root == root->fs_info->tree_root)
  3622. btrfs_drop_extent_cache(inode, ALIGN(new_size,
  3623. root->sectorsize), (u64)-1, 0);
  3624. /*
  3625. * This function is also used to drop the items in the log tree before
  3626. * we relog the inode, so if root != BTRFS_I(inode)->root, it means
  3627. * it is used to drop the loged items. So we shouldn't kill the delayed
  3628. * items.
  3629. */
  3630. if (min_type == 0 && root == BTRFS_I(inode)->root)
  3631. btrfs_kill_delayed_inode_items(inode);
  3632. key.objectid = ino;
  3633. key.offset = (u64)-1;
  3634. key.type = (u8)-1;
  3635. search_again:
  3636. path->leave_spinning = 1;
  3637. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  3638. if (ret < 0) {
  3639. err = ret;
  3640. goto out;
  3641. }
  3642. if (ret > 0) {
  3643. /* there are no items in the tree for us to truncate, we're
  3644. * done
  3645. */
  3646. if (path->slots[0] == 0)
  3647. goto out;
  3648. path->slots[0]--;
  3649. }
  3650. while (1) {
  3651. fi = NULL;
  3652. leaf = path->nodes[0];
  3653. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3654. found_type = found_key.type;
  3655. if (found_key.objectid != ino)
  3656. break;
  3657. if (found_type < min_type)
  3658. break;
  3659. item_end = found_key.offset;
  3660. if (found_type == BTRFS_EXTENT_DATA_KEY) {
  3661. fi = btrfs_item_ptr(leaf, path->slots[0],
  3662. struct btrfs_file_extent_item);
  3663. extent_type = btrfs_file_extent_type(leaf, fi);
  3664. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  3665. item_end +=
  3666. btrfs_file_extent_num_bytes(leaf, fi);
  3667. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  3668. item_end += btrfs_file_extent_inline_len(leaf,
  3669. path->slots[0], fi);
  3670. }
  3671. item_end--;
  3672. }
  3673. if (found_type > min_type) {
  3674. del_item = 1;
  3675. } else {
  3676. if (item_end < new_size)
  3677. break;
  3678. if (found_key.offset >= new_size)
  3679. del_item = 1;
  3680. else
  3681. del_item = 0;
  3682. }
  3683. found_extent = 0;
  3684. /* FIXME, shrink the extent if the ref count is only 1 */
  3685. if (found_type != BTRFS_EXTENT_DATA_KEY)
  3686. goto delete;
  3687. if (del_item)
  3688. last_size = found_key.offset;
  3689. else
  3690. last_size = new_size;
  3691. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  3692. u64 num_dec;
  3693. extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
  3694. if (!del_item) {
  3695. u64 orig_num_bytes =
  3696. btrfs_file_extent_num_bytes(leaf, fi);
  3697. extent_num_bytes = ALIGN(new_size -
  3698. found_key.offset,
  3699. root->sectorsize);
  3700. btrfs_set_file_extent_num_bytes(leaf, fi,
  3701. extent_num_bytes);
  3702. num_dec = (orig_num_bytes -
  3703. extent_num_bytes);
  3704. if (test_bit(BTRFS_ROOT_REF_COWS,
  3705. &root->state) &&
  3706. extent_start != 0)
  3707. inode_sub_bytes(inode, num_dec);
  3708. btrfs_mark_buffer_dirty(leaf);
  3709. } else {
  3710. extent_num_bytes =
  3711. btrfs_file_extent_disk_num_bytes(leaf,
  3712. fi);
  3713. extent_offset = found_key.offset -
  3714. btrfs_file_extent_offset(leaf, fi);
  3715. /* FIXME blocksize != 4096 */
  3716. num_dec = btrfs_file_extent_num_bytes(leaf, fi);
  3717. if (extent_start != 0) {
  3718. found_extent = 1;
  3719. if (test_bit(BTRFS_ROOT_REF_COWS,
  3720. &root->state))
  3721. inode_sub_bytes(inode, num_dec);
  3722. }
  3723. }
  3724. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  3725. /*
  3726. * we can't truncate inline items that have had
  3727. * special encodings
  3728. */
  3729. if (!del_item &&
  3730. btrfs_file_extent_compression(leaf, fi) == 0 &&
  3731. btrfs_file_extent_encryption(leaf, fi) == 0 &&
  3732. btrfs_file_extent_other_encoding(leaf, fi) == 0) {
  3733. u32 size = new_size - found_key.offset;
  3734. if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
  3735. inode_sub_bytes(inode, item_end + 1 -
  3736. new_size);
  3737. /*
  3738. * update the ram bytes to properly reflect
  3739. * the new size of our item
  3740. */
  3741. btrfs_set_file_extent_ram_bytes(leaf, fi, size);
  3742. size =
  3743. btrfs_file_extent_calc_inline_size(size);
  3744. btrfs_truncate_item(root, path, size, 1);
  3745. } else if (test_bit(BTRFS_ROOT_REF_COWS,
  3746. &root->state)) {
  3747. inode_sub_bytes(inode, item_end + 1 -
  3748. found_key.offset);
  3749. }
  3750. }
  3751. delete:
  3752. if (del_item) {
  3753. if (!pending_del_nr) {
  3754. /* no pending yet, add ourselves */
  3755. pending_del_slot = path->slots[0];
  3756. pending_del_nr = 1;
  3757. } else if (pending_del_nr &&
  3758. path->slots[0] + 1 == pending_del_slot) {
  3759. /* hop on the pending chunk */
  3760. pending_del_nr++;
  3761. pending_del_slot = path->slots[0];
  3762. } else {
  3763. BUG();
  3764. }
  3765. } else {
  3766. break;
  3767. }
  3768. if (found_extent &&
  3769. (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
  3770. root == root->fs_info->tree_root)) {
  3771. btrfs_set_path_blocking(path);
  3772. ret = btrfs_free_extent(trans, root, extent_start,
  3773. extent_num_bytes, 0,
  3774. btrfs_header_owner(leaf),
  3775. ino, extent_offset, 0);
  3776. BUG_ON(ret);
  3777. }
  3778. if (found_type == BTRFS_INODE_ITEM_KEY)
  3779. break;
  3780. if (path->slots[0] == 0 ||
  3781. path->slots[0] != pending_del_slot) {
  3782. if (pending_del_nr) {
  3783. ret = btrfs_del_items(trans, root, path,
  3784. pending_del_slot,
  3785. pending_del_nr);
  3786. if (ret) {
  3787. btrfs_abort_transaction(trans,
  3788. root, ret);
  3789. goto error;
  3790. }
  3791. pending_del_nr = 0;
  3792. }
  3793. btrfs_release_path(path);
  3794. goto search_again;
  3795. } else {
  3796. path->slots[0]--;
  3797. }
  3798. }
  3799. out:
  3800. if (pending_del_nr) {
  3801. ret = btrfs_del_items(trans, root, path, pending_del_slot,
  3802. pending_del_nr);
  3803. if (ret)
  3804. btrfs_abort_transaction(trans, root, ret);
  3805. }
  3806. error:
  3807. if (last_size != (u64)-1 &&
  3808. root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  3809. btrfs_ordered_update_i_size(inode, last_size, NULL);
  3810. btrfs_free_path(path);
  3811. return err;
  3812. }
  3813. /*
  3814. * btrfs_truncate_page - read, zero a chunk and write a page
  3815. * @inode - inode that we're zeroing
  3816. * @from - the offset to start zeroing
  3817. * @len - the length to zero, 0 to zero the entire range respective to the
  3818. * offset
  3819. * @front - zero up to the offset instead of from the offset on
  3820. *
  3821. * This will find the page for the "from" offset and cow the page and zero the
  3822. * part we want to zero. This is used with truncate and hole punching.
  3823. */
  3824. int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
  3825. int front)
  3826. {
  3827. struct address_space *mapping = inode->i_mapping;
  3828. struct btrfs_root *root = BTRFS_I(inode)->root;
  3829. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  3830. struct btrfs_ordered_extent *ordered;
  3831. struct extent_state *cached_state = NULL;
  3832. char *kaddr;
  3833. u32 blocksize = root->sectorsize;
  3834. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  3835. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  3836. struct page *page;
  3837. gfp_t mask = btrfs_alloc_write_mask(mapping);
  3838. int ret = 0;
  3839. u64 page_start;
  3840. u64 page_end;
  3841. if ((offset & (blocksize - 1)) == 0 &&
  3842. (!len || ((len & (blocksize - 1)) == 0)))
  3843. goto out;
  3844. ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
  3845. if (ret)
  3846. goto out;
  3847. again:
  3848. page = find_or_create_page(mapping, index, mask);
  3849. if (!page) {
  3850. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  3851. ret = -ENOMEM;
  3852. goto out;
  3853. }
  3854. page_start = page_offset(page);
  3855. page_end = page_start + PAGE_CACHE_SIZE - 1;
  3856. if (!PageUptodate(page)) {
  3857. ret = btrfs_readpage(NULL, page);
  3858. lock_page(page);
  3859. if (page->mapping != mapping) {
  3860. unlock_page(page);
  3861. page_cache_release(page);
  3862. goto again;
  3863. }
  3864. if (!PageUptodate(page)) {
  3865. ret = -EIO;
  3866. goto out_unlock;
  3867. }
  3868. }
  3869. wait_on_page_writeback(page);
  3870. lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
  3871. set_page_extent_mapped(page);
  3872. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  3873. if (ordered) {
  3874. unlock_extent_cached(io_tree, page_start, page_end,
  3875. &cached_state, GFP_NOFS);
  3876. unlock_page(page);
  3877. page_cache_release(page);
  3878. btrfs_start_ordered_extent(inode, ordered, 1);
  3879. btrfs_put_ordered_extent(ordered);
  3880. goto again;
  3881. }
  3882. clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
  3883. EXTENT_DIRTY | EXTENT_DELALLOC |
  3884. EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
  3885. 0, 0, &cached_state, GFP_NOFS);
  3886. ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
  3887. &cached_state);
  3888. if (ret) {
  3889. unlock_extent_cached(io_tree, page_start, page_end,
  3890. &cached_state, GFP_NOFS);
  3891. goto out_unlock;
  3892. }
  3893. if (offset != PAGE_CACHE_SIZE) {
  3894. if (!len)
  3895. len = PAGE_CACHE_SIZE - offset;
  3896. kaddr = kmap(page);
  3897. if (front)
  3898. memset(kaddr, 0, offset);
  3899. else
  3900. memset(kaddr + offset, 0, len);
  3901. flush_dcache_page(page);
  3902. kunmap(page);
  3903. }
  3904. ClearPageChecked(page);
  3905. set_page_dirty(page);
  3906. unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
  3907. GFP_NOFS);
  3908. out_unlock:
  3909. if (ret)
  3910. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  3911. unlock_page(page);
  3912. page_cache_release(page);
  3913. out:
  3914. return ret;
  3915. }
  3916. static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
  3917. u64 offset, u64 len)
  3918. {
  3919. struct btrfs_trans_handle *trans;
  3920. int ret;
  3921. /*
  3922. * Still need to make sure the inode looks like it's been updated so
  3923. * that any holes get logged if we fsync.
  3924. */
  3925. if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
  3926. BTRFS_I(inode)->last_trans = root->fs_info->generation;
  3927. BTRFS_I(inode)->last_sub_trans = root->log_transid;
  3928. BTRFS_I(inode)->last_log_commit = root->last_log_commit;
  3929. return 0;
  3930. }
  3931. /*
  3932. * 1 - for the one we're dropping
  3933. * 1 - for the one we're adding
  3934. * 1 - for updating the inode.
  3935. */
  3936. trans = btrfs_start_transaction(root, 3);
  3937. if (IS_ERR(trans))
  3938. return PTR_ERR(trans);
  3939. ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
  3940. if (ret) {
  3941. btrfs_abort_transaction(trans, root, ret);
  3942. btrfs_end_transaction(trans, root);
  3943. return ret;
  3944. }
  3945. ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
  3946. 0, 0, len, 0, len, 0, 0, 0);
  3947. if (ret)
  3948. btrfs_abort_transaction(trans, root, ret);
  3949. else
  3950. btrfs_update_inode(trans, root, inode);
  3951. btrfs_end_transaction(trans, root);
  3952. return ret;
  3953. }
  3954. /*
  3955. * This function puts in dummy file extents for the area we're creating a hole
  3956. * for. So if we are truncating this file to a larger size we need to insert
  3957. * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
  3958. * the range between oldsize and size
  3959. */
  3960. int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
  3961. {
  3962. struct btrfs_root *root = BTRFS_I(inode)->root;
  3963. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  3964. struct extent_map *em = NULL;
  3965. struct extent_state *cached_state = NULL;
  3966. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  3967. u64 hole_start = ALIGN(oldsize, root->sectorsize);
  3968. u64 block_end = ALIGN(size, root->sectorsize);
  3969. u64 last_byte;
  3970. u64 cur_offset;
  3971. u64 hole_size;
  3972. int err = 0;
  3973. /*
  3974. * If our size started in the middle of a page we need to zero out the
  3975. * rest of the page before we expand the i_size, otherwise we could
  3976. * expose stale data.
  3977. */
  3978. err = btrfs_truncate_page(inode, oldsize, 0, 0);
  3979. if (err)
  3980. return err;
  3981. if (size <= hole_start)
  3982. return 0;
  3983. while (1) {
  3984. struct btrfs_ordered_extent *ordered;
  3985. lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
  3986. &cached_state);
  3987. ordered = btrfs_lookup_ordered_range(inode, hole_start,
  3988. block_end - hole_start);
  3989. if (!ordered)
  3990. break;
  3991. unlock_extent_cached(io_tree, hole_start, block_end - 1,
  3992. &cached_state, GFP_NOFS);
  3993. btrfs_start_ordered_extent(inode, ordered, 1);
  3994. btrfs_put_ordered_extent(ordered);
  3995. }
  3996. cur_offset = hole_start;
  3997. while (1) {
  3998. em = btrfs_get_extent(inode, NULL, 0, cur_offset,
  3999. block_end - cur_offset, 0);
  4000. if (IS_ERR(em)) {
  4001. err = PTR_ERR(em);
  4002. em = NULL;
  4003. break;
  4004. }
  4005. last_byte = min(extent_map_end(em), block_end);
  4006. last_byte = ALIGN(last_byte , root->sectorsize);
  4007. if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
  4008. struct extent_map *hole_em;
  4009. hole_size = last_byte - cur_offset;
  4010. err = maybe_insert_hole(root, inode, cur_offset,
  4011. hole_size);
  4012. if (err)
  4013. break;
  4014. btrfs_drop_extent_cache(inode, cur_offset,
  4015. cur_offset + hole_size - 1, 0);
  4016. hole_em = alloc_extent_map();
  4017. if (!hole_em) {
  4018. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  4019. &BTRFS_I(inode)->runtime_flags);
  4020. goto next;
  4021. }
  4022. hole_em->start = cur_offset;
  4023. hole_em->len = hole_size;
  4024. hole_em->orig_start = cur_offset;
  4025. hole_em->block_start = EXTENT_MAP_HOLE;
  4026. hole_em->block_len = 0;
  4027. hole_em->orig_block_len = 0;
  4028. hole_em->ram_bytes = hole_size;
  4029. hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
  4030. hole_em->compress_type = BTRFS_COMPRESS_NONE;
  4031. hole_em->generation = root->fs_info->generation;
  4032. while (1) {
  4033. write_lock(&em_tree->lock);
  4034. err = add_extent_mapping(em_tree, hole_em, 1);
  4035. write_unlock(&em_tree->lock);
  4036. if (err != -EEXIST)
  4037. break;
  4038. btrfs_drop_extent_cache(inode, cur_offset,
  4039. cur_offset +
  4040. hole_size - 1, 0);
  4041. }
  4042. free_extent_map(hole_em);
  4043. }
  4044. next:
  4045. free_extent_map(em);
  4046. em = NULL;
  4047. cur_offset = last_byte;
  4048. if (cur_offset >= block_end)
  4049. break;
  4050. }
  4051. free_extent_map(em);
  4052. unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
  4053. GFP_NOFS);
  4054. return err;
  4055. }
  4056. static int wait_snapshoting_atomic_t(atomic_t *a)
  4057. {
  4058. schedule();
  4059. return 0;
  4060. }
  4061. static void wait_for_snapshot_creation(struct btrfs_root *root)
  4062. {
  4063. while (true) {
  4064. int ret;
  4065. ret = btrfs_start_write_no_snapshoting(root);
  4066. if (ret)
  4067. break;
  4068. wait_on_atomic_t(&root->will_be_snapshoted,
  4069. wait_snapshoting_atomic_t,
  4070. TASK_UNINTERRUPTIBLE);
  4071. }
  4072. }
  4073. static int btrfs_setsize(struct inode *inode, struct iattr *attr)
  4074. {
  4075. struct btrfs_root *root = BTRFS_I(inode)->root;
  4076. struct btrfs_trans_handle *trans;
  4077. loff_t oldsize = i_size_read(inode);
  4078. loff_t newsize = attr->ia_size;
  4079. int mask = attr->ia_valid;
  4080. int ret;
  4081. /*
  4082. * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
  4083. * special case where we need to update the times despite not having
  4084. * these flags set. For all other operations the VFS set these flags
  4085. * explicitly if it wants a timestamp update.
  4086. */
  4087. if (newsize != oldsize) {
  4088. inode_inc_iversion(inode);
  4089. if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
  4090. inode->i_ctime = inode->i_mtime =
  4091. current_fs_time(inode->i_sb);
  4092. }
  4093. if (newsize > oldsize) {
  4094. truncate_pagecache(inode, newsize);
  4095. /*
  4096. * Don't do an expanding truncate while snapshoting is ongoing.
  4097. * This is to ensure the snapshot captures a fully consistent
  4098. * state of this file - if the snapshot captures this expanding
  4099. * truncation, it must capture all writes that happened before
  4100. * this truncation.
  4101. */
  4102. wait_for_snapshot_creation(root);
  4103. ret = btrfs_cont_expand(inode, oldsize, newsize);
  4104. if (ret) {
  4105. btrfs_end_write_no_snapshoting(root);
  4106. return ret;
  4107. }
  4108. trans = btrfs_start_transaction(root, 1);
  4109. if (IS_ERR(trans)) {
  4110. btrfs_end_write_no_snapshoting(root);
  4111. return PTR_ERR(trans);
  4112. }
  4113. i_size_write(inode, newsize);
  4114. btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
  4115. ret = btrfs_update_inode(trans, root, inode);
  4116. btrfs_end_write_no_snapshoting(root);
  4117. btrfs_end_transaction(trans, root);
  4118. } else {
  4119. /*
  4120. * We're truncating a file that used to have good data down to
  4121. * zero. Make sure it gets into the ordered flush list so that
  4122. * any new writes get down to disk quickly.
  4123. */
  4124. if (newsize == 0)
  4125. set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
  4126. &BTRFS_I(inode)->runtime_flags);
  4127. /*
  4128. * 1 for the orphan item we're going to add
  4129. * 1 for the orphan item deletion.
  4130. */
  4131. trans = btrfs_start_transaction(root, 2);
  4132. if (IS_ERR(trans))
  4133. return PTR_ERR(trans);
  4134. /*
  4135. * We need to do this in case we fail at _any_ point during the
  4136. * actual truncate. Once we do the truncate_setsize we could
  4137. * invalidate pages which forces any outstanding ordered io to
  4138. * be instantly completed which will give us extents that need
  4139. * to be truncated. If we fail to get an orphan inode down we
  4140. * could have left over extents that were never meant to live,
  4141. * so we need to garuntee from this point on that everything
  4142. * will be consistent.
  4143. */
  4144. ret = btrfs_orphan_add(trans, inode);
  4145. btrfs_end_transaction(trans, root);
  4146. if (ret)
  4147. return ret;
  4148. /* we don't support swapfiles, so vmtruncate shouldn't fail */
  4149. truncate_setsize(inode, newsize);
  4150. /* Disable nonlocked read DIO to avoid the end less truncate */
  4151. btrfs_inode_block_unlocked_dio(inode);
  4152. inode_dio_wait(inode);
  4153. btrfs_inode_resume_unlocked_dio(inode);
  4154. ret = btrfs_truncate(inode);
  4155. if (ret && inode->i_nlink) {
  4156. int err;
  4157. /*
  4158. * failed to truncate, disk_i_size is only adjusted down
  4159. * as we remove extents, so it should represent the true
  4160. * size of the inode, so reset the in memory size and
  4161. * delete our orphan entry.
  4162. */
  4163. trans = btrfs_join_transaction(root);
  4164. if (IS_ERR(trans)) {
  4165. btrfs_orphan_del(NULL, inode);
  4166. return ret;
  4167. }
  4168. i_size_write(inode, BTRFS_I(inode)->disk_i_size);
  4169. err = btrfs_orphan_del(trans, inode);
  4170. if (err)
  4171. btrfs_abort_transaction(trans, root, err);
  4172. btrfs_end_transaction(trans, root);
  4173. }
  4174. }
  4175. return ret;
  4176. }
  4177. static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
  4178. {
  4179. struct inode *inode = dentry->d_inode;
  4180. struct btrfs_root *root = BTRFS_I(inode)->root;
  4181. int err;
  4182. if (btrfs_root_readonly(root))
  4183. return -EROFS;
  4184. err = inode_change_ok(inode, attr);
  4185. if (err)
  4186. return err;
  4187. if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
  4188. err = btrfs_setsize(inode, attr);
  4189. if (err)
  4190. return err;
  4191. }
  4192. if (attr->ia_valid) {
  4193. setattr_copy(inode, attr);
  4194. inode_inc_iversion(inode);
  4195. err = btrfs_dirty_inode(inode);
  4196. if (!err && attr->ia_valid & ATTR_MODE)
  4197. err = posix_acl_chmod(inode, inode->i_mode);
  4198. }
  4199. return err;
  4200. }
  4201. /*
  4202. * While truncating the inode pages during eviction, we get the VFS calling
  4203. * btrfs_invalidatepage() against each page of the inode. This is slow because
  4204. * the calls to btrfs_invalidatepage() result in a huge amount of calls to
  4205. * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
  4206. * extent_state structures over and over, wasting lots of time.
  4207. *
  4208. * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
  4209. * those expensive operations on a per page basis and do only the ordered io
  4210. * finishing, while we release here the extent_map and extent_state structures,
  4211. * without the excessive merging and splitting.
  4212. */
  4213. static void evict_inode_truncate_pages(struct inode *inode)
  4214. {
  4215. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  4216. struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
  4217. struct rb_node *node;
  4218. ASSERT(inode->i_state & I_FREEING);
  4219. truncate_inode_pages_final(&inode->i_data);
  4220. write_lock(&map_tree->lock);
  4221. while (!RB_EMPTY_ROOT(&map_tree->map)) {
  4222. struct extent_map *em;
  4223. node = rb_first(&map_tree->map);
  4224. em = rb_entry(node, struct extent_map, rb_node);
  4225. clear_bit(EXTENT_FLAG_PINNED, &em->flags);
  4226. clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
  4227. remove_extent_mapping(map_tree, em);
  4228. free_extent_map(em);
  4229. if (need_resched()) {
  4230. write_unlock(&map_tree->lock);
  4231. cond_resched();
  4232. write_lock(&map_tree->lock);
  4233. }
  4234. }
  4235. write_unlock(&map_tree->lock);
  4236. spin_lock(&io_tree->lock);
  4237. while (!RB_EMPTY_ROOT(&io_tree->state)) {
  4238. struct extent_state *state;
  4239. struct extent_state *cached_state = NULL;
  4240. node = rb_first(&io_tree->state);
  4241. state = rb_entry(node, struct extent_state, rb_node);
  4242. atomic_inc(&state->refs);
  4243. spin_unlock(&io_tree->lock);
  4244. lock_extent_bits(io_tree, state->start, state->end,
  4245. 0, &cached_state);
  4246. clear_extent_bit(io_tree, state->start, state->end,
  4247. EXTENT_LOCKED | EXTENT_DIRTY |
  4248. EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
  4249. EXTENT_DEFRAG, 1, 1,
  4250. &cached_state, GFP_NOFS);
  4251. free_extent_state(state);
  4252. cond_resched();
  4253. spin_lock(&io_tree->lock);
  4254. }
  4255. spin_unlock(&io_tree->lock);
  4256. }
  4257. void btrfs_evict_inode(struct inode *inode)
  4258. {
  4259. struct btrfs_trans_handle *trans;
  4260. struct btrfs_root *root = BTRFS_I(inode)->root;
  4261. struct btrfs_block_rsv *rsv, *global_rsv;
  4262. u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
  4263. int ret;
  4264. trace_btrfs_inode_evict(inode);
  4265. evict_inode_truncate_pages(inode);
  4266. if (inode->i_nlink &&
  4267. ((btrfs_root_refs(&root->root_item) != 0 &&
  4268. root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
  4269. btrfs_is_free_space_inode(inode)))
  4270. goto no_delete;
  4271. if (is_bad_inode(inode)) {
  4272. btrfs_orphan_del(NULL, inode);
  4273. goto no_delete;
  4274. }
  4275. /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
  4276. btrfs_wait_ordered_range(inode, 0, (u64)-1);
  4277. btrfs_free_io_failure_record(inode, 0, (u64)-1);
  4278. if (root->fs_info->log_root_recovering) {
  4279. BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
  4280. &BTRFS_I(inode)->runtime_flags));
  4281. goto no_delete;
  4282. }
  4283. if (inode->i_nlink > 0) {
  4284. BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
  4285. root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
  4286. goto no_delete;
  4287. }
  4288. ret = btrfs_commit_inode_delayed_inode(inode);
  4289. if (ret) {
  4290. btrfs_orphan_del(NULL, inode);
  4291. goto no_delete;
  4292. }
  4293. rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
  4294. if (!rsv) {
  4295. btrfs_orphan_del(NULL, inode);
  4296. goto no_delete;
  4297. }
  4298. rsv->size = min_size;
  4299. rsv->failfast = 1;
  4300. global_rsv = &root->fs_info->global_block_rsv;
  4301. btrfs_i_size_write(inode, 0);
  4302. /*
  4303. * This is a bit simpler than btrfs_truncate since we've already
  4304. * reserved our space for our orphan item in the unlink, so we just
  4305. * need to reserve some slack space in case we add bytes and update
  4306. * inode item when doing the truncate.
  4307. */
  4308. while (1) {
  4309. ret = btrfs_block_rsv_refill(root, rsv, min_size,
  4310. BTRFS_RESERVE_FLUSH_LIMIT);
  4311. /*
  4312. * Try and steal from the global reserve since we will
  4313. * likely not use this space anyway, we want to try as
  4314. * hard as possible to get this to work.
  4315. */
  4316. if (ret)
  4317. ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
  4318. if (ret) {
  4319. btrfs_warn(root->fs_info,
  4320. "Could not get space for a delete, will truncate on mount %d",
  4321. ret);
  4322. btrfs_orphan_del(NULL, inode);
  4323. btrfs_free_block_rsv(root, rsv);
  4324. goto no_delete;
  4325. }
  4326. trans = btrfs_join_transaction(root);
  4327. if (IS_ERR(trans)) {
  4328. btrfs_orphan_del(NULL, inode);
  4329. btrfs_free_block_rsv(root, rsv);
  4330. goto no_delete;
  4331. }
  4332. trans->block_rsv = rsv;
  4333. ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
  4334. if (ret != -ENOSPC)
  4335. break;
  4336. trans->block_rsv = &root->fs_info->trans_block_rsv;
  4337. btrfs_end_transaction(trans, root);
  4338. trans = NULL;
  4339. btrfs_btree_balance_dirty(root);
  4340. }
  4341. btrfs_free_block_rsv(root, rsv);
  4342. /*
  4343. * Errors here aren't a big deal, it just means we leave orphan items
  4344. * in the tree. They will be cleaned up on the next mount.
  4345. */
  4346. if (ret == 0) {
  4347. trans->block_rsv = root->orphan_block_rsv;
  4348. btrfs_orphan_del(trans, inode);
  4349. } else {
  4350. btrfs_orphan_del(NULL, inode);
  4351. }
  4352. trans->block_rsv = &root->fs_info->trans_block_rsv;
  4353. if (!(root == root->fs_info->tree_root ||
  4354. root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
  4355. btrfs_return_ino(root, btrfs_ino(inode));
  4356. btrfs_end_transaction(trans, root);
  4357. btrfs_btree_balance_dirty(root);
  4358. no_delete:
  4359. btrfs_remove_delayed_node(inode);
  4360. clear_inode(inode);
  4361. return;
  4362. }
  4363. /*
  4364. * this returns the key found in the dir entry in the location pointer.
  4365. * If no dir entries were found, location->objectid is 0.
  4366. */
  4367. static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
  4368. struct btrfs_key *location)
  4369. {
  4370. const char *name = dentry->d_name.name;
  4371. int namelen = dentry->d_name.len;
  4372. struct btrfs_dir_item *di;
  4373. struct btrfs_path *path;
  4374. struct btrfs_root *root = BTRFS_I(dir)->root;
  4375. int ret = 0;
  4376. path = btrfs_alloc_path();
  4377. if (!path)
  4378. return -ENOMEM;
  4379. di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
  4380. namelen, 0);
  4381. if (IS_ERR(di))
  4382. ret = PTR_ERR(di);
  4383. if (IS_ERR_OR_NULL(di))
  4384. goto out_err;
  4385. btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
  4386. out:
  4387. btrfs_free_path(path);
  4388. return ret;
  4389. out_err:
  4390. location->objectid = 0;
  4391. goto out;
  4392. }
  4393. /*
  4394. * when we hit a tree root in a directory, the btrfs part of the inode
  4395. * needs to be changed to reflect the root directory of the tree root. This
  4396. * is kind of like crossing a mount point.
  4397. */
  4398. static int fixup_tree_root_location(struct btrfs_root *root,
  4399. struct inode *dir,
  4400. struct dentry *dentry,
  4401. struct btrfs_key *location,
  4402. struct btrfs_root **sub_root)
  4403. {
  4404. struct btrfs_path *path;
  4405. struct btrfs_root *new_root;
  4406. struct btrfs_root_ref *ref;
  4407. struct extent_buffer *leaf;
  4408. int ret;
  4409. int err = 0;
  4410. path = btrfs_alloc_path();
  4411. if (!path) {
  4412. err = -ENOMEM;
  4413. goto out;
  4414. }
  4415. err = -ENOENT;
  4416. ret = btrfs_find_item(root->fs_info->tree_root, path,
  4417. BTRFS_I(dir)->root->root_key.objectid,
  4418. location->objectid, BTRFS_ROOT_REF_KEY, NULL);
  4419. if (ret) {
  4420. if (ret < 0)
  4421. err = ret;
  4422. goto out;
  4423. }
  4424. leaf = path->nodes[0];
  4425. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
  4426. if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
  4427. btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
  4428. goto out;
  4429. ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
  4430. (unsigned long)(ref + 1),
  4431. dentry->d_name.len);
  4432. if (ret)
  4433. goto out;
  4434. btrfs_release_path(path);
  4435. new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
  4436. if (IS_ERR(new_root)) {
  4437. err = PTR_ERR(new_root);
  4438. goto out;
  4439. }
  4440. *sub_root = new_root;
  4441. location->objectid = btrfs_root_dirid(&new_root->root_item);
  4442. location->type = BTRFS_INODE_ITEM_KEY;
  4443. location->offset = 0;
  4444. err = 0;
  4445. out:
  4446. btrfs_free_path(path);
  4447. return err;
  4448. }
  4449. static void inode_tree_add(struct inode *inode)
  4450. {
  4451. struct btrfs_root *root = BTRFS_I(inode)->root;
  4452. struct btrfs_inode *entry;
  4453. struct rb_node **p;
  4454. struct rb_node *parent;
  4455. struct rb_node *new = &BTRFS_I(inode)->rb_node;
  4456. u64 ino = btrfs_ino(inode);
  4457. if (inode_unhashed(inode))
  4458. return;
  4459. parent = NULL;
  4460. spin_lock(&root->inode_lock);
  4461. p = &root->inode_tree.rb_node;
  4462. while (*p) {
  4463. parent = *p;
  4464. entry = rb_entry(parent, struct btrfs_inode, rb_node);
  4465. if (ino < btrfs_ino(&entry->vfs_inode))
  4466. p = &parent->rb_left;
  4467. else if (ino > btrfs_ino(&entry->vfs_inode))
  4468. p = &parent->rb_right;
  4469. else {
  4470. WARN_ON(!(entry->vfs_inode.i_state &
  4471. (I_WILL_FREE | I_FREEING)));
  4472. rb_replace_node(parent, new, &root->inode_tree);
  4473. RB_CLEAR_NODE(parent);
  4474. spin_unlock(&root->inode_lock);
  4475. return;
  4476. }
  4477. }
  4478. rb_link_node(new, parent, p);
  4479. rb_insert_color(new, &root->inode_tree);
  4480. spin_unlock(&root->inode_lock);
  4481. }
  4482. static void inode_tree_del(struct inode *inode)
  4483. {
  4484. struct btrfs_root *root = BTRFS_I(inode)->root;
  4485. int empty = 0;
  4486. spin_lock(&root->inode_lock);
  4487. if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
  4488. rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
  4489. RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
  4490. empty = RB_EMPTY_ROOT(&root->inode_tree);
  4491. }
  4492. spin_unlock(&root->inode_lock);
  4493. if (empty && btrfs_root_refs(&root->root_item) == 0) {
  4494. synchronize_srcu(&root->fs_info->subvol_srcu);
  4495. spin_lock(&root->inode_lock);
  4496. empty = RB_EMPTY_ROOT(&root->inode_tree);
  4497. spin_unlock(&root->inode_lock);
  4498. if (empty)
  4499. btrfs_add_dead_root(root);
  4500. }
  4501. }
  4502. void btrfs_invalidate_inodes(struct btrfs_root *root)
  4503. {
  4504. struct rb_node *node;
  4505. struct rb_node *prev;
  4506. struct btrfs_inode *entry;
  4507. struct inode *inode;
  4508. u64 objectid = 0;
  4509. if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
  4510. WARN_ON(btrfs_root_refs(&root->root_item) != 0);
  4511. spin_lock(&root->inode_lock);
  4512. again:
  4513. node = root->inode_tree.rb_node;
  4514. prev = NULL;
  4515. while (node) {
  4516. prev = node;
  4517. entry = rb_entry(node, struct btrfs_inode, rb_node);
  4518. if (objectid < btrfs_ino(&entry->vfs_inode))
  4519. node = node->rb_left;
  4520. else if (objectid > btrfs_ino(&entry->vfs_inode))
  4521. node = node->rb_right;
  4522. else
  4523. break;
  4524. }
  4525. if (!node) {
  4526. while (prev) {
  4527. entry = rb_entry(prev, struct btrfs_inode, rb_node);
  4528. if (objectid <= btrfs_ino(&entry->vfs_inode)) {
  4529. node = prev;
  4530. break;
  4531. }
  4532. prev = rb_next(prev);
  4533. }
  4534. }
  4535. while (node) {
  4536. entry = rb_entry(node, struct btrfs_inode, rb_node);
  4537. objectid = btrfs_ino(&entry->vfs_inode) + 1;
  4538. inode = igrab(&entry->vfs_inode);
  4539. if (inode) {
  4540. spin_unlock(&root->inode_lock);
  4541. if (atomic_read(&inode->i_count) > 1)
  4542. d_prune_aliases(inode);
  4543. /*
  4544. * btrfs_drop_inode will have it removed from
  4545. * the inode cache when its usage count
  4546. * hits zero.
  4547. */
  4548. iput(inode);
  4549. cond_resched();
  4550. spin_lock(&root->inode_lock);
  4551. goto again;
  4552. }
  4553. if (cond_resched_lock(&root->inode_lock))
  4554. goto again;
  4555. node = rb_next(node);
  4556. }
  4557. spin_unlock(&root->inode_lock);
  4558. }
  4559. static int btrfs_init_locked_inode(struct inode *inode, void *p)
  4560. {
  4561. struct btrfs_iget_args *args = p;
  4562. inode->i_ino = args->location->objectid;
  4563. memcpy(&BTRFS_I(inode)->location, args->location,
  4564. sizeof(*args->location));
  4565. BTRFS_I(inode)->root = args->root;
  4566. return 0;
  4567. }
  4568. static int btrfs_find_actor(struct inode *inode, void *opaque)
  4569. {
  4570. struct btrfs_iget_args *args = opaque;
  4571. return args->location->objectid == BTRFS_I(inode)->location.objectid &&
  4572. args->root == BTRFS_I(inode)->root;
  4573. }
  4574. static struct inode *btrfs_iget_locked(struct super_block *s,
  4575. struct btrfs_key *location,
  4576. struct btrfs_root *root)
  4577. {
  4578. struct inode *inode;
  4579. struct btrfs_iget_args args;
  4580. unsigned long hashval = btrfs_inode_hash(location->objectid, root);
  4581. args.location = location;
  4582. args.root = root;
  4583. inode = iget5_locked(s, hashval, btrfs_find_actor,
  4584. btrfs_init_locked_inode,
  4585. (void *)&args);
  4586. return inode;
  4587. }
  4588. /* Get an inode object given its location and corresponding root.
  4589. * Returns in *is_new if the inode was read from disk
  4590. */
  4591. struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
  4592. struct btrfs_root *root, int *new)
  4593. {
  4594. struct inode *inode;
  4595. inode = btrfs_iget_locked(s, location, root);
  4596. if (!inode)
  4597. return ERR_PTR(-ENOMEM);
  4598. if (inode->i_state & I_NEW) {
  4599. btrfs_read_locked_inode(inode);
  4600. if (!is_bad_inode(inode)) {
  4601. inode_tree_add(inode);
  4602. unlock_new_inode(inode);
  4603. if (new)
  4604. *new = 1;
  4605. } else {
  4606. unlock_new_inode(inode);
  4607. iput(inode);
  4608. inode = ERR_PTR(-ESTALE);
  4609. }
  4610. }
  4611. return inode;
  4612. }
  4613. static struct inode *new_simple_dir(struct super_block *s,
  4614. struct btrfs_key *key,
  4615. struct btrfs_root *root)
  4616. {
  4617. struct inode *inode = new_inode(s);
  4618. if (!inode)
  4619. return ERR_PTR(-ENOMEM);
  4620. BTRFS_I(inode)->root = root;
  4621. memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
  4622. set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
  4623. inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
  4624. inode->i_op = &btrfs_dir_ro_inode_operations;
  4625. inode->i_fop = &simple_dir_operations;
  4626. inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
  4627. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  4628. return inode;
  4629. }
  4630. struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
  4631. {
  4632. struct inode *inode;
  4633. struct btrfs_root *root = BTRFS_I(dir)->root;
  4634. struct btrfs_root *sub_root = root;
  4635. struct btrfs_key location;
  4636. int index;
  4637. int ret = 0;
  4638. if (dentry->d_name.len > BTRFS_NAME_LEN)
  4639. return ERR_PTR(-ENAMETOOLONG);
  4640. ret = btrfs_inode_by_name(dir, dentry, &location);
  4641. if (ret < 0)
  4642. return ERR_PTR(ret);
  4643. if (location.objectid == 0)
  4644. return ERR_PTR(-ENOENT);
  4645. if (location.type == BTRFS_INODE_ITEM_KEY) {
  4646. inode = btrfs_iget(dir->i_sb, &location, root, NULL);
  4647. return inode;
  4648. }
  4649. BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
  4650. index = srcu_read_lock(&root->fs_info->subvol_srcu);
  4651. ret = fixup_tree_root_location(root, dir, dentry,
  4652. &location, &sub_root);
  4653. if (ret < 0) {
  4654. if (ret != -ENOENT)
  4655. inode = ERR_PTR(ret);
  4656. else
  4657. inode = new_simple_dir(dir->i_sb, &location, sub_root);
  4658. } else {
  4659. inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
  4660. }
  4661. srcu_read_unlock(&root->fs_info->subvol_srcu, index);
  4662. if (!IS_ERR(inode) && root != sub_root) {
  4663. down_read(&root->fs_info->cleanup_work_sem);
  4664. if (!(inode->i_sb->s_flags & MS_RDONLY))
  4665. ret = btrfs_orphan_cleanup(sub_root);
  4666. up_read(&root->fs_info->cleanup_work_sem);
  4667. if (ret) {
  4668. iput(inode);
  4669. inode = ERR_PTR(ret);
  4670. }
  4671. }
  4672. return inode;
  4673. }
  4674. static int btrfs_dentry_delete(const struct dentry *dentry)
  4675. {
  4676. struct btrfs_root *root;
  4677. struct inode *inode = dentry->d_inode;
  4678. if (!inode && !IS_ROOT(dentry))
  4679. inode = dentry->d_parent->d_inode;
  4680. if (inode) {
  4681. root = BTRFS_I(inode)->root;
  4682. if (btrfs_root_refs(&root->root_item) == 0)
  4683. return 1;
  4684. if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
  4685. return 1;
  4686. }
  4687. return 0;
  4688. }
  4689. static void btrfs_dentry_release(struct dentry *dentry)
  4690. {
  4691. kfree(dentry->d_fsdata);
  4692. }
  4693. static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
  4694. unsigned int flags)
  4695. {
  4696. struct inode *inode;
  4697. inode = btrfs_lookup_dentry(dir, dentry);
  4698. if (IS_ERR(inode)) {
  4699. if (PTR_ERR(inode) == -ENOENT)
  4700. inode = NULL;
  4701. else
  4702. return ERR_CAST(inode);
  4703. }
  4704. return d_splice_alias(inode, dentry);
  4705. }
  4706. unsigned char btrfs_filetype_table[] = {
  4707. DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
  4708. };
  4709. static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
  4710. {
  4711. struct inode *inode = file_inode(file);
  4712. struct btrfs_root *root = BTRFS_I(inode)->root;
  4713. struct btrfs_item *item;
  4714. struct btrfs_dir_item *di;
  4715. struct btrfs_key key;
  4716. struct btrfs_key found_key;
  4717. struct btrfs_path *path;
  4718. struct list_head ins_list;
  4719. struct list_head del_list;
  4720. int ret;
  4721. struct extent_buffer *leaf;
  4722. int slot;
  4723. unsigned char d_type;
  4724. int over = 0;
  4725. u32 di_cur;
  4726. u32 di_total;
  4727. u32 di_len;
  4728. int key_type = BTRFS_DIR_INDEX_KEY;
  4729. char tmp_name[32];
  4730. char *name_ptr;
  4731. int name_len;
  4732. int is_curr = 0; /* ctx->pos points to the current index? */
  4733. /* FIXME, use a real flag for deciding about the key type */
  4734. if (root->fs_info->tree_root == root)
  4735. key_type = BTRFS_DIR_ITEM_KEY;
  4736. if (!dir_emit_dots(file, ctx))
  4737. return 0;
  4738. path = btrfs_alloc_path();
  4739. if (!path)
  4740. return -ENOMEM;
  4741. path->reada = 1;
  4742. if (key_type == BTRFS_DIR_INDEX_KEY) {
  4743. INIT_LIST_HEAD(&ins_list);
  4744. INIT_LIST_HEAD(&del_list);
  4745. btrfs_get_delayed_items(inode, &ins_list, &del_list);
  4746. }
  4747. key.type = key_type;
  4748. key.offset = ctx->pos;
  4749. key.objectid = btrfs_ino(inode);
  4750. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4751. if (ret < 0)
  4752. goto err;
  4753. while (1) {
  4754. leaf = path->nodes[0];
  4755. slot = path->slots[0];
  4756. if (slot >= btrfs_header_nritems(leaf)) {
  4757. ret = btrfs_next_leaf(root, path);
  4758. if (ret < 0)
  4759. goto err;
  4760. else if (ret > 0)
  4761. break;
  4762. continue;
  4763. }
  4764. item = btrfs_item_nr(slot);
  4765. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  4766. if (found_key.objectid != key.objectid)
  4767. break;
  4768. if (found_key.type != key_type)
  4769. break;
  4770. if (found_key.offset < ctx->pos)
  4771. goto next;
  4772. if (key_type == BTRFS_DIR_INDEX_KEY &&
  4773. btrfs_should_delete_dir_index(&del_list,
  4774. found_key.offset))
  4775. goto next;
  4776. ctx->pos = found_key.offset;
  4777. is_curr = 1;
  4778. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  4779. di_cur = 0;
  4780. di_total = btrfs_item_size(leaf, item);
  4781. while (di_cur < di_total) {
  4782. struct btrfs_key location;
  4783. if (verify_dir_item(root, leaf, di))
  4784. break;
  4785. name_len = btrfs_dir_name_len(leaf, di);
  4786. if (name_len <= sizeof(tmp_name)) {
  4787. name_ptr = tmp_name;
  4788. } else {
  4789. name_ptr = kmalloc(name_len, GFP_NOFS);
  4790. if (!name_ptr) {
  4791. ret = -ENOMEM;
  4792. goto err;
  4793. }
  4794. }
  4795. read_extent_buffer(leaf, name_ptr,
  4796. (unsigned long)(di + 1), name_len);
  4797. d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
  4798. btrfs_dir_item_key_to_cpu(leaf, di, &location);
  4799. /* is this a reference to our own snapshot? If so
  4800. * skip it.
  4801. *
  4802. * In contrast to old kernels, we insert the snapshot's
  4803. * dir item and dir index after it has been created, so
  4804. * we won't find a reference to our own snapshot. We
  4805. * still keep the following code for backward
  4806. * compatibility.
  4807. */
  4808. if (location.type == BTRFS_ROOT_ITEM_KEY &&
  4809. location.objectid == root->root_key.objectid) {
  4810. over = 0;
  4811. goto skip;
  4812. }
  4813. over = !dir_emit(ctx, name_ptr, name_len,
  4814. location.objectid, d_type);
  4815. skip:
  4816. if (name_ptr != tmp_name)
  4817. kfree(name_ptr);
  4818. if (over)
  4819. goto nopos;
  4820. di_len = btrfs_dir_name_len(leaf, di) +
  4821. btrfs_dir_data_len(leaf, di) + sizeof(*di);
  4822. di_cur += di_len;
  4823. di = (struct btrfs_dir_item *)((char *)di + di_len);
  4824. }
  4825. next:
  4826. path->slots[0]++;
  4827. }
  4828. if (key_type == BTRFS_DIR_INDEX_KEY) {
  4829. if (is_curr)
  4830. ctx->pos++;
  4831. ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
  4832. if (ret)
  4833. goto nopos;
  4834. }
  4835. /* Reached end of directory/root. Bump pos past the last item. */
  4836. ctx->pos++;
  4837. /*
  4838. * Stop new entries from being returned after we return the last
  4839. * entry.
  4840. *
  4841. * New directory entries are assigned a strictly increasing
  4842. * offset. This means that new entries created during readdir
  4843. * are *guaranteed* to be seen in the future by that readdir.
  4844. * This has broken buggy programs which operate on names as
  4845. * they're returned by readdir. Until we re-use freed offsets
  4846. * we have this hack to stop new entries from being returned
  4847. * under the assumption that they'll never reach this huge
  4848. * offset.
  4849. *
  4850. * This is being careful not to overflow 32bit loff_t unless the
  4851. * last entry requires it because doing so has broken 32bit apps
  4852. * in the past.
  4853. */
  4854. if (key_type == BTRFS_DIR_INDEX_KEY) {
  4855. if (ctx->pos >= INT_MAX)
  4856. ctx->pos = LLONG_MAX;
  4857. else
  4858. ctx->pos = INT_MAX;
  4859. }
  4860. nopos:
  4861. ret = 0;
  4862. err:
  4863. if (key_type == BTRFS_DIR_INDEX_KEY)
  4864. btrfs_put_delayed_items(&ins_list, &del_list);
  4865. btrfs_free_path(path);
  4866. return ret;
  4867. }
  4868. int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
  4869. {
  4870. struct btrfs_root *root = BTRFS_I(inode)->root;
  4871. struct btrfs_trans_handle *trans;
  4872. int ret = 0;
  4873. bool nolock = false;
  4874. if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
  4875. return 0;
  4876. if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
  4877. nolock = true;
  4878. if (wbc->sync_mode == WB_SYNC_ALL) {
  4879. if (nolock)
  4880. trans = btrfs_join_transaction_nolock(root);
  4881. else
  4882. trans = btrfs_join_transaction(root);
  4883. if (IS_ERR(trans))
  4884. return PTR_ERR(trans);
  4885. ret = btrfs_commit_transaction(trans, root);
  4886. }
  4887. return ret;
  4888. }
  4889. /*
  4890. * This is somewhat expensive, updating the tree every time the
  4891. * inode changes. But, it is most likely to find the inode in cache.
  4892. * FIXME, needs more benchmarking...there are no reasons other than performance
  4893. * to keep or drop this code.
  4894. */
  4895. static int btrfs_dirty_inode(struct inode *inode)
  4896. {
  4897. struct btrfs_root *root = BTRFS_I(inode)->root;
  4898. struct btrfs_trans_handle *trans;
  4899. int ret;
  4900. if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
  4901. return 0;
  4902. trans = btrfs_join_transaction(root);
  4903. if (IS_ERR(trans))
  4904. return PTR_ERR(trans);
  4905. ret = btrfs_update_inode(trans, root, inode);
  4906. if (ret && ret == -ENOSPC) {
  4907. /* whoops, lets try again with the full transaction */
  4908. btrfs_end_transaction(trans, root);
  4909. trans = btrfs_start_transaction(root, 1);
  4910. if (IS_ERR(trans))
  4911. return PTR_ERR(trans);
  4912. ret = btrfs_update_inode(trans, root, inode);
  4913. }
  4914. btrfs_end_transaction(trans, root);
  4915. if (BTRFS_I(inode)->delayed_node)
  4916. btrfs_balance_delayed_items(root);
  4917. return ret;
  4918. }
  4919. /*
  4920. * This is a copy of file_update_time. We need this so we can return error on
  4921. * ENOSPC for updating the inode in the case of file write and mmap writes.
  4922. */
  4923. static int btrfs_update_time(struct inode *inode, struct timespec *now,
  4924. int flags)
  4925. {
  4926. struct btrfs_root *root = BTRFS_I(inode)->root;
  4927. if (btrfs_root_readonly(root))
  4928. return -EROFS;
  4929. if (flags & S_VERSION)
  4930. inode_inc_iversion(inode);
  4931. if (flags & S_CTIME)
  4932. inode->i_ctime = *now;
  4933. if (flags & S_MTIME)
  4934. inode->i_mtime = *now;
  4935. if (flags & S_ATIME)
  4936. inode->i_atime = *now;
  4937. return btrfs_dirty_inode(inode);
  4938. }
  4939. /*
  4940. * find the highest existing sequence number in a directory
  4941. * and then set the in-memory index_cnt variable to reflect
  4942. * free sequence numbers
  4943. */
  4944. static int btrfs_set_inode_index_count(struct inode *inode)
  4945. {
  4946. struct btrfs_root *root = BTRFS_I(inode)->root;
  4947. struct btrfs_key key, found_key;
  4948. struct btrfs_path *path;
  4949. struct extent_buffer *leaf;
  4950. int ret;
  4951. key.objectid = btrfs_ino(inode);
  4952. key.type = BTRFS_DIR_INDEX_KEY;
  4953. key.offset = (u64)-1;
  4954. path = btrfs_alloc_path();
  4955. if (!path)
  4956. return -ENOMEM;
  4957. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4958. if (ret < 0)
  4959. goto out;
  4960. /* FIXME: we should be able to handle this */
  4961. if (ret == 0)
  4962. goto out;
  4963. ret = 0;
  4964. /*
  4965. * MAGIC NUMBER EXPLANATION:
  4966. * since we search a directory based on f_pos we have to start at 2
  4967. * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
  4968. * else has to start at 2
  4969. */
  4970. if (path->slots[0] == 0) {
  4971. BTRFS_I(inode)->index_cnt = 2;
  4972. goto out;
  4973. }
  4974. path->slots[0]--;
  4975. leaf = path->nodes[0];
  4976. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  4977. if (found_key.objectid != btrfs_ino(inode) ||
  4978. found_key.type != BTRFS_DIR_INDEX_KEY) {
  4979. BTRFS_I(inode)->index_cnt = 2;
  4980. goto out;
  4981. }
  4982. BTRFS_I(inode)->index_cnt = found_key.offset + 1;
  4983. out:
  4984. btrfs_free_path(path);
  4985. return ret;
  4986. }
  4987. /*
  4988. * helper to find a free sequence number in a given directory. This current
  4989. * code is very simple, later versions will do smarter things in the btree
  4990. */
  4991. int btrfs_set_inode_index(struct inode *dir, u64 *index)
  4992. {
  4993. int ret = 0;
  4994. if (BTRFS_I(dir)->index_cnt == (u64)-1) {
  4995. ret = btrfs_inode_delayed_dir_index_count(dir);
  4996. if (ret) {
  4997. ret = btrfs_set_inode_index_count(dir);
  4998. if (ret)
  4999. return ret;
  5000. }
  5001. }
  5002. *index = BTRFS_I(dir)->index_cnt;
  5003. BTRFS_I(dir)->index_cnt++;
  5004. return ret;
  5005. }
  5006. static int btrfs_insert_inode_locked(struct inode *inode)
  5007. {
  5008. struct btrfs_iget_args args;
  5009. args.location = &BTRFS_I(inode)->location;
  5010. args.root = BTRFS_I(inode)->root;
  5011. return insert_inode_locked4(inode,
  5012. btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
  5013. btrfs_find_actor, &args);
  5014. }
  5015. static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
  5016. struct btrfs_root *root,
  5017. struct inode *dir,
  5018. const char *name, int name_len,
  5019. u64 ref_objectid, u64 objectid,
  5020. umode_t mode, u64 *index)
  5021. {
  5022. struct inode *inode;
  5023. struct btrfs_inode_item *inode_item;
  5024. struct btrfs_key *location;
  5025. struct btrfs_path *path;
  5026. struct btrfs_inode_ref *ref;
  5027. struct btrfs_key key[2];
  5028. u32 sizes[2];
  5029. int nitems = name ? 2 : 1;
  5030. unsigned long ptr;
  5031. int ret;
  5032. path = btrfs_alloc_path();
  5033. if (!path)
  5034. return ERR_PTR(-ENOMEM);
  5035. inode = new_inode(root->fs_info->sb);
  5036. if (!inode) {
  5037. btrfs_free_path(path);
  5038. return ERR_PTR(-ENOMEM);
  5039. }
  5040. /*
  5041. * O_TMPFILE, set link count to 0, so that after this point,
  5042. * we fill in an inode item with the correct link count.
  5043. */
  5044. if (!name)
  5045. set_nlink(inode, 0);
  5046. /*
  5047. * we have to initialize this early, so we can reclaim the inode
  5048. * number if we fail afterwards in this function.
  5049. */
  5050. inode->i_ino = objectid;
  5051. if (dir && name) {
  5052. trace_btrfs_inode_request(dir);
  5053. ret = btrfs_set_inode_index(dir, index);
  5054. if (ret) {
  5055. btrfs_free_path(path);
  5056. iput(inode);
  5057. return ERR_PTR(ret);
  5058. }
  5059. } else if (dir) {
  5060. *index = 0;
  5061. }
  5062. /*
  5063. * index_cnt is ignored for everything but a dir,
  5064. * btrfs_get_inode_index_count has an explanation for the magic
  5065. * number
  5066. */
  5067. BTRFS_I(inode)->index_cnt = 2;
  5068. BTRFS_I(inode)->dir_index = *index;
  5069. BTRFS_I(inode)->root = root;
  5070. BTRFS_I(inode)->generation = trans->transid;
  5071. inode->i_generation = BTRFS_I(inode)->generation;
  5072. /*
  5073. * We could have gotten an inode number from somebody who was fsynced
  5074. * and then removed in this same transaction, so let's just set full
  5075. * sync since it will be a full sync anyway and this will blow away the
  5076. * old info in the log.
  5077. */
  5078. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
  5079. key[0].objectid = objectid;
  5080. key[0].type = BTRFS_INODE_ITEM_KEY;
  5081. key[0].offset = 0;
  5082. sizes[0] = sizeof(struct btrfs_inode_item);
  5083. if (name) {
  5084. /*
  5085. * Start new inodes with an inode_ref. This is slightly more
  5086. * efficient for small numbers of hard links since they will
  5087. * be packed into one item. Extended refs will kick in if we
  5088. * add more hard links than can fit in the ref item.
  5089. */
  5090. key[1].objectid = objectid;
  5091. key[1].type = BTRFS_INODE_REF_KEY;
  5092. key[1].offset = ref_objectid;
  5093. sizes[1] = name_len + sizeof(*ref);
  5094. }
  5095. location = &BTRFS_I(inode)->location;
  5096. location->objectid = objectid;
  5097. location->offset = 0;
  5098. location->type = BTRFS_INODE_ITEM_KEY;
  5099. ret = btrfs_insert_inode_locked(inode);
  5100. if (ret < 0)
  5101. goto fail;
  5102. path->leave_spinning = 1;
  5103. ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
  5104. if (ret != 0)
  5105. goto fail_unlock;
  5106. inode_init_owner(inode, dir, mode);
  5107. inode_set_bytes(inode, 0);
  5108. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  5109. inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  5110. struct btrfs_inode_item);
  5111. memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
  5112. sizeof(*inode_item));
  5113. fill_inode_item(trans, path->nodes[0], inode_item, inode);
  5114. if (name) {
  5115. ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
  5116. struct btrfs_inode_ref);
  5117. btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
  5118. btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
  5119. ptr = (unsigned long)(ref + 1);
  5120. write_extent_buffer(path->nodes[0], name, ptr, name_len);
  5121. }
  5122. btrfs_mark_buffer_dirty(path->nodes[0]);
  5123. btrfs_free_path(path);
  5124. btrfs_inherit_iflags(inode, dir);
  5125. if (S_ISREG(mode)) {
  5126. if (btrfs_test_opt(root, NODATASUM))
  5127. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
  5128. if (btrfs_test_opt(root, NODATACOW))
  5129. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
  5130. BTRFS_INODE_NODATASUM;
  5131. }
  5132. inode_tree_add(inode);
  5133. trace_btrfs_inode_new(inode);
  5134. btrfs_set_inode_last_trans(trans, inode);
  5135. btrfs_update_root_times(trans, root);
  5136. ret = btrfs_inode_inherit_props(trans, inode, dir);
  5137. if (ret)
  5138. btrfs_err(root->fs_info,
  5139. "error inheriting props for ino %llu (root %llu): %d",
  5140. btrfs_ino(inode), root->root_key.objectid, ret);
  5141. return inode;
  5142. fail_unlock:
  5143. unlock_new_inode(inode);
  5144. fail:
  5145. if (dir && name)
  5146. BTRFS_I(dir)->index_cnt--;
  5147. btrfs_free_path(path);
  5148. iput(inode);
  5149. return ERR_PTR(ret);
  5150. }
  5151. static inline u8 btrfs_inode_type(struct inode *inode)
  5152. {
  5153. return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
  5154. }
  5155. /*
  5156. * utility function to add 'inode' into 'parent_inode' with
  5157. * a give name and a given sequence number.
  5158. * if 'add_backref' is true, also insert a backref from the
  5159. * inode to the parent directory.
  5160. */
  5161. int btrfs_add_link(struct btrfs_trans_handle *trans,
  5162. struct inode *parent_inode, struct inode *inode,
  5163. const char *name, int name_len, int add_backref, u64 index)
  5164. {
  5165. int ret = 0;
  5166. struct btrfs_key key;
  5167. struct btrfs_root *root = BTRFS_I(parent_inode)->root;
  5168. u64 ino = btrfs_ino(inode);
  5169. u64 parent_ino = btrfs_ino(parent_inode);
  5170. if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
  5171. memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
  5172. } else {
  5173. key.objectid = ino;
  5174. key.type = BTRFS_INODE_ITEM_KEY;
  5175. key.offset = 0;
  5176. }
  5177. if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
  5178. ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
  5179. key.objectid, root->root_key.objectid,
  5180. parent_ino, index, name, name_len);
  5181. } else if (add_backref) {
  5182. ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
  5183. parent_ino, index);
  5184. }
  5185. /* Nothing to clean up yet */
  5186. if (ret)
  5187. return ret;
  5188. ret = btrfs_insert_dir_item(trans, root, name, name_len,
  5189. parent_inode, &key,
  5190. btrfs_inode_type(inode), index);
  5191. if (ret == -EEXIST || ret == -EOVERFLOW)
  5192. goto fail_dir_item;
  5193. else if (ret) {
  5194. btrfs_abort_transaction(trans, root, ret);
  5195. return ret;
  5196. }
  5197. btrfs_i_size_write(parent_inode, parent_inode->i_size +
  5198. name_len * 2);
  5199. inode_inc_iversion(parent_inode);
  5200. parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
  5201. ret = btrfs_update_inode(trans, root, parent_inode);
  5202. if (ret)
  5203. btrfs_abort_transaction(trans, root, ret);
  5204. return ret;
  5205. fail_dir_item:
  5206. if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
  5207. u64 local_index;
  5208. int err;
  5209. err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
  5210. key.objectid, root->root_key.objectid,
  5211. parent_ino, &local_index, name, name_len);
  5212. } else if (add_backref) {
  5213. u64 local_index;
  5214. int err;
  5215. err = btrfs_del_inode_ref(trans, root, name, name_len,
  5216. ino, parent_ino, &local_index);
  5217. }
  5218. return ret;
  5219. }
  5220. static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
  5221. struct inode *dir, struct dentry *dentry,
  5222. struct inode *inode, int backref, u64 index)
  5223. {
  5224. int err = btrfs_add_link(trans, dir, inode,
  5225. dentry->d_name.name, dentry->d_name.len,
  5226. backref, index);
  5227. if (err > 0)
  5228. err = -EEXIST;
  5229. return err;
  5230. }
  5231. static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
  5232. umode_t mode, dev_t rdev)
  5233. {
  5234. struct btrfs_trans_handle *trans;
  5235. struct btrfs_root *root = BTRFS_I(dir)->root;
  5236. struct inode *inode = NULL;
  5237. int err;
  5238. int drop_inode = 0;
  5239. u64 objectid;
  5240. u64 index = 0;
  5241. if (!new_valid_dev(rdev))
  5242. return -EINVAL;
  5243. /*
  5244. * 2 for inode item and ref
  5245. * 2 for dir items
  5246. * 1 for xattr if selinux is on
  5247. */
  5248. trans = btrfs_start_transaction(root, 5);
  5249. if (IS_ERR(trans))
  5250. return PTR_ERR(trans);
  5251. err = btrfs_find_free_ino(root, &objectid);
  5252. if (err)
  5253. goto out_unlock;
  5254. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  5255. dentry->d_name.len, btrfs_ino(dir), objectid,
  5256. mode, &index);
  5257. if (IS_ERR(inode)) {
  5258. err = PTR_ERR(inode);
  5259. goto out_unlock;
  5260. }
  5261. /*
  5262. * If the active LSM wants to access the inode during
  5263. * d_instantiate it needs these. Smack checks to see
  5264. * if the filesystem supports xattrs by looking at the
  5265. * ops vector.
  5266. */
  5267. inode->i_op = &btrfs_special_inode_operations;
  5268. init_special_inode(inode, inode->i_mode, rdev);
  5269. err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
  5270. if (err)
  5271. goto out_unlock_inode;
  5272. err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
  5273. if (err) {
  5274. goto out_unlock_inode;
  5275. } else {
  5276. btrfs_update_inode(trans, root, inode);
  5277. unlock_new_inode(inode);
  5278. d_instantiate(dentry, inode);
  5279. }
  5280. out_unlock:
  5281. btrfs_end_transaction(trans, root);
  5282. btrfs_balance_delayed_items(root);
  5283. btrfs_btree_balance_dirty(root);
  5284. if (drop_inode) {
  5285. inode_dec_link_count(inode);
  5286. iput(inode);
  5287. }
  5288. return err;
  5289. out_unlock_inode:
  5290. drop_inode = 1;
  5291. unlock_new_inode(inode);
  5292. goto out_unlock;
  5293. }
  5294. static int btrfs_create(struct inode *dir, struct dentry *dentry,
  5295. umode_t mode, bool excl)
  5296. {
  5297. struct btrfs_trans_handle *trans;
  5298. struct btrfs_root *root = BTRFS_I(dir)->root;
  5299. struct inode *inode = NULL;
  5300. int drop_inode_on_err = 0;
  5301. int err;
  5302. u64 objectid;
  5303. u64 index = 0;
  5304. /*
  5305. * 2 for inode item and ref
  5306. * 2 for dir items
  5307. * 1 for xattr if selinux is on
  5308. */
  5309. trans = btrfs_start_transaction(root, 5);
  5310. if (IS_ERR(trans))
  5311. return PTR_ERR(trans);
  5312. err = btrfs_find_free_ino(root, &objectid);
  5313. if (err)
  5314. goto out_unlock;
  5315. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  5316. dentry->d_name.len, btrfs_ino(dir), objectid,
  5317. mode, &index);
  5318. if (IS_ERR(inode)) {
  5319. err = PTR_ERR(inode);
  5320. goto out_unlock;
  5321. }
  5322. drop_inode_on_err = 1;
  5323. /*
  5324. * If the active LSM wants to access the inode during
  5325. * d_instantiate it needs these. Smack checks to see
  5326. * if the filesystem supports xattrs by looking at the
  5327. * ops vector.
  5328. */
  5329. inode->i_fop = &btrfs_file_operations;
  5330. inode->i_op = &btrfs_file_inode_operations;
  5331. inode->i_mapping->a_ops = &btrfs_aops;
  5332. err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
  5333. if (err)
  5334. goto out_unlock_inode;
  5335. err = btrfs_update_inode(trans, root, inode);
  5336. if (err)
  5337. goto out_unlock_inode;
  5338. err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
  5339. if (err)
  5340. goto out_unlock_inode;
  5341. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  5342. unlock_new_inode(inode);
  5343. d_instantiate(dentry, inode);
  5344. out_unlock:
  5345. btrfs_end_transaction(trans, root);
  5346. if (err && drop_inode_on_err) {
  5347. inode_dec_link_count(inode);
  5348. iput(inode);
  5349. }
  5350. btrfs_balance_delayed_items(root);
  5351. btrfs_btree_balance_dirty(root);
  5352. return err;
  5353. out_unlock_inode:
  5354. unlock_new_inode(inode);
  5355. goto out_unlock;
  5356. }
  5357. static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
  5358. struct dentry *dentry)
  5359. {
  5360. struct btrfs_trans_handle *trans;
  5361. struct btrfs_root *root = BTRFS_I(dir)->root;
  5362. struct inode *inode = old_dentry->d_inode;
  5363. u64 index;
  5364. int err;
  5365. int drop_inode = 0;
  5366. /* do not allow sys_link's with other subvols of the same device */
  5367. if (root->objectid != BTRFS_I(inode)->root->objectid)
  5368. return -EXDEV;
  5369. if (inode->i_nlink >= BTRFS_LINK_MAX)
  5370. return -EMLINK;
  5371. err = btrfs_set_inode_index(dir, &index);
  5372. if (err)
  5373. goto fail;
  5374. /*
  5375. * 2 items for inode and inode ref
  5376. * 2 items for dir items
  5377. * 1 item for parent inode
  5378. */
  5379. trans = btrfs_start_transaction(root, 5);
  5380. if (IS_ERR(trans)) {
  5381. err = PTR_ERR(trans);
  5382. goto fail;
  5383. }
  5384. /* There are several dir indexes for this inode, clear the cache. */
  5385. BTRFS_I(inode)->dir_index = 0ULL;
  5386. inc_nlink(inode);
  5387. inode_inc_iversion(inode);
  5388. inode->i_ctime = CURRENT_TIME;
  5389. ihold(inode);
  5390. set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
  5391. err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
  5392. if (err) {
  5393. drop_inode = 1;
  5394. } else {
  5395. struct dentry *parent = dentry->d_parent;
  5396. err = btrfs_update_inode(trans, root, inode);
  5397. if (err)
  5398. goto fail;
  5399. if (inode->i_nlink == 1) {
  5400. /*
  5401. * If new hard link count is 1, it's a file created
  5402. * with open(2) O_TMPFILE flag.
  5403. */
  5404. err = btrfs_orphan_del(trans, inode);
  5405. if (err)
  5406. goto fail;
  5407. }
  5408. d_instantiate(dentry, inode);
  5409. btrfs_log_new_name(trans, inode, NULL, parent);
  5410. }
  5411. btrfs_end_transaction(trans, root);
  5412. btrfs_balance_delayed_items(root);
  5413. fail:
  5414. if (drop_inode) {
  5415. inode_dec_link_count(inode);
  5416. iput(inode);
  5417. }
  5418. btrfs_btree_balance_dirty(root);
  5419. return err;
  5420. }
  5421. static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  5422. {
  5423. struct inode *inode = NULL;
  5424. struct btrfs_trans_handle *trans;
  5425. struct btrfs_root *root = BTRFS_I(dir)->root;
  5426. int err = 0;
  5427. int drop_on_err = 0;
  5428. u64 objectid = 0;
  5429. u64 index = 0;
  5430. /*
  5431. * 2 items for inode and ref
  5432. * 2 items for dir items
  5433. * 1 for xattr if selinux is on
  5434. */
  5435. trans = btrfs_start_transaction(root, 5);
  5436. if (IS_ERR(trans))
  5437. return PTR_ERR(trans);
  5438. err = btrfs_find_free_ino(root, &objectid);
  5439. if (err)
  5440. goto out_fail;
  5441. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  5442. dentry->d_name.len, btrfs_ino(dir), objectid,
  5443. S_IFDIR | mode, &index);
  5444. if (IS_ERR(inode)) {
  5445. err = PTR_ERR(inode);
  5446. goto out_fail;
  5447. }
  5448. drop_on_err = 1;
  5449. /* these must be set before we unlock the inode */
  5450. inode->i_op = &btrfs_dir_inode_operations;
  5451. inode->i_fop = &btrfs_dir_file_operations;
  5452. err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
  5453. if (err)
  5454. goto out_fail_inode;
  5455. btrfs_i_size_write(inode, 0);
  5456. err = btrfs_update_inode(trans, root, inode);
  5457. if (err)
  5458. goto out_fail_inode;
  5459. err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
  5460. dentry->d_name.len, 0, index);
  5461. if (err)
  5462. goto out_fail_inode;
  5463. d_instantiate(dentry, inode);
  5464. /*
  5465. * mkdir is special. We're unlocking after we call d_instantiate
  5466. * to avoid a race with nfsd calling d_instantiate.
  5467. */
  5468. unlock_new_inode(inode);
  5469. drop_on_err = 0;
  5470. out_fail:
  5471. btrfs_end_transaction(trans, root);
  5472. if (drop_on_err) {
  5473. inode_dec_link_count(inode);
  5474. iput(inode);
  5475. }
  5476. btrfs_balance_delayed_items(root);
  5477. btrfs_btree_balance_dirty(root);
  5478. return err;
  5479. out_fail_inode:
  5480. unlock_new_inode(inode);
  5481. goto out_fail;
  5482. }
  5483. /* Find next extent map of a given extent map, caller needs to ensure locks */
  5484. static struct extent_map *next_extent_map(struct extent_map *em)
  5485. {
  5486. struct rb_node *next;
  5487. next = rb_next(&em->rb_node);
  5488. if (!next)
  5489. return NULL;
  5490. return container_of(next, struct extent_map, rb_node);
  5491. }
  5492. static struct extent_map *prev_extent_map(struct extent_map *em)
  5493. {
  5494. struct rb_node *prev;
  5495. prev = rb_prev(&em->rb_node);
  5496. if (!prev)
  5497. return NULL;
  5498. return container_of(prev, struct extent_map, rb_node);
  5499. }
  5500. /* helper for btfs_get_extent. Given an existing extent in the tree,
  5501. * the existing extent is the nearest extent to map_start,
  5502. * and an extent that you want to insert, deal with overlap and insert
  5503. * the best fitted new extent into the tree.
  5504. */
  5505. static int merge_extent_mapping(struct extent_map_tree *em_tree,
  5506. struct extent_map *existing,
  5507. struct extent_map *em,
  5508. u64 map_start)
  5509. {
  5510. struct extent_map *prev;
  5511. struct extent_map *next;
  5512. u64 start;
  5513. u64 end;
  5514. u64 start_diff;
  5515. BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
  5516. if (existing->start > map_start) {
  5517. next = existing;
  5518. prev = prev_extent_map(next);
  5519. } else {
  5520. prev = existing;
  5521. next = next_extent_map(prev);
  5522. }
  5523. start = prev ? extent_map_end(prev) : em->start;
  5524. start = max_t(u64, start, em->start);
  5525. end = next ? next->start : extent_map_end(em);
  5526. end = min_t(u64, end, extent_map_end(em));
  5527. start_diff = start - em->start;
  5528. em->start = start;
  5529. em->len = end - start;
  5530. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  5531. !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  5532. em->block_start += start_diff;
  5533. em->block_len -= start_diff;
  5534. }
  5535. return add_extent_mapping(em_tree, em, 0);
  5536. }
  5537. static noinline int uncompress_inline(struct btrfs_path *path,
  5538. struct inode *inode, struct page *page,
  5539. size_t pg_offset, u64 extent_offset,
  5540. struct btrfs_file_extent_item *item)
  5541. {
  5542. int ret;
  5543. struct extent_buffer *leaf = path->nodes[0];
  5544. char *tmp;
  5545. size_t max_size;
  5546. unsigned long inline_size;
  5547. unsigned long ptr;
  5548. int compress_type;
  5549. WARN_ON(pg_offset != 0);
  5550. compress_type = btrfs_file_extent_compression(leaf, item);
  5551. max_size = btrfs_file_extent_ram_bytes(leaf, item);
  5552. inline_size = btrfs_file_extent_inline_item_len(leaf,
  5553. btrfs_item_nr(path->slots[0]));
  5554. tmp = kmalloc(inline_size, GFP_NOFS);
  5555. if (!tmp)
  5556. return -ENOMEM;
  5557. ptr = btrfs_file_extent_inline_start(item);
  5558. read_extent_buffer(leaf, tmp, ptr, inline_size);
  5559. max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
  5560. ret = btrfs_decompress(compress_type, tmp, page,
  5561. extent_offset, inline_size, max_size);
  5562. kfree(tmp);
  5563. return ret;
  5564. }
  5565. /*
  5566. * a bit scary, this does extent mapping from logical file offset to the disk.
  5567. * the ugly parts come from merging extents from the disk with the in-ram
  5568. * representation. This gets more complex because of the data=ordered code,
  5569. * where the in-ram extents might be locked pending data=ordered completion.
  5570. *
  5571. * This also copies inline extents directly into the page.
  5572. */
  5573. struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
  5574. size_t pg_offset, u64 start, u64 len,
  5575. int create)
  5576. {
  5577. int ret;
  5578. int err = 0;
  5579. u64 extent_start = 0;
  5580. u64 extent_end = 0;
  5581. u64 objectid = btrfs_ino(inode);
  5582. u32 found_type;
  5583. struct btrfs_path *path = NULL;
  5584. struct btrfs_root *root = BTRFS_I(inode)->root;
  5585. struct btrfs_file_extent_item *item;
  5586. struct extent_buffer *leaf;
  5587. struct btrfs_key found_key;
  5588. struct extent_map *em = NULL;
  5589. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  5590. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  5591. struct btrfs_trans_handle *trans = NULL;
  5592. const bool new_inline = !page || create;
  5593. again:
  5594. read_lock(&em_tree->lock);
  5595. em = lookup_extent_mapping(em_tree, start, len);
  5596. if (em)
  5597. em->bdev = root->fs_info->fs_devices->latest_bdev;
  5598. read_unlock(&em_tree->lock);
  5599. if (em) {
  5600. if (em->start > start || em->start + em->len <= start)
  5601. free_extent_map(em);
  5602. else if (em->block_start == EXTENT_MAP_INLINE && page)
  5603. free_extent_map(em);
  5604. else
  5605. goto out;
  5606. }
  5607. em = alloc_extent_map();
  5608. if (!em) {
  5609. err = -ENOMEM;
  5610. goto out;
  5611. }
  5612. em->bdev = root->fs_info->fs_devices->latest_bdev;
  5613. em->start = EXTENT_MAP_HOLE;
  5614. em->orig_start = EXTENT_MAP_HOLE;
  5615. em->len = (u64)-1;
  5616. em->block_len = (u64)-1;
  5617. if (!path) {
  5618. path = btrfs_alloc_path();
  5619. if (!path) {
  5620. err = -ENOMEM;
  5621. goto out;
  5622. }
  5623. /*
  5624. * Chances are we'll be called again, so go ahead and do
  5625. * readahead
  5626. */
  5627. path->reada = 1;
  5628. }
  5629. ret = btrfs_lookup_file_extent(trans, root, path,
  5630. objectid, start, trans != NULL);
  5631. if (ret < 0) {
  5632. err = ret;
  5633. goto out;
  5634. }
  5635. if (ret != 0) {
  5636. if (path->slots[0] == 0)
  5637. goto not_found;
  5638. path->slots[0]--;
  5639. }
  5640. leaf = path->nodes[0];
  5641. item = btrfs_item_ptr(leaf, path->slots[0],
  5642. struct btrfs_file_extent_item);
  5643. /* are we inside the extent that was found? */
  5644. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  5645. found_type = found_key.type;
  5646. if (found_key.objectid != objectid ||
  5647. found_type != BTRFS_EXTENT_DATA_KEY) {
  5648. /*
  5649. * If we backup past the first extent we want to move forward
  5650. * and see if there is an extent in front of us, otherwise we'll
  5651. * say there is a hole for our whole search range which can
  5652. * cause problems.
  5653. */
  5654. extent_end = start;
  5655. goto next;
  5656. }
  5657. found_type = btrfs_file_extent_type(leaf, item);
  5658. extent_start = found_key.offset;
  5659. if (found_type == BTRFS_FILE_EXTENT_REG ||
  5660. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  5661. extent_end = extent_start +
  5662. btrfs_file_extent_num_bytes(leaf, item);
  5663. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  5664. size_t size;
  5665. size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
  5666. extent_end = ALIGN(extent_start + size, root->sectorsize);
  5667. }
  5668. next:
  5669. if (start >= extent_end) {
  5670. path->slots[0]++;
  5671. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  5672. ret = btrfs_next_leaf(root, path);
  5673. if (ret < 0) {
  5674. err = ret;
  5675. goto out;
  5676. }
  5677. if (ret > 0)
  5678. goto not_found;
  5679. leaf = path->nodes[0];
  5680. }
  5681. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  5682. if (found_key.objectid != objectid ||
  5683. found_key.type != BTRFS_EXTENT_DATA_KEY)
  5684. goto not_found;
  5685. if (start + len <= found_key.offset)
  5686. goto not_found;
  5687. if (start > found_key.offset)
  5688. goto next;
  5689. em->start = start;
  5690. em->orig_start = start;
  5691. em->len = found_key.offset - start;
  5692. goto not_found_em;
  5693. }
  5694. btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
  5695. if (found_type == BTRFS_FILE_EXTENT_REG ||
  5696. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  5697. goto insert;
  5698. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  5699. unsigned long ptr;
  5700. char *map;
  5701. size_t size;
  5702. size_t extent_offset;
  5703. size_t copy_size;
  5704. if (new_inline)
  5705. goto out;
  5706. size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
  5707. extent_offset = page_offset(page) + pg_offset - extent_start;
  5708. copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
  5709. size - extent_offset);
  5710. em->start = extent_start + extent_offset;
  5711. em->len = ALIGN(copy_size, root->sectorsize);
  5712. em->orig_block_len = em->len;
  5713. em->orig_start = em->start;
  5714. ptr = btrfs_file_extent_inline_start(item) + extent_offset;
  5715. if (create == 0 && !PageUptodate(page)) {
  5716. if (btrfs_file_extent_compression(leaf, item) !=
  5717. BTRFS_COMPRESS_NONE) {
  5718. ret = uncompress_inline(path, inode, page,
  5719. pg_offset,
  5720. extent_offset, item);
  5721. if (ret) {
  5722. err = ret;
  5723. goto out;
  5724. }
  5725. } else {
  5726. map = kmap(page);
  5727. read_extent_buffer(leaf, map + pg_offset, ptr,
  5728. copy_size);
  5729. if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
  5730. memset(map + pg_offset + copy_size, 0,
  5731. PAGE_CACHE_SIZE - pg_offset -
  5732. copy_size);
  5733. }
  5734. kunmap(page);
  5735. }
  5736. flush_dcache_page(page);
  5737. } else if (create && PageUptodate(page)) {
  5738. BUG();
  5739. if (!trans) {
  5740. kunmap(page);
  5741. free_extent_map(em);
  5742. em = NULL;
  5743. btrfs_release_path(path);
  5744. trans = btrfs_join_transaction(root);
  5745. if (IS_ERR(trans))
  5746. return ERR_CAST(trans);
  5747. goto again;
  5748. }
  5749. map = kmap(page);
  5750. write_extent_buffer(leaf, map + pg_offset, ptr,
  5751. copy_size);
  5752. kunmap(page);
  5753. btrfs_mark_buffer_dirty(leaf);
  5754. }
  5755. set_extent_uptodate(io_tree, em->start,
  5756. extent_map_end(em) - 1, NULL, GFP_NOFS);
  5757. goto insert;
  5758. }
  5759. not_found:
  5760. em->start = start;
  5761. em->orig_start = start;
  5762. em->len = len;
  5763. not_found_em:
  5764. em->block_start = EXTENT_MAP_HOLE;
  5765. set_bit(EXTENT_FLAG_VACANCY, &em->flags);
  5766. insert:
  5767. btrfs_release_path(path);
  5768. if (em->start > start || extent_map_end(em) <= start) {
  5769. btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
  5770. em->start, em->len, start, len);
  5771. err = -EIO;
  5772. goto out;
  5773. }
  5774. err = 0;
  5775. write_lock(&em_tree->lock);
  5776. ret = add_extent_mapping(em_tree, em, 0);
  5777. /* it is possible that someone inserted the extent into the tree
  5778. * while we had the lock dropped. It is also possible that
  5779. * an overlapping map exists in the tree
  5780. */
  5781. if (ret == -EEXIST) {
  5782. struct extent_map *existing;
  5783. ret = 0;
  5784. existing = search_extent_mapping(em_tree, start, len);
  5785. /*
  5786. * existing will always be non-NULL, since there must be
  5787. * extent causing the -EEXIST.
  5788. */
  5789. if (start >= extent_map_end(existing) ||
  5790. start <= existing->start) {
  5791. /*
  5792. * The existing extent map is the one nearest to
  5793. * the [start, start + len) range which overlaps
  5794. */
  5795. err = merge_extent_mapping(em_tree, existing,
  5796. em, start);
  5797. free_extent_map(existing);
  5798. if (err) {
  5799. free_extent_map(em);
  5800. em = NULL;
  5801. }
  5802. } else {
  5803. free_extent_map(em);
  5804. em = existing;
  5805. err = 0;
  5806. }
  5807. }
  5808. write_unlock(&em_tree->lock);
  5809. out:
  5810. trace_btrfs_get_extent(root, em);
  5811. if (path)
  5812. btrfs_free_path(path);
  5813. if (trans) {
  5814. ret = btrfs_end_transaction(trans, root);
  5815. if (!err)
  5816. err = ret;
  5817. }
  5818. if (err) {
  5819. free_extent_map(em);
  5820. return ERR_PTR(err);
  5821. }
  5822. BUG_ON(!em); /* Error is always set */
  5823. return em;
  5824. }
  5825. struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
  5826. size_t pg_offset, u64 start, u64 len,
  5827. int create)
  5828. {
  5829. struct extent_map *em;
  5830. struct extent_map *hole_em = NULL;
  5831. u64 range_start = start;
  5832. u64 end;
  5833. u64 found;
  5834. u64 found_end;
  5835. int err = 0;
  5836. em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
  5837. if (IS_ERR(em))
  5838. return em;
  5839. if (em) {
  5840. /*
  5841. * if our em maps to
  5842. * - a hole or
  5843. * - a pre-alloc extent,
  5844. * there might actually be delalloc bytes behind it.
  5845. */
  5846. if (em->block_start != EXTENT_MAP_HOLE &&
  5847. !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  5848. return em;
  5849. else
  5850. hole_em = em;
  5851. }
  5852. /* check to see if we've wrapped (len == -1 or similar) */
  5853. end = start + len;
  5854. if (end < start)
  5855. end = (u64)-1;
  5856. else
  5857. end -= 1;
  5858. em = NULL;
  5859. /* ok, we didn't find anything, lets look for delalloc */
  5860. found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
  5861. end, len, EXTENT_DELALLOC, 1);
  5862. found_end = range_start + found;
  5863. if (found_end < range_start)
  5864. found_end = (u64)-1;
  5865. /*
  5866. * we didn't find anything useful, return
  5867. * the original results from get_extent()
  5868. */
  5869. if (range_start > end || found_end <= start) {
  5870. em = hole_em;
  5871. hole_em = NULL;
  5872. goto out;
  5873. }
  5874. /* adjust the range_start to make sure it doesn't
  5875. * go backwards from the start they passed in
  5876. */
  5877. range_start = max(start, range_start);
  5878. found = found_end - range_start;
  5879. if (found > 0) {
  5880. u64 hole_start = start;
  5881. u64 hole_len = len;
  5882. em = alloc_extent_map();
  5883. if (!em) {
  5884. err = -ENOMEM;
  5885. goto out;
  5886. }
  5887. /*
  5888. * when btrfs_get_extent can't find anything it
  5889. * returns one huge hole
  5890. *
  5891. * make sure what it found really fits our range, and
  5892. * adjust to make sure it is based on the start from
  5893. * the caller
  5894. */
  5895. if (hole_em) {
  5896. u64 calc_end = extent_map_end(hole_em);
  5897. if (calc_end <= start || (hole_em->start > end)) {
  5898. free_extent_map(hole_em);
  5899. hole_em = NULL;
  5900. } else {
  5901. hole_start = max(hole_em->start, start);
  5902. hole_len = calc_end - hole_start;
  5903. }
  5904. }
  5905. em->bdev = NULL;
  5906. if (hole_em && range_start > hole_start) {
  5907. /* our hole starts before our delalloc, so we
  5908. * have to return just the parts of the hole
  5909. * that go until the delalloc starts
  5910. */
  5911. em->len = min(hole_len,
  5912. range_start - hole_start);
  5913. em->start = hole_start;
  5914. em->orig_start = hole_start;
  5915. /*
  5916. * don't adjust block start at all,
  5917. * it is fixed at EXTENT_MAP_HOLE
  5918. */
  5919. em->block_start = hole_em->block_start;
  5920. em->block_len = hole_len;
  5921. if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
  5922. set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
  5923. } else {
  5924. em->start = range_start;
  5925. em->len = found;
  5926. em->orig_start = range_start;
  5927. em->block_start = EXTENT_MAP_DELALLOC;
  5928. em->block_len = found;
  5929. }
  5930. } else if (hole_em) {
  5931. return hole_em;
  5932. }
  5933. out:
  5934. free_extent_map(hole_em);
  5935. if (err) {
  5936. free_extent_map(em);
  5937. return ERR_PTR(err);
  5938. }
  5939. return em;
  5940. }
  5941. static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
  5942. u64 start, u64 len)
  5943. {
  5944. struct btrfs_root *root = BTRFS_I(inode)->root;
  5945. struct extent_map *em;
  5946. struct btrfs_key ins;
  5947. u64 alloc_hint;
  5948. int ret;
  5949. alloc_hint = get_extent_allocation_hint(inode, start, len);
  5950. ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
  5951. alloc_hint, &ins, 1, 1);
  5952. if (ret)
  5953. return ERR_PTR(ret);
  5954. em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
  5955. ins.offset, ins.offset, ins.offset, 0);
  5956. if (IS_ERR(em)) {
  5957. btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
  5958. return em;
  5959. }
  5960. ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
  5961. ins.offset, ins.offset, 0);
  5962. if (ret) {
  5963. btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
  5964. free_extent_map(em);
  5965. return ERR_PTR(ret);
  5966. }
  5967. return em;
  5968. }
  5969. /*
  5970. * returns 1 when the nocow is safe, < 1 on error, 0 if the
  5971. * block must be cow'd
  5972. */
  5973. noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
  5974. u64 *orig_start, u64 *orig_block_len,
  5975. u64 *ram_bytes)
  5976. {
  5977. struct btrfs_trans_handle *trans;
  5978. struct btrfs_path *path;
  5979. int ret;
  5980. struct extent_buffer *leaf;
  5981. struct btrfs_root *root = BTRFS_I(inode)->root;
  5982. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  5983. struct btrfs_file_extent_item *fi;
  5984. struct btrfs_key key;
  5985. u64 disk_bytenr;
  5986. u64 backref_offset;
  5987. u64 extent_end;
  5988. u64 num_bytes;
  5989. int slot;
  5990. int found_type;
  5991. bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
  5992. path = btrfs_alloc_path();
  5993. if (!path)
  5994. return -ENOMEM;
  5995. ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
  5996. offset, 0);
  5997. if (ret < 0)
  5998. goto out;
  5999. slot = path->slots[0];
  6000. if (ret == 1) {
  6001. if (slot == 0) {
  6002. /* can't find the item, must cow */
  6003. ret = 0;
  6004. goto out;
  6005. }
  6006. slot--;
  6007. }
  6008. ret = 0;
  6009. leaf = path->nodes[0];
  6010. btrfs_item_key_to_cpu(leaf, &key, slot);
  6011. if (key.objectid != btrfs_ino(inode) ||
  6012. key.type != BTRFS_EXTENT_DATA_KEY) {
  6013. /* not our file or wrong item type, must cow */
  6014. goto out;
  6015. }
  6016. if (key.offset > offset) {
  6017. /* Wrong offset, must cow */
  6018. goto out;
  6019. }
  6020. fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  6021. found_type = btrfs_file_extent_type(leaf, fi);
  6022. if (found_type != BTRFS_FILE_EXTENT_REG &&
  6023. found_type != BTRFS_FILE_EXTENT_PREALLOC) {
  6024. /* not a regular extent, must cow */
  6025. goto out;
  6026. }
  6027. if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
  6028. goto out;
  6029. extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
  6030. if (extent_end <= offset)
  6031. goto out;
  6032. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  6033. if (disk_bytenr == 0)
  6034. goto out;
  6035. if (btrfs_file_extent_compression(leaf, fi) ||
  6036. btrfs_file_extent_encryption(leaf, fi) ||
  6037. btrfs_file_extent_other_encoding(leaf, fi))
  6038. goto out;
  6039. backref_offset = btrfs_file_extent_offset(leaf, fi);
  6040. if (orig_start) {
  6041. *orig_start = key.offset - backref_offset;
  6042. *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
  6043. *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
  6044. }
  6045. if (btrfs_extent_readonly(root, disk_bytenr))
  6046. goto out;
  6047. num_bytes = min(offset + *len, extent_end) - offset;
  6048. if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  6049. u64 range_end;
  6050. range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
  6051. ret = test_range_bit(io_tree, offset, range_end,
  6052. EXTENT_DELALLOC, 0, NULL);
  6053. if (ret) {
  6054. ret = -EAGAIN;
  6055. goto out;
  6056. }
  6057. }
  6058. btrfs_release_path(path);
  6059. /*
  6060. * look for other files referencing this extent, if we
  6061. * find any we must cow
  6062. */
  6063. trans = btrfs_join_transaction(root);
  6064. if (IS_ERR(trans)) {
  6065. ret = 0;
  6066. goto out;
  6067. }
  6068. ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
  6069. key.offset - backref_offset, disk_bytenr);
  6070. btrfs_end_transaction(trans, root);
  6071. if (ret) {
  6072. ret = 0;
  6073. goto out;
  6074. }
  6075. /*
  6076. * adjust disk_bytenr and num_bytes to cover just the bytes
  6077. * in this extent we are about to write. If there
  6078. * are any csums in that range we have to cow in order
  6079. * to keep the csums correct
  6080. */
  6081. disk_bytenr += backref_offset;
  6082. disk_bytenr += offset - key.offset;
  6083. if (csum_exist_in_range(root, disk_bytenr, num_bytes))
  6084. goto out;
  6085. /*
  6086. * all of the above have passed, it is safe to overwrite this extent
  6087. * without cow
  6088. */
  6089. *len = num_bytes;
  6090. ret = 1;
  6091. out:
  6092. btrfs_free_path(path);
  6093. return ret;
  6094. }
  6095. bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
  6096. {
  6097. struct radix_tree_root *root = &inode->i_mapping->page_tree;
  6098. int found = false;
  6099. void **pagep = NULL;
  6100. struct page *page = NULL;
  6101. int start_idx;
  6102. int end_idx;
  6103. start_idx = start >> PAGE_CACHE_SHIFT;
  6104. /*
  6105. * end is the last byte in the last page. end == start is legal
  6106. */
  6107. end_idx = end >> PAGE_CACHE_SHIFT;
  6108. rcu_read_lock();
  6109. /* Most of the code in this while loop is lifted from
  6110. * find_get_page. It's been modified to begin searching from a
  6111. * page and return just the first page found in that range. If the
  6112. * found idx is less than or equal to the end idx then we know that
  6113. * a page exists. If no pages are found or if those pages are
  6114. * outside of the range then we're fine (yay!) */
  6115. while (page == NULL &&
  6116. radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
  6117. page = radix_tree_deref_slot(pagep);
  6118. if (unlikely(!page))
  6119. break;
  6120. if (radix_tree_exception(page)) {
  6121. if (radix_tree_deref_retry(page)) {
  6122. page = NULL;
  6123. continue;
  6124. }
  6125. /*
  6126. * Otherwise, shmem/tmpfs must be storing a swap entry
  6127. * here as an exceptional entry: so return it without
  6128. * attempting to raise page count.
  6129. */
  6130. page = NULL;
  6131. break; /* TODO: Is this relevant for this use case? */
  6132. }
  6133. if (!page_cache_get_speculative(page)) {
  6134. page = NULL;
  6135. continue;
  6136. }
  6137. /*
  6138. * Has the page moved?
  6139. * This is part of the lockless pagecache protocol. See
  6140. * include/linux/pagemap.h for details.
  6141. */
  6142. if (unlikely(page != *pagep)) {
  6143. page_cache_release(page);
  6144. page = NULL;
  6145. }
  6146. }
  6147. if (page) {
  6148. if (page->index <= end_idx)
  6149. found = true;
  6150. page_cache_release(page);
  6151. }
  6152. rcu_read_unlock();
  6153. return found;
  6154. }
  6155. static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
  6156. struct extent_state **cached_state, int writing)
  6157. {
  6158. struct btrfs_ordered_extent *ordered;
  6159. int ret = 0;
  6160. while (1) {
  6161. lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  6162. 0, cached_state);
  6163. /*
  6164. * We're concerned with the entire range that we're going to be
  6165. * doing DIO to, so we need to make sure theres no ordered
  6166. * extents in this range.
  6167. */
  6168. ordered = btrfs_lookup_ordered_range(inode, lockstart,
  6169. lockend - lockstart + 1);
  6170. /*
  6171. * We need to make sure there are no buffered pages in this
  6172. * range either, we could have raced between the invalidate in
  6173. * generic_file_direct_write and locking the extent. The
  6174. * invalidate needs to happen so that reads after a write do not
  6175. * get stale data.
  6176. */
  6177. if (!ordered &&
  6178. (!writing ||
  6179. !btrfs_page_exists_in_range(inode, lockstart, lockend)))
  6180. break;
  6181. unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  6182. cached_state, GFP_NOFS);
  6183. if (ordered) {
  6184. btrfs_start_ordered_extent(inode, ordered, 1);
  6185. btrfs_put_ordered_extent(ordered);
  6186. } else {
  6187. /* Screw you mmap */
  6188. ret = btrfs_fdatawrite_range(inode, lockstart, lockend);
  6189. if (ret)
  6190. break;
  6191. ret = filemap_fdatawait_range(inode->i_mapping,
  6192. lockstart,
  6193. lockend);
  6194. if (ret)
  6195. break;
  6196. /*
  6197. * If we found a page that couldn't be invalidated just
  6198. * fall back to buffered.
  6199. */
  6200. ret = invalidate_inode_pages2_range(inode->i_mapping,
  6201. lockstart >> PAGE_CACHE_SHIFT,
  6202. lockend >> PAGE_CACHE_SHIFT);
  6203. if (ret)
  6204. break;
  6205. }
  6206. cond_resched();
  6207. }
  6208. return ret;
  6209. }
  6210. static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
  6211. u64 len, u64 orig_start,
  6212. u64 block_start, u64 block_len,
  6213. u64 orig_block_len, u64 ram_bytes,
  6214. int type)
  6215. {
  6216. struct extent_map_tree *em_tree;
  6217. struct extent_map *em;
  6218. struct btrfs_root *root = BTRFS_I(inode)->root;
  6219. int ret;
  6220. em_tree = &BTRFS_I(inode)->extent_tree;
  6221. em = alloc_extent_map();
  6222. if (!em)
  6223. return ERR_PTR(-ENOMEM);
  6224. em->start = start;
  6225. em->orig_start = orig_start;
  6226. em->mod_start = start;
  6227. em->mod_len = len;
  6228. em->len = len;
  6229. em->block_len = block_len;
  6230. em->block_start = block_start;
  6231. em->bdev = root->fs_info->fs_devices->latest_bdev;
  6232. em->orig_block_len = orig_block_len;
  6233. em->ram_bytes = ram_bytes;
  6234. em->generation = -1;
  6235. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  6236. if (type == BTRFS_ORDERED_PREALLOC)
  6237. set_bit(EXTENT_FLAG_FILLING, &em->flags);
  6238. do {
  6239. btrfs_drop_extent_cache(inode, em->start,
  6240. em->start + em->len - 1, 0);
  6241. write_lock(&em_tree->lock);
  6242. ret = add_extent_mapping(em_tree, em, 1);
  6243. write_unlock(&em_tree->lock);
  6244. } while (ret == -EEXIST);
  6245. if (ret) {
  6246. free_extent_map(em);
  6247. return ERR_PTR(ret);
  6248. }
  6249. return em;
  6250. }
  6251. static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
  6252. struct buffer_head *bh_result, int create)
  6253. {
  6254. struct extent_map *em;
  6255. struct btrfs_root *root = BTRFS_I(inode)->root;
  6256. struct extent_state *cached_state = NULL;
  6257. u64 start = iblock << inode->i_blkbits;
  6258. u64 lockstart, lockend;
  6259. u64 len = bh_result->b_size;
  6260. int unlock_bits = EXTENT_LOCKED;
  6261. int ret = 0;
  6262. if (create)
  6263. unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
  6264. else
  6265. len = min_t(u64, len, root->sectorsize);
  6266. lockstart = start;
  6267. lockend = start + len - 1;
  6268. /*
  6269. * If this errors out it's because we couldn't invalidate pagecache for
  6270. * this range and we need to fallback to buffered.
  6271. */
  6272. if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
  6273. return -ENOTBLK;
  6274. em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
  6275. if (IS_ERR(em)) {
  6276. ret = PTR_ERR(em);
  6277. goto unlock_err;
  6278. }
  6279. /*
  6280. * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
  6281. * io. INLINE is special, and we could probably kludge it in here, but
  6282. * it's still buffered so for safety lets just fall back to the generic
  6283. * buffered path.
  6284. *
  6285. * For COMPRESSED we _have_ to read the entire extent in so we can
  6286. * decompress it, so there will be buffering required no matter what we
  6287. * do, so go ahead and fallback to buffered.
  6288. *
  6289. * We return -ENOTBLK because thats what makes DIO go ahead and go back
  6290. * to buffered IO. Don't blame me, this is the price we pay for using
  6291. * the generic code.
  6292. */
  6293. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
  6294. em->block_start == EXTENT_MAP_INLINE) {
  6295. free_extent_map(em);
  6296. ret = -ENOTBLK;
  6297. goto unlock_err;
  6298. }
  6299. /* Just a good old fashioned hole, return */
  6300. if (!create && (em->block_start == EXTENT_MAP_HOLE ||
  6301. test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
  6302. free_extent_map(em);
  6303. goto unlock_err;
  6304. }
  6305. /*
  6306. * We don't allocate a new extent in the following cases
  6307. *
  6308. * 1) The inode is marked as NODATACOW. In this case we'll just use the
  6309. * existing extent.
  6310. * 2) The extent is marked as PREALLOC. We're good to go here and can
  6311. * just use the extent.
  6312. *
  6313. */
  6314. if (!create) {
  6315. len = min(len, em->len - (start - em->start));
  6316. lockstart = start + len;
  6317. goto unlock;
  6318. }
  6319. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
  6320. ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
  6321. em->block_start != EXTENT_MAP_HOLE)) {
  6322. int type;
  6323. int ret;
  6324. u64 block_start, orig_start, orig_block_len, ram_bytes;
  6325. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  6326. type = BTRFS_ORDERED_PREALLOC;
  6327. else
  6328. type = BTRFS_ORDERED_NOCOW;
  6329. len = min(len, em->len - (start - em->start));
  6330. block_start = em->block_start + (start - em->start);
  6331. if (can_nocow_extent(inode, start, &len, &orig_start,
  6332. &orig_block_len, &ram_bytes) == 1) {
  6333. if (type == BTRFS_ORDERED_PREALLOC) {
  6334. free_extent_map(em);
  6335. em = create_pinned_em(inode, start, len,
  6336. orig_start,
  6337. block_start, len,
  6338. orig_block_len,
  6339. ram_bytes, type);
  6340. if (IS_ERR(em)) {
  6341. ret = PTR_ERR(em);
  6342. goto unlock_err;
  6343. }
  6344. }
  6345. ret = btrfs_add_ordered_extent_dio(inode, start,
  6346. block_start, len, len, type);
  6347. if (ret) {
  6348. free_extent_map(em);
  6349. goto unlock_err;
  6350. }
  6351. goto unlock;
  6352. }
  6353. }
  6354. /*
  6355. * this will cow the extent, reset the len in case we changed
  6356. * it above
  6357. */
  6358. len = bh_result->b_size;
  6359. free_extent_map(em);
  6360. em = btrfs_new_extent_direct(inode, start, len);
  6361. if (IS_ERR(em)) {
  6362. ret = PTR_ERR(em);
  6363. goto unlock_err;
  6364. }
  6365. len = min(len, em->len - (start - em->start));
  6366. unlock:
  6367. bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
  6368. inode->i_blkbits;
  6369. bh_result->b_size = len;
  6370. bh_result->b_bdev = em->bdev;
  6371. set_buffer_mapped(bh_result);
  6372. if (create) {
  6373. if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  6374. set_buffer_new(bh_result);
  6375. /*
  6376. * Need to update the i_size under the extent lock so buffered
  6377. * readers will get the updated i_size when we unlock.
  6378. */
  6379. if (start + len > i_size_read(inode))
  6380. i_size_write(inode, start + len);
  6381. spin_lock(&BTRFS_I(inode)->lock);
  6382. BTRFS_I(inode)->outstanding_extents++;
  6383. spin_unlock(&BTRFS_I(inode)->lock);
  6384. ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
  6385. lockstart + len - 1, EXTENT_DELALLOC, NULL,
  6386. &cached_state, GFP_NOFS);
  6387. BUG_ON(ret);
  6388. }
  6389. /*
  6390. * In the case of write we need to clear and unlock the entire range,
  6391. * in the case of read we need to unlock only the end area that we
  6392. * aren't using if there is any left over space.
  6393. */
  6394. if (lockstart < lockend) {
  6395. clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
  6396. lockend, unlock_bits, 1, 0,
  6397. &cached_state, GFP_NOFS);
  6398. } else {
  6399. free_extent_state(cached_state);
  6400. }
  6401. free_extent_map(em);
  6402. return 0;
  6403. unlock_err:
  6404. clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  6405. unlock_bits, 1, 0, &cached_state, GFP_NOFS);
  6406. return ret;
  6407. }
  6408. static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
  6409. int rw, int mirror_num)
  6410. {
  6411. struct btrfs_root *root = BTRFS_I(inode)->root;
  6412. int ret;
  6413. BUG_ON(rw & REQ_WRITE);
  6414. bio_get(bio);
  6415. ret = btrfs_bio_wq_end_io(root->fs_info, bio,
  6416. BTRFS_WQ_ENDIO_DIO_REPAIR);
  6417. if (ret)
  6418. goto err;
  6419. ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
  6420. err:
  6421. bio_put(bio);
  6422. return ret;
  6423. }
  6424. static int btrfs_check_dio_repairable(struct inode *inode,
  6425. struct bio *failed_bio,
  6426. struct io_failure_record *failrec,
  6427. int failed_mirror)
  6428. {
  6429. int num_copies;
  6430. num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
  6431. failrec->logical, failrec->len);
  6432. if (num_copies == 1) {
  6433. /*
  6434. * we only have a single copy of the data, so don't bother with
  6435. * all the retry and error correction code that follows. no
  6436. * matter what the error is, it is very likely to persist.
  6437. */
  6438. pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n",
  6439. num_copies, failrec->this_mirror, failed_mirror);
  6440. return 0;
  6441. }
  6442. failrec->failed_mirror = failed_mirror;
  6443. failrec->this_mirror++;
  6444. if (failrec->this_mirror == failed_mirror)
  6445. failrec->this_mirror++;
  6446. if (failrec->this_mirror > num_copies) {
  6447. pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n",
  6448. num_copies, failrec->this_mirror, failed_mirror);
  6449. return 0;
  6450. }
  6451. return 1;
  6452. }
  6453. static int dio_read_error(struct inode *inode, struct bio *failed_bio,
  6454. struct page *page, u64 start, u64 end,
  6455. int failed_mirror, bio_end_io_t *repair_endio,
  6456. void *repair_arg)
  6457. {
  6458. struct io_failure_record *failrec;
  6459. struct bio *bio;
  6460. int isector;
  6461. int read_mode;
  6462. int ret;
  6463. BUG_ON(failed_bio->bi_rw & REQ_WRITE);
  6464. ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
  6465. if (ret)
  6466. return ret;
  6467. ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
  6468. failed_mirror);
  6469. if (!ret) {
  6470. free_io_failure(inode, failrec);
  6471. return -EIO;
  6472. }
  6473. if (failed_bio->bi_vcnt > 1)
  6474. read_mode = READ_SYNC | REQ_FAILFAST_DEV;
  6475. else
  6476. read_mode = READ_SYNC;
  6477. isector = start - btrfs_io_bio(failed_bio)->logical;
  6478. isector >>= inode->i_sb->s_blocksize_bits;
  6479. bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
  6480. 0, isector, repair_endio, repair_arg);
  6481. if (!bio) {
  6482. free_io_failure(inode, failrec);
  6483. return -EIO;
  6484. }
  6485. btrfs_debug(BTRFS_I(inode)->root->fs_info,
  6486. "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
  6487. read_mode, failrec->this_mirror, failrec->in_validation);
  6488. ret = submit_dio_repair_bio(inode, bio, read_mode,
  6489. failrec->this_mirror);
  6490. if (ret) {
  6491. free_io_failure(inode, failrec);
  6492. bio_put(bio);
  6493. }
  6494. return ret;
  6495. }
  6496. struct btrfs_retry_complete {
  6497. struct completion done;
  6498. struct inode *inode;
  6499. u64 start;
  6500. int uptodate;
  6501. };
  6502. static void btrfs_retry_endio_nocsum(struct bio *bio, int err)
  6503. {
  6504. struct btrfs_retry_complete *done = bio->bi_private;
  6505. struct bio_vec *bvec;
  6506. int i;
  6507. if (err)
  6508. goto end;
  6509. done->uptodate = 1;
  6510. bio_for_each_segment_all(bvec, bio, i)
  6511. clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
  6512. end:
  6513. complete(&done->done);
  6514. bio_put(bio);
  6515. }
  6516. static int __btrfs_correct_data_nocsum(struct inode *inode,
  6517. struct btrfs_io_bio *io_bio)
  6518. {
  6519. struct bio_vec *bvec;
  6520. struct btrfs_retry_complete done;
  6521. u64 start;
  6522. int i;
  6523. int ret;
  6524. start = io_bio->logical;
  6525. done.inode = inode;
  6526. bio_for_each_segment_all(bvec, &io_bio->bio, i) {
  6527. try_again:
  6528. done.uptodate = 0;
  6529. done.start = start;
  6530. init_completion(&done.done);
  6531. ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
  6532. start + bvec->bv_len - 1,
  6533. io_bio->mirror_num,
  6534. btrfs_retry_endio_nocsum, &done);
  6535. if (ret)
  6536. return ret;
  6537. wait_for_completion(&done.done);
  6538. if (!done.uptodate) {
  6539. /* We might have another mirror, so try again */
  6540. goto try_again;
  6541. }
  6542. start += bvec->bv_len;
  6543. }
  6544. return 0;
  6545. }
  6546. static void btrfs_retry_endio(struct bio *bio, int err)
  6547. {
  6548. struct btrfs_retry_complete *done = bio->bi_private;
  6549. struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
  6550. struct bio_vec *bvec;
  6551. int uptodate;
  6552. int ret;
  6553. int i;
  6554. if (err)
  6555. goto end;
  6556. uptodate = 1;
  6557. bio_for_each_segment_all(bvec, bio, i) {
  6558. ret = __readpage_endio_check(done->inode, io_bio, i,
  6559. bvec->bv_page, 0,
  6560. done->start, bvec->bv_len);
  6561. if (!ret)
  6562. clean_io_failure(done->inode, done->start,
  6563. bvec->bv_page, 0);
  6564. else
  6565. uptodate = 0;
  6566. }
  6567. done->uptodate = uptodate;
  6568. end:
  6569. complete(&done->done);
  6570. bio_put(bio);
  6571. }
  6572. static int __btrfs_subio_endio_read(struct inode *inode,
  6573. struct btrfs_io_bio *io_bio, int err)
  6574. {
  6575. struct bio_vec *bvec;
  6576. struct btrfs_retry_complete done;
  6577. u64 start;
  6578. u64 offset = 0;
  6579. int i;
  6580. int ret;
  6581. err = 0;
  6582. start = io_bio->logical;
  6583. done.inode = inode;
  6584. bio_for_each_segment_all(bvec, &io_bio->bio, i) {
  6585. ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
  6586. 0, start, bvec->bv_len);
  6587. if (likely(!ret))
  6588. goto next;
  6589. try_again:
  6590. done.uptodate = 0;
  6591. done.start = start;
  6592. init_completion(&done.done);
  6593. ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
  6594. start + bvec->bv_len - 1,
  6595. io_bio->mirror_num,
  6596. btrfs_retry_endio, &done);
  6597. if (ret) {
  6598. err = ret;
  6599. goto next;
  6600. }
  6601. wait_for_completion(&done.done);
  6602. if (!done.uptodate) {
  6603. /* We might have another mirror, so try again */
  6604. goto try_again;
  6605. }
  6606. next:
  6607. offset += bvec->bv_len;
  6608. start += bvec->bv_len;
  6609. }
  6610. return err;
  6611. }
  6612. static int btrfs_subio_endio_read(struct inode *inode,
  6613. struct btrfs_io_bio *io_bio, int err)
  6614. {
  6615. bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  6616. if (skip_csum) {
  6617. if (unlikely(err))
  6618. return __btrfs_correct_data_nocsum(inode, io_bio);
  6619. else
  6620. return 0;
  6621. } else {
  6622. return __btrfs_subio_endio_read(inode, io_bio, err);
  6623. }
  6624. }
  6625. static void btrfs_endio_direct_read(struct bio *bio, int err)
  6626. {
  6627. struct btrfs_dio_private *dip = bio->bi_private;
  6628. struct inode *inode = dip->inode;
  6629. struct bio *dio_bio;
  6630. struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
  6631. if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
  6632. err = btrfs_subio_endio_read(inode, io_bio, err);
  6633. unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
  6634. dip->logical_offset + dip->bytes - 1);
  6635. dio_bio = dip->dio_bio;
  6636. kfree(dip);
  6637. /* If we had a csum failure make sure to clear the uptodate flag */
  6638. if (err)
  6639. clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
  6640. dio_end_io(dio_bio, err);
  6641. if (io_bio->end_io)
  6642. io_bio->end_io(io_bio, err);
  6643. bio_put(bio);
  6644. }
  6645. static void btrfs_endio_direct_write(struct bio *bio, int err)
  6646. {
  6647. struct btrfs_dio_private *dip = bio->bi_private;
  6648. struct inode *inode = dip->inode;
  6649. struct btrfs_root *root = BTRFS_I(inode)->root;
  6650. struct btrfs_ordered_extent *ordered = NULL;
  6651. u64 ordered_offset = dip->logical_offset;
  6652. u64 ordered_bytes = dip->bytes;
  6653. struct bio *dio_bio;
  6654. int ret;
  6655. if (err)
  6656. goto out_done;
  6657. again:
  6658. ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
  6659. &ordered_offset,
  6660. ordered_bytes, !err);
  6661. if (!ret)
  6662. goto out_test;
  6663. btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
  6664. finish_ordered_fn, NULL, NULL);
  6665. btrfs_queue_work(root->fs_info->endio_write_workers,
  6666. &ordered->work);
  6667. out_test:
  6668. /*
  6669. * our bio might span multiple ordered extents. If we haven't
  6670. * completed the accounting for the whole dio, go back and try again
  6671. */
  6672. if (ordered_offset < dip->logical_offset + dip->bytes) {
  6673. ordered_bytes = dip->logical_offset + dip->bytes -
  6674. ordered_offset;
  6675. ordered = NULL;
  6676. goto again;
  6677. }
  6678. out_done:
  6679. dio_bio = dip->dio_bio;
  6680. kfree(dip);
  6681. /* If we had an error make sure to clear the uptodate flag */
  6682. if (err)
  6683. clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
  6684. dio_end_io(dio_bio, err);
  6685. bio_put(bio);
  6686. }
  6687. static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
  6688. struct bio *bio, int mirror_num,
  6689. unsigned long bio_flags, u64 offset)
  6690. {
  6691. int ret;
  6692. struct btrfs_root *root = BTRFS_I(inode)->root;
  6693. ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
  6694. BUG_ON(ret); /* -ENOMEM */
  6695. return 0;
  6696. }
  6697. static void btrfs_end_dio_bio(struct bio *bio, int err)
  6698. {
  6699. struct btrfs_dio_private *dip = bio->bi_private;
  6700. if (err)
  6701. btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
  6702. "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
  6703. btrfs_ino(dip->inode), bio->bi_rw,
  6704. (unsigned long long)bio->bi_iter.bi_sector,
  6705. bio->bi_iter.bi_size, err);
  6706. if (dip->subio_endio)
  6707. err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
  6708. if (err) {
  6709. dip->errors = 1;
  6710. /*
  6711. * before atomic variable goto zero, we must make sure
  6712. * dip->errors is perceived to be set.
  6713. */
  6714. smp_mb__before_atomic();
  6715. }
  6716. /* if there are more bios still pending for this dio, just exit */
  6717. if (!atomic_dec_and_test(&dip->pending_bios))
  6718. goto out;
  6719. if (dip->errors) {
  6720. bio_io_error(dip->orig_bio);
  6721. } else {
  6722. set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
  6723. bio_endio(dip->orig_bio, 0);
  6724. }
  6725. out:
  6726. bio_put(bio);
  6727. }
  6728. static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
  6729. u64 first_sector, gfp_t gfp_flags)
  6730. {
  6731. int nr_vecs = bio_get_nr_vecs(bdev);
  6732. return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
  6733. }
  6734. static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root,
  6735. struct inode *inode,
  6736. struct btrfs_dio_private *dip,
  6737. struct bio *bio,
  6738. u64 file_offset)
  6739. {
  6740. struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
  6741. struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
  6742. int ret;
  6743. /*
  6744. * We load all the csum data we need when we submit
  6745. * the first bio to reduce the csum tree search and
  6746. * contention.
  6747. */
  6748. if (dip->logical_offset == file_offset) {
  6749. ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio,
  6750. file_offset);
  6751. if (ret)
  6752. return ret;
  6753. }
  6754. if (bio == dip->orig_bio)
  6755. return 0;
  6756. file_offset -= dip->logical_offset;
  6757. file_offset >>= inode->i_sb->s_blocksize_bits;
  6758. io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
  6759. return 0;
  6760. }
  6761. static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
  6762. int rw, u64 file_offset, int skip_sum,
  6763. int async_submit)
  6764. {
  6765. struct btrfs_dio_private *dip = bio->bi_private;
  6766. int write = rw & REQ_WRITE;
  6767. struct btrfs_root *root = BTRFS_I(inode)->root;
  6768. int ret;
  6769. if (async_submit)
  6770. async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
  6771. bio_get(bio);
  6772. if (!write) {
  6773. ret = btrfs_bio_wq_end_io(root->fs_info, bio,
  6774. BTRFS_WQ_ENDIO_DATA);
  6775. if (ret)
  6776. goto err;
  6777. }
  6778. if (skip_sum)
  6779. goto map;
  6780. if (write && async_submit) {
  6781. ret = btrfs_wq_submit_bio(root->fs_info,
  6782. inode, rw, bio, 0, 0,
  6783. file_offset,
  6784. __btrfs_submit_bio_start_direct_io,
  6785. __btrfs_submit_bio_done);
  6786. goto err;
  6787. } else if (write) {
  6788. /*
  6789. * If we aren't doing async submit, calculate the csum of the
  6790. * bio now.
  6791. */
  6792. ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
  6793. if (ret)
  6794. goto err;
  6795. } else {
  6796. ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio,
  6797. file_offset);
  6798. if (ret)
  6799. goto err;
  6800. }
  6801. map:
  6802. ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
  6803. err:
  6804. bio_put(bio);
  6805. return ret;
  6806. }
  6807. static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
  6808. int skip_sum)
  6809. {
  6810. struct inode *inode = dip->inode;
  6811. struct btrfs_root *root = BTRFS_I(inode)->root;
  6812. struct bio *bio;
  6813. struct bio *orig_bio = dip->orig_bio;
  6814. struct bio_vec *bvec = orig_bio->bi_io_vec;
  6815. u64 start_sector = orig_bio->bi_iter.bi_sector;
  6816. u64 file_offset = dip->logical_offset;
  6817. u64 submit_len = 0;
  6818. u64 map_length;
  6819. int nr_pages = 0;
  6820. int ret;
  6821. int async_submit = 0;
  6822. map_length = orig_bio->bi_iter.bi_size;
  6823. ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
  6824. &map_length, NULL, 0);
  6825. if (ret)
  6826. return -EIO;
  6827. if (map_length >= orig_bio->bi_iter.bi_size) {
  6828. bio = orig_bio;
  6829. dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
  6830. goto submit;
  6831. }
  6832. /* async crcs make it difficult to collect full stripe writes. */
  6833. if (btrfs_get_alloc_profile(root, 1) &
  6834. (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
  6835. async_submit = 0;
  6836. else
  6837. async_submit = 1;
  6838. bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
  6839. if (!bio)
  6840. return -ENOMEM;
  6841. bio->bi_private = dip;
  6842. bio->bi_end_io = btrfs_end_dio_bio;
  6843. btrfs_io_bio(bio)->logical = file_offset;
  6844. atomic_inc(&dip->pending_bios);
  6845. while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
  6846. if (map_length < submit_len + bvec->bv_len ||
  6847. bio_add_page(bio, bvec->bv_page, bvec->bv_len,
  6848. bvec->bv_offset) < bvec->bv_len) {
  6849. /*
  6850. * inc the count before we submit the bio so
  6851. * we know the end IO handler won't happen before
  6852. * we inc the count. Otherwise, the dip might get freed
  6853. * before we're done setting it up
  6854. */
  6855. atomic_inc(&dip->pending_bios);
  6856. ret = __btrfs_submit_dio_bio(bio, inode, rw,
  6857. file_offset, skip_sum,
  6858. async_submit);
  6859. if (ret) {
  6860. bio_put(bio);
  6861. atomic_dec(&dip->pending_bios);
  6862. goto out_err;
  6863. }
  6864. start_sector += submit_len >> 9;
  6865. file_offset += submit_len;
  6866. submit_len = 0;
  6867. nr_pages = 0;
  6868. bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
  6869. start_sector, GFP_NOFS);
  6870. if (!bio)
  6871. goto out_err;
  6872. bio->bi_private = dip;
  6873. bio->bi_end_io = btrfs_end_dio_bio;
  6874. btrfs_io_bio(bio)->logical = file_offset;
  6875. map_length = orig_bio->bi_iter.bi_size;
  6876. ret = btrfs_map_block(root->fs_info, rw,
  6877. start_sector << 9,
  6878. &map_length, NULL, 0);
  6879. if (ret) {
  6880. bio_put(bio);
  6881. goto out_err;
  6882. }
  6883. } else {
  6884. submit_len += bvec->bv_len;
  6885. nr_pages++;
  6886. bvec++;
  6887. }
  6888. }
  6889. submit:
  6890. ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
  6891. async_submit);
  6892. if (!ret)
  6893. return 0;
  6894. bio_put(bio);
  6895. out_err:
  6896. dip->errors = 1;
  6897. /*
  6898. * before atomic variable goto zero, we must
  6899. * make sure dip->errors is perceived to be set.
  6900. */
  6901. smp_mb__before_atomic();
  6902. if (atomic_dec_and_test(&dip->pending_bios))
  6903. bio_io_error(dip->orig_bio);
  6904. /* bio_end_io() will handle error, so we needn't return it */
  6905. return 0;
  6906. }
  6907. static void btrfs_submit_direct(int rw, struct bio *dio_bio,
  6908. struct inode *inode, loff_t file_offset)
  6909. {
  6910. struct btrfs_root *root = BTRFS_I(inode)->root;
  6911. struct btrfs_dio_private *dip;
  6912. struct bio *io_bio;
  6913. struct btrfs_io_bio *btrfs_bio;
  6914. int skip_sum;
  6915. int write = rw & REQ_WRITE;
  6916. int ret = 0;
  6917. skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  6918. io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
  6919. if (!io_bio) {
  6920. ret = -ENOMEM;
  6921. goto free_ordered;
  6922. }
  6923. dip = kzalloc(sizeof(*dip), GFP_NOFS);
  6924. if (!dip) {
  6925. ret = -ENOMEM;
  6926. goto free_io_bio;
  6927. }
  6928. dip->private = dio_bio->bi_private;
  6929. dip->inode = inode;
  6930. dip->logical_offset = file_offset;
  6931. dip->bytes = dio_bio->bi_iter.bi_size;
  6932. dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
  6933. io_bio->bi_private = dip;
  6934. dip->orig_bio = io_bio;
  6935. dip->dio_bio = dio_bio;
  6936. atomic_set(&dip->pending_bios, 0);
  6937. btrfs_bio = btrfs_io_bio(io_bio);
  6938. btrfs_bio->logical = file_offset;
  6939. if (write) {
  6940. io_bio->bi_end_io = btrfs_endio_direct_write;
  6941. } else {
  6942. io_bio->bi_end_io = btrfs_endio_direct_read;
  6943. dip->subio_endio = btrfs_subio_endio_read;
  6944. }
  6945. ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
  6946. if (!ret)
  6947. return;
  6948. if (btrfs_bio->end_io)
  6949. btrfs_bio->end_io(btrfs_bio, ret);
  6950. free_io_bio:
  6951. bio_put(io_bio);
  6952. free_ordered:
  6953. /*
  6954. * If this is a write, we need to clean up the reserved space and kill
  6955. * the ordered extent.
  6956. */
  6957. if (write) {
  6958. struct btrfs_ordered_extent *ordered;
  6959. ordered = btrfs_lookup_ordered_extent(inode, file_offset);
  6960. if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
  6961. !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
  6962. btrfs_free_reserved_extent(root, ordered->start,
  6963. ordered->disk_len, 1);
  6964. btrfs_put_ordered_extent(ordered);
  6965. btrfs_put_ordered_extent(ordered);
  6966. }
  6967. bio_endio(dio_bio, ret);
  6968. }
  6969. static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
  6970. const struct iov_iter *iter, loff_t offset)
  6971. {
  6972. int seg;
  6973. int i;
  6974. unsigned blocksize_mask = root->sectorsize - 1;
  6975. ssize_t retval = -EINVAL;
  6976. if (offset & blocksize_mask)
  6977. goto out;
  6978. if (iov_iter_alignment(iter) & blocksize_mask)
  6979. goto out;
  6980. /* If this is a write we don't need to check anymore */
  6981. if (rw & WRITE)
  6982. return 0;
  6983. /*
  6984. * Check to make sure we don't have duplicate iov_base's in this
  6985. * iovec, if so return EINVAL, otherwise we'll get csum errors
  6986. * when reading back.
  6987. */
  6988. for (seg = 0; seg < iter->nr_segs; seg++) {
  6989. for (i = seg + 1; i < iter->nr_segs; i++) {
  6990. if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
  6991. goto out;
  6992. }
  6993. }
  6994. retval = 0;
  6995. out:
  6996. return retval;
  6997. }
  6998. static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
  6999. struct iov_iter *iter, loff_t offset)
  7000. {
  7001. struct file *file = iocb->ki_filp;
  7002. struct inode *inode = file->f_mapping->host;
  7003. size_t count = 0;
  7004. int flags = 0;
  7005. bool wakeup = true;
  7006. bool relock = false;
  7007. ssize_t ret;
  7008. if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iter, offset))
  7009. return 0;
  7010. atomic_inc(&inode->i_dio_count);
  7011. smp_mb__after_atomic();
  7012. /*
  7013. * The generic stuff only does filemap_write_and_wait_range, which
  7014. * isn't enough if we've written compressed pages to this area, so
  7015. * we need to flush the dirty pages again to make absolutely sure
  7016. * that any outstanding dirty pages are on disk.
  7017. */
  7018. count = iov_iter_count(iter);
  7019. if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
  7020. &BTRFS_I(inode)->runtime_flags))
  7021. filemap_fdatawrite_range(inode->i_mapping, offset,
  7022. offset + count - 1);
  7023. if (rw & WRITE) {
  7024. /*
  7025. * If the write DIO is beyond the EOF, we need update
  7026. * the isize, but it is protected by i_mutex. So we can
  7027. * not unlock the i_mutex at this case.
  7028. */
  7029. if (offset + count <= inode->i_size) {
  7030. mutex_unlock(&inode->i_mutex);
  7031. relock = true;
  7032. }
  7033. ret = btrfs_delalloc_reserve_space(inode, count);
  7034. if (ret)
  7035. goto out;
  7036. } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
  7037. &BTRFS_I(inode)->runtime_flags)) {
  7038. inode_dio_done(inode);
  7039. flags = DIO_LOCKING | DIO_SKIP_HOLES;
  7040. wakeup = false;
  7041. }
  7042. ret = __blockdev_direct_IO(rw, iocb, inode,
  7043. BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
  7044. iter, offset, btrfs_get_blocks_direct, NULL,
  7045. btrfs_submit_direct, flags);
  7046. if (rw & WRITE) {
  7047. if (ret < 0 && ret != -EIOCBQUEUED)
  7048. btrfs_delalloc_release_space(inode, count);
  7049. else if (ret >= 0 && (size_t)ret < count)
  7050. btrfs_delalloc_release_space(inode,
  7051. count - (size_t)ret);
  7052. else
  7053. btrfs_delalloc_release_metadata(inode, 0);
  7054. }
  7055. out:
  7056. if (wakeup)
  7057. inode_dio_done(inode);
  7058. if (relock)
  7059. mutex_lock(&inode->i_mutex);
  7060. return ret;
  7061. }
  7062. #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
  7063. static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  7064. __u64 start, __u64 len)
  7065. {
  7066. int ret;
  7067. ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
  7068. if (ret)
  7069. return ret;
  7070. return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
  7071. }
  7072. int btrfs_readpage(struct file *file, struct page *page)
  7073. {
  7074. struct extent_io_tree *tree;
  7075. tree = &BTRFS_I(page->mapping->host)->io_tree;
  7076. return extent_read_full_page(tree, page, btrfs_get_extent, 0);
  7077. }
  7078. static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
  7079. {
  7080. struct extent_io_tree *tree;
  7081. if (current->flags & PF_MEMALLOC) {
  7082. redirty_page_for_writepage(wbc, page);
  7083. unlock_page(page);
  7084. return 0;
  7085. }
  7086. tree = &BTRFS_I(page->mapping->host)->io_tree;
  7087. return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
  7088. }
  7089. static int btrfs_writepages(struct address_space *mapping,
  7090. struct writeback_control *wbc)
  7091. {
  7092. struct extent_io_tree *tree;
  7093. tree = &BTRFS_I(mapping->host)->io_tree;
  7094. return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
  7095. }
  7096. static int
  7097. btrfs_readpages(struct file *file, struct address_space *mapping,
  7098. struct list_head *pages, unsigned nr_pages)
  7099. {
  7100. struct extent_io_tree *tree;
  7101. tree = &BTRFS_I(mapping->host)->io_tree;
  7102. return extent_readpages(tree, mapping, pages, nr_pages,
  7103. btrfs_get_extent);
  7104. }
  7105. static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  7106. {
  7107. struct extent_io_tree *tree;
  7108. struct extent_map_tree *map;
  7109. int ret;
  7110. tree = &BTRFS_I(page->mapping->host)->io_tree;
  7111. map = &BTRFS_I(page->mapping->host)->extent_tree;
  7112. ret = try_release_extent_mapping(map, tree, page, gfp_flags);
  7113. if (ret == 1) {
  7114. ClearPagePrivate(page);
  7115. set_page_private(page, 0);
  7116. page_cache_release(page);
  7117. }
  7118. return ret;
  7119. }
  7120. static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  7121. {
  7122. if (PageWriteback(page) || PageDirty(page))
  7123. return 0;
  7124. return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
  7125. }
  7126. static void btrfs_invalidatepage(struct page *page, unsigned int offset,
  7127. unsigned int length)
  7128. {
  7129. struct inode *inode = page->mapping->host;
  7130. struct extent_io_tree *tree;
  7131. struct btrfs_ordered_extent *ordered;
  7132. struct extent_state *cached_state = NULL;
  7133. u64 page_start = page_offset(page);
  7134. u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
  7135. int inode_evicting = inode->i_state & I_FREEING;
  7136. /*
  7137. * we have the page locked, so new writeback can't start,
  7138. * and the dirty bit won't be cleared while we are here.
  7139. *
  7140. * Wait for IO on this page so that we can safely clear
  7141. * the PagePrivate2 bit and do ordered accounting
  7142. */
  7143. wait_on_page_writeback(page);
  7144. tree = &BTRFS_I(inode)->io_tree;
  7145. if (offset) {
  7146. btrfs_releasepage(page, GFP_NOFS);
  7147. return;
  7148. }
  7149. if (!inode_evicting)
  7150. lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
  7151. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  7152. if (ordered) {
  7153. /*
  7154. * IO on this page will never be started, so we need
  7155. * to account for any ordered extents now
  7156. */
  7157. if (!inode_evicting)
  7158. clear_extent_bit(tree, page_start, page_end,
  7159. EXTENT_DIRTY | EXTENT_DELALLOC |
  7160. EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
  7161. EXTENT_DEFRAG, 1, 0, &cached_state,
  7162. GFP_NOFS);
  7163. /*
  7164. * whoever cleared the private bit is responsible
  7165. * for the finish_ordered_io
  7166. */
  7167. if (TestClearPagePrivate2(page)) {
  7168. struct btrfs_ordered_inode_tree *tree;
  7169. u64 new_len;
  7170. tree = &BTRFS_I(inode)->ordered_tree;
  7171. spin_lock_irq(&tree->lock);
  7172. set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
  7173. new_len = page_start - ordered->file_offset;
  7174. if (new_len < ordered->truncated_len)
  7175. ordered->truncated_len = new_len;
  7176. spin_unlock_irq(&tree->lock);
  7177. if (btrfs_dec_test_ordered_pending(inode, &ordered,
  7178. page_start,
  7179. PAGE_CACHE_SIZE, 1))
  7180. btrfs_finish_ordered_io(ordered);
  7181. }
  7182. btrfs_put_ordered_extent(ordered);
  7183. if (!inode_evicting) {
  7184. cached_state = NULL;
  7185. lock_extent_bits(tree, page_start, page_end, 0,
  7186. &cached_state);
  7187. }
  7188. }
  7189. if (!inode_evicting) {
  7190. clear_extent_bit(tree, page_start, page_end,
  7191. EXTENT_LOCKED | EXTENT_DIRTY |
  7192. EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
  7193. EXTENT_DEFRAG, 1, 1,
  7194. &cached_state, GFP_NOFS);
  7195. __btrfs_releasepage(page, GFP_NOFS);
  7196. }
  7197. ClearPageChecked(page);
  7198. if (PagePrivate(page)) {
  7199. ClearPagePrivate(page);
  7200. set_page_private(page, 0);
  7201. page_cache_release(page);
  7202. }
  7203. }
  7204. /*
  7205. * btrfs_page_mkwrite() is not allowed to change the file size as it gets
  7206. * called from a page fault handler when a page is first dirtied. Hence we must
  7207. * be careful to check for EOF conditions here. We set the page up correctly
  7208. * for a written page which means we get ENOSPC checking when writing into
  7209. * holes and correct delalloc and unwritten extent mapping on filesystems that
  7210. * support these features.
  7211. *
  7212. * We are not allowed to take the i_mutex here so we have to play games to
  7213. * protect against truncate races as the page could now be beyond EOF. Because
  7214. * vmtruncate() writes the inode size before removing pages, once we have the
  7215. * page lock we can determine safely if the page is beyond EOF. If it is not
  7216. * beyond EOF, then the page is guaranteed safe against truncation until we
  7217. * unlock the page.
  7218. */
  7219. int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  7220. {
  7221. struct page *page = vmf->page;
  7222. struct inode *inode = file_inode(vma->vm_file);
  7223. struct btrfs_root *root = BTRFS_I(inode)->root;
  7224. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  7225. struct btrfs_ordered_extent *ordered;
  7226. struct extent_state *cached_state = NULL;
  7227. char *kaddr;
  7228. unsigned long zero_start;
  7229. loff_t size;
  7230. int ret;
  7231. int reserved = 0;
  7232. u64 page_start;
  7233. u64 page_end;
  7234. sb_start_pagefault(inode->i_sb);
  7235. ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
  7236. if (!ret) {
  7237. ret = file_update_time(vma->vm_file);
  7238. reserved = 1;
  7239. }
  7240. if (ret) {
  7241. if (ret == -ENOMEM)
  7242. ret = VM_FAULT_OOM;
  7243. else /* -ENOSPC, -EIO, etc */
  7244. ret = VM_FAULT_SIGBUS;
  7245. if (reserved)
  7246. goto out;
  7247. goto out_noreserve;
  7248. }
  7249. ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
  7250. again:
  7251. lock_page(page);
  7252. size = i_size_read(inode);
  7253. page_start = page_offset(page);
  7254. page_end = page_start + PAGE_CACHE_SIZE - 1;
  7255. if ((page->mapping != inode->i_mapping) ||
  7256. (page_start >= size)) {
  7257. /* page got truncated out from underneath us */
  7258. goto out_unlock;
  7259. }
  7260. wait_on_page_writeback(page);
  7261. lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
  7262. set_page_extent_mapped(page);
  7263. /*
  7264. * we can't set the delalloc bits if there are pending ordered
  7265. * extents. Drop our locks and wait for them to finish
  7266. */
  7267. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  7268. if (ordered) {
  7269. unlock_extent_cached(io_tree, page_start, page_end,
  7270. &cached_state, GFP_NOFS);
  7271. unlock_page(page);
  7272. btrfs_start_ordered_extent(inode, ordered, 1);
  7273. btrfs_put_ordered_extent(ordered);
  7274. goto again;
  7275. }
  7276. /*
  7277. * XXX - page_mkwrite gets called every time the page is dirtied, even
  7278. * if it was already dirty, so for space accounting reasons we need to
  7279. * clear any delalloc bits for the range we are fixing to save. There
  7280. * is probably a better way to do this, but for now keep consistent with
  7281. * prepare_pages in the normal write path.
  7282. */
  7283. clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
  7284. EXTENT_DIRTY | EXTENT_DELALLOC |
  7285. EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
  7286. 0, 0, &cached_state, GFP_NOFS);
  7287. ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
  7288. &cached_state);
  7289. if (ret) {
  7290. unlock_extent_cached(io_tree, page_start, page_end,
  7291. &cached_state, GFP_NOFS);
  7292. ret = VM_FAULT_SIGBUS;
  7293. goto out_unlock;
  7294. }
  7295. ret = 0;
  7296. /* page is wholly or partially inside EOF */
  7297. if (page_start + PAGE_CACHE_SIZE > size)
  7298. zero_start = size & ~PAGE_CACHE_MASK;
  7299. else
  7300. zero_start = PAGE_CACHE_SIZE;
  7301. if (zero_start != PAGE_CACHE_SIZE) {
  7302. kaddr = kmap(page);
  7303. memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
  7304. flush_dcache_page(page);
  7305. kunmap(page);
  7306. }
  7307. ClearPageChecked(page);
  7308. set_page_dirty(page);
  7309. SetPageUptodate(page);
  7310. BTRFS_I(inode)->last_trans = root->fs_info->generation;
  7311. BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
  7312. BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
  7313. unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
  7314. out_unlock:
  7315. if (!ret) {
  7316. sb_end_pagefault(inode->i_sb);
  7317. return VM_FAULT_LOCKED;
  7318. }
  7319. unlock_page(page);
  7320. out:
  7321. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  7322. out_noreserve:
  7323. sb_end_pagefault(inode->i_sb);
  7324. return ret;
  7325. }
  7326. static int btrfs_truncate(struct inode *inode)
  7327. {
  7328. struct btrfs_root *root = BTRFS_I(inode)->root;
  7329. struct btrfs_block_rsv *rsv;
  7330. int ret = 0;
  7331. int err = 0;
  7332. struct btrfs_trans_handle *trans;
  7333. u64 mask = root->sectorsize - 1;
  7334. u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
  7335. ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
  7336. (u64)-1);
  7337. if (ret)
  7338. return ret;
  7339. /*
  7340. * Yes ladies and gentelment, this is indeed ugly. The fact is we have
  7341. * 3 things going on here
  7342. *
  7343. * 1) We need to reserve space for our orphan item and the space to
  7344. * delete our orphan item. Lord knows we don't want to have a dangling
  7345. * orphan item because we didn't reserve space to remove it.
  7346. *
  7347. * 2) We need to reserve space to update our inode.
  7348. *
  7349. * 3) We need to have something to cache all the space that is going to
  7350. * be free'd up by the truncate operation, but also have some slack
  7351. * space reserved in case it uses space during the truncate (thank you
  7352. * very much snapshotting).
  7353. *
  7354. * And we need these to all be seperate. The fact is we can use alot of
  7355. * space doing the truncate, and we have no earthly idea how much space
  7356. * we will use, so we need the truncate reservation to be seperate so it
  7357. * doesn't end up using space reserved for updating the inode or
  7358. * removing the orphan item. We also need to be able to stop the
  7359. * transaction and start a new one, which means we need to be able to
  7360. * update the inode several times, and we have no idea of knowing how
  7361. * many times that will be, so we can't just reserve 1 item for the
  7362. * entirety of the opration, so that has to be done seperately as well.
  7363. * Then there is the orphan item, which does indeed need to be held on
  7364. * to for the whole operation, and we need nobody to touch this reserved
  7365. * space except the orphan code.
  7366. *
  7367. * So that leaves us with
  7368. *
  7369. * 1) root->orphan_block_rsv - for the orphan deletion.
  7370. * 2) rsv - for the truncate reservation, which we will steal from the
  7371. * transaction reservation.
  7372. * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
  7373. * updating the inode.
  7374. */
  7375. rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
  7376. if (!rsv)
  7377. return -ENOMEM;
  7378. rsv->size = min_size;
  7379. rsv->failfast = 1;
  7380. /*
  7381. * 1 for the truncate slack space
  7382. * 1 for updating the inode.
  7383. */
  7384. trans = btrfs_start_transaction(root, 2);
  7385. if (IS_ERR(trans)) {
  7386. err = PTR_ERR(trans);
  7387. goto out;
  7388. }
  7389. /* Migrate the slack space for the truncate to our reserve */
  7390. ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
  7391. min_size);
  7392. BUG_ON(ret);
  7393. /*
  7394. * So if we truncate and then write and fsync we normally would just
  7395. * write the extents that changed, which is a problem if we need to
  7396. * first truncate that entire inode. So set this flag so we write out
  7397. * all of the extents in the inode to the sync log so we're completely
  7398. * safe.
  7399. */
  7400. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
  7401. trans->block_rsv = rsv;
  7402. while (1) {
  7403. ret = btrfs_truncate_inode_items(trans, root, inode,
  7404. inode->i_size,
  7405. BTRFS_EXTENT_DATA_KEY);
  7406. if (ret != -ENOSPC) {
  7407. err = ret;
  7408. break;
  7409. }
  7410. trans->block_rsv = &root->fs_info->trans_block_rsv;
  7411. ret = btrfs_update_inode(trans, root, inode);
  7412. if (ret) {
  7413. err = ret;
  7414. break;
  7415. }
  7416. btrfs_end_transaction(trans, root);
  7417. btrfs_btree_balance_dirty(root);
  7418. trans = btrfs_start_transaction(root, 2);
  7419. if (IS_ERR(trans)) {
  7420. ret = err = PTR_ERR(trans);
  7421. trans = NULL;
  7422. break;
  7423. }
  7424. ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
  7425. rsv, min_size);
  7426. BUG_ON(ret); /* shouldn't happen */
  7427. trans->block_rsv = rsv;
  7428. }
  7429. if (ret == 0 && inode->i_nlink > 0) {
  7430. trans->block_rsv = root->orphan_block_rsv;
  7431. ret = btrfs_orphan_del(trans, inode);
  7432. if (ret)
  7433. err = ret;
  7434. }
  7435. if (trans) {
  7436. trans->block_rsv = &root->fs_info->trans_block_rsv;
  7437. ret = btrfs_update_inode(trans, root, inode);
  7438. if (ret && !err)
  7439. err = ret;
  7440. ret = btrfs_end_transaction(trans, root);
  7441. btrfs_btree_balance_dirty(root);
  7442. }
  7443. out:
  7444. btrfs_free_block_rsv(root, rsv);
  7445. if (ret && !err)
  7446. err = ret;
  7447. return err;
  7448. }
  7449. /*
  7450. * create a new subvolume directory/inode (helper for the ioctl).
  7451. */
  7452. int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
  7453. struct btrfs_root *new_root,
  7454. struct btrfs_root *parent_root,
  7455. u64 new_dirid)
  7456. {
  7457. struct inode *inode;
  7458. int err;
  7459. u64 index = 0;
  7460. inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
  7461. new_dirid, new_dirid,
  7462. S_IFDIR | (~current_umask() & S_IRWXUGO),
  7463. &index);
  7464. if (IS_ERR(inode))
  7465. return PTR_ERR(inode);
  7466. inode->i_op = &btrfs_dir_inode_operations;
  7467. inode->i_fop = &btrfs_dir_file_operations;
  7468. set_nlink(inode, 1);
  7469. btrfs_i_size_write(inode, 0);
  7470. unlock_new_inode(inode);
  7471. err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
  7472. if (err)
  7473. btrfs_err(new_root->fs_info,
  7474. "error inheriting subvolume %llu properties: %d",
  7475. new_root->root_key.objectid, err);
  7476. err = btrfs_update_inode(trans, new_root, inode);
  7477. iput(inode);
  7478. return err;
  7479. }
  7480. struct inode *btrfs_alloc_inode(struct super_block *sb)
  7481. {
  7482. struct btrfs_inode *ei;
  7483. struct inode *inode;
  7484. ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
  7485. if (!ei)
  7486. return NULL;
  7487. ei->root = NULL;
  7488. ei->generation = 0;
  7489. ei->last_trans = 0;
  7490. ei->last_sub_trans = 0;
  7491. ei->logged_trans = 0;
  7492. ei->delalloc_bytes = 0;
  7493. ei->defrag_bytes = 0;
  7494. ei->disk_i_size = 0;
  7495. ei->flags = 0;
  7496. ei->csum_bytes = 0;
  7497. ei->index_cnt = (u64)-1;
  7498. ei->dir_index = 0;
  7499. ei->last_unlink_trans = 0;
  7500. ei->last_log_commit = 0;
  7501. spin_lock_init(&ei->lock);
  7502. ei->outstanding_extents = 0;
  7503. ei->reserved_extents = 0;
  7504. ei->runtime_flags = 0;
  7505. ei->force_compress = BTRFS_COMPRESS_NONE;
  7506. ei->delayed_node = NULL;
  7507. inode = &ei->vfs_inode;
  7508. extent_map_tree_init(&ei->extent_tree);
  7509. extent_io_tree_init(&ei->io_tree, &inode->i_data);
  7510. extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
  7511. ei->io_tree.track_uptodate = 1;
  7512. ei->io_failure_tree.track_uptodate = 1;
  7513. atomic_set(&ei->sync_writers, 0);
  7514. mutex_init(&ei->log_mutex);
  7515. mutex_init(&ei->delalloc_mutex);
  7516. btrfs_ordered_inode_tree_init(&ei->ordered_tree);
  7517. INIT_LIST_HEAD(&ei->delalloc_inodes);
  7518. RB_CLEAR_NODE(&ei->rb_node);
  7519. return inode;
  7520. }
  7521. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  7522. void btrfs_test_destroy_inode(struct inode *inode)
  7523. {
  7524. btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
  7525. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  7526. }
  7527. #endif
  7528. static void btrfs_i_callback(struct rcu_head *head)
  7529. {
  7530. struct inode *inode = container_of(head, struct inode, i_rcu);
  7531. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  7532. }
  7533. void btrfs_destroy_inode(struct inode *inode)
  7534. {
  7535. struct btrfs_ordered_extent *ordered;
  7536. struct btrfs_root *root = BTRFS_I(inode)->root;
  7537. WARN_ON(!hlist_empty(&inode->i_dentry));
  7538. WARN_ON(inode->i_data.nrpages);
  7539. WARN_ON(BTRFS_I(inode)->outstanding_extents);
  7540. WARN_ON(BTRFS_I(inode)->reserved_extents);
  7541. WARN_ON(BTRFS_I(inode)->delalloc_bytes);
  7542. WARN_ON(BTRFS_I(inode)->csum_bytes);
  7543. WARN_ON(BTRFS_I(inode)->defrag_bytes);
  7544. /*
  7545. * This can happen where we create an inode, but somebody else also
  7546. * created the same inode and we need to destroy the one we already
  7547. * created.
  7548. */
  7549. if (!root)
  7550. goto free;
  7551. if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
  7552. &BTRFS_I(inode)->runtime_flags)) {
  7553. btrfs_info(root->fs_info, "inode %llu still on the orphan list",
  7554. btrfs_ino(inode));
  7555. atomic_dec(&root->orphan_inodes);
  7556. }
  7557. while (1) {
  7558. ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
  7559. if (!ordered)
  7560. break;
  7561. else {
  7562. btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
  7563. ordered->file_offset, ordered->len);
  7564. btrfs_remove_ordered_extent(inode, ordered);
  7565. btrfs_put_ordered_extent(ordered);
  7566. btrfs_put_ordered_extent(ordered);
  7567. }
  7568. }
  7569. inode_tree_del(inode);
  7570. btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
  7571. free:
  7572. call_rcu(&inode->i_rcu, btrfs_i_callback);
  7573. }
  7574. int btrfs_drop_inode(struct inode *inode)
  7575. {
  7576. struct btrfs_root *root = BTRFS_I(inode)->root;
  7577. if (root == NULL)
  7578. return 1;
  7579. /* the snap/subvol tree is on deleting */
  7580. if (btrfs_root_refs(&root->root_item) == 0)
  7581. return 1;
  7582. else
  7583. return generic_drop_inode(inode);
  7584. }
  7585. static void init_once(void *foo)
  7586. {
  7587. struct btrfs_inode *ei = (struct btrfs_inode *) foo;
  7588. inode_init_once(&ei->vfs_inode);
  7589. }
  7590. void btrfs_destroy_cachep(void)
  7591. {
  7592. /*
  7593. * Make sure all delayed rcu free inodes are flushed before we
  7594. * destroy cache.
  7595. */
  7596. rcu_barrier();
  7597. if (btrfs_inode_cachep)
  7598. kmem_cache_destroy(btrfs_inode_cachep);
  7599. if (btrfs_trans_handle_cachep)
  7600. kmem_cache_destroy(btrfs_trans_handle_cachep);
  7601. if (btrfs_transaction_cachep)
  7602. kmem_cache_destroy(btrfs_transaction_cachep);
  7603. if (btrfs_path_cachep)
  7604. kmem_cache_destroy(btrfs_path_cachep);
  7605. if (btrfs_free_space_cachep)
  7606. kmem_cache_destroy(btrfs_free_space_cachep);
  7607. if (btrfs_delalloc_work_cachep)
  7608. kmem_cache_destroy(btrfs_delalloc_work_cachep);
  7609. }
  7610. int btrfs_init_cachep(void)
  7611. {
  7612. btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
  7613. sizeof(struct btrfs_inode), 0,
  7614. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
  7615. if (!btrfs_inode_cachep)
  7616. goto fail;
  7617. btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
  7618. sizeof(struct btrfs_trans_handle), 0,
  7619. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  7620. if (!btrfs_trans_handle_cachep)
  7621. goto fail;
  7622. btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
  7623. sizeof(struct btrfs_transaction), 0,
  7624. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  7625. if (!btrfs_transaction_cachep)
  7626. goto fail;
  7627. btrfs_path_cachep = kmem_cache_create("btrfs_path",
  7628. sizeof(struct btrfs_path), 0,
  7629. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  7630. if (!btrfs_path_cachep)
  7631. goto fail;
  7632. btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
  7633. sizeof(struct btrfs_free_space), 0,
  7634. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  7635. if (!btrfs_free_space_cachep)
  7636. goto fail;
  7637. btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
  7638. sizeof(struct btrfs_delalloc_work), 0,
  7639. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
  7640. NULL);
  7641. if (!btrfs_delalloc_work_cachep)
  7642. goto fail;
  7643. return 0;
  7644. fail:
  7645. btrfs_destroy_cachep();
  7646. return -ENOMEM;
  7647. }
  7648. static int btrfs_getattr(struct vfsmount *mnt,
  7649. struct dentry *dentry, struct kstat *stat)
  7650. {
  7651. u64 delalloc_bytes;
  7652. struct inode *inode = dentry->d_inode;
  7653. u32 blocksize = inode->i_sb->s_blocksize;
  7654. generic_fillattr(inode, stat);
  7655. stat->dev = BTRFS_I(inode)->root->anon_dev;
  7656. stat->blksize = PAGE_CACHE_SIZE;
  7657. spin_lock(&BTRFS_I(inode)->lock);
  7658. delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
  7659. spin_unlock(&BTRFS_I(inode)->lock);
  7660. stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
  7661. ALIGN(delalloc_bytes, blocksize)) >> 9;
  7662. return 0;
  7663. }
  7664. static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  7665. struct inode *new_dir, struct dentry *new_dentry)
  7666. {
  7667. struct btrfs_trans_handle *trans;
  7668. struct btrfs_root *root = BTRFS_I(old_dir)->root;
  7669. struct btrfs_root *dest = BTRFS_I(new_dir)->root;
  7670. struct inode *new_inode = new_dentry->d_inode;
  7671. struct inode *old_inode = old_dentry->d_inode;
  7672. struct timespec ctime = CURRENT_TIME;
  7673. u64 index = 0;
  7674. u64 root_objectid;
  7675. int ret;
  7676. u64 old_ino = btrfs_ino(old_inode);
  7677. if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
  7678. return -EPERM;
  7679. /* we only allow rename subvolume link between subvolumes */
  7680. if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
  7681. return -EXDEV;
  7682. if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
  7683. (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
  7684. return -ENOTEMPTY;
  7685. if (S_ISDIR(old_inode->i_mode) && new_inode &&
  7686. new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
  7687. return -ENOTEMPTY;
  7688. /* check for collisions, even if the name isn't there */
  7689. ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
  7690. new_dentry->d_name.name,
  7691. new_dentry->d_name.len);
  7692. if (ret) {
  7693. if (ret == -EEXIST) {
  7694. /* we shouldn't get
  7695. * eexist without a new_inode */
  7696. if (WARN_ON(!new_inode)) {
  7697. return ret;
  7698. }
  7699. } else {
  7700. /* maybe -EOVERFLOW */
  7701. return ret;
  7702. }
  7703. }
  7704. ret = 0;
  7705. /*
  7706. * we're using rename to replace one file with another. Start IO on it
  7707. * now so we don't add too much work to the end of the transaction
  7708. */
  7709. if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
  7710. filemap_flush(old_inode->i_mapping);
  7711. /* close the racy window with snapshot create/destroy ioctl */
  7712. if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
  7713. down_read(&root->fs_info->subvol_sem);
  7714. /*
  7715. * We want to reserve the absolute worst case amount of items. So if
  7716. * both inodes are subvols and we need to unlink them then that would
  7717. * require 4 item modifications, but if they are both normal inodes it
  7718. * would require 5 item modifications, so we'll assume their normal
  7719. * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
  7720. * should cover the worst case number of items we'll modify.
  7721. */
  7722. trans = btrfs_start_transaction(root, 11);
  7723. if (IS_ERR(trans)) {
  7724. ret = PTR_ERR(trans);
  7725. goto out_notrans;
  7726. }
  7727. if (dest != root)
  7728. btrfs_record_root_in_trans(trans, dest);
  7729. ret = btrfs_set_inode_index(new_dir, &index);
  7730. if (ret)
  7731. goto out_fail;
  7732. BTRFS_I(old_inode)->dir_index = 0ULL;
  7733. if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  7734. /* force full log commit if subvolume involved. */
  7735. btrfs_set_log_full_commit(root->fs_info, trans);
  7736. } else {
  7737. ret = btrfs_insert_inode_ref(trans, dest,
  7738. new_dentry->d_name.name,
  7739. new_dentry->d_name.len,
  7740. old_ino,
  7741. btrfs_ino(new_dir), index);
  7742. if (ret)
  7743. goto out_fail;
  7744. /*
  7745. * this is an ugly little race, but the rename is required
  7746. * to make sure that if we crash, the inode is either at the
  7747. * old name or the new one. pinning the log transaction lets
  7748. * us make sure we don't allow a log commit to come in after
  7749. * we unlink the name but before we add the new name back in.
  7750. */
  7751. btrfs_pin_log_trans(root);
  7752. }
  7753. inode_inc_iversion(old_dir);
  7754. inode_inc_iversion(new_dir);
  7755. inode_inc_iversion(old_inode);
  7756. old_dir->i_ctime = old_dir->i_mtime = ctime;
  7757. new_dir->i_ctime = new_dir->i_mtime = ctime;
  7758. old_inode->i_ctime = ctime;
  7759. if (old_dentry->d_parent != new_dentry->d_parent)
  7760. btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
  7761. if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  7762. root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
  7763. ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
  7764. old_dentry->d_name.name,
  7765. old_dentry->d_name.len);
  7766. } else {
  7767. ret = __btrfs_unlink_inode(trans, root, old_dir,
  7768. old_dentry->d_inode,
  7769. old_dentry->d_name.name,
  7770. old_dentry->d_name.len);
  7771. if (!ret)
  7772. ret = btrfs_update_inode(trans, root, old_inode);
  7773. }
  7774. if (ret) {
  7775. btrfs_abort_transaction(trans, root, ret);
  7776. goto out_fail;
  7777. }
  7778. if (new_inode) {
  7779. inode_inc_iversion(new_inode);
  7780. new_inode->i_ctime = CURRENT_TIME;
  7781. if (unlikely(btrfs_ino(new_inode) ==
  7782. BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
  7783. root_objectid = BTRFS_I(new_inode)->location.objectid;
  7784. ret = btrfs_unlink_subvol(trans, dest, new_dir,
  7785. root_objectid,
  7786. new_dentry->d_name.name,
  7787. new_dentry->d_name.len);
  7788. BUG_ON(new_inode->i_nlink == 0);
  7789. } else {
  7790. ret = btrfs_unlink_inode(trans, dest, new_dir,
  7791. new_dentry->d_inode,
  7792. new_dentry->d_name.name,
  7793. new_dentry->d_name.len);
  7794. }
  7795. if (!ret && new_inode->i_nlink == 0)
  7796. ret = btrfs_orphan_add(trans, new_dentry->d_inode);
  7797. if (ret) {
  7798. btrfs_abort_transaction(trans, root, ret);
  7799. goto out_fail;
  7800. }
  7801. }
  7802. ret = btrfs_add_link(trans, new_dir, old_inode,
  7803. new_dentry->d_name.name,
  7804. new_dentry->d_name.len, 0, index);
  7805. if (ret) {
  7806. btrfs_abort_transaction(trans, root, ret);
  7807. goto out_fail;
  7808. }
  7809. if (old_inode->i_nlink == 1)
  7810. BTRFS_I(old_inode)->dir_index = index;
  7811. if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
  7812. struct dentry *parent = new_dentry->d_parent;
  7813. btrfs_log_new_name(trans, old_inode, old_dir, parent);
  7814. btrfs_end_log_trans(root);
  7815. }
  7816. out_fail:
  7817. btrfs_end_transaction(trans, root);
  7818. out_notrans:
  7819. if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
  7820. up_read(&root->fs_info->subvol_sem);
  7821. return ret;
  7822. }
  7823. static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
  7824. struct inode *new_dir, struct dentry *new_dentry,
  7825. unsigned int flags)
  7826. {
  7827. if (flags & ~RENAME_NOREPLACE)
  7828. return -EINVAL;
  7829. return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry);
  7830. }
  7831. static void btrfs_run_delalloc_work(struct btrfs_work *work)
  7832. {
  7833. struct btrfs_delalloc_work *delalloc_work;
  7834. struct inode *inode;
  7835. delalloc_work = container_of(work, struct btrfs_delalloc_work,
  7836. work);
  7837. inode = delalloc_work->inode;
  7838. if (delalloc_work->wait) {
  7839. btrfs_wait_ordered_range(inode, 0, (u64)-1);
  7840. } else {
  7841. filemap_flush(inode->i_mapping);
  7842. if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
  7843. &BTRFS_I(inode)->runtime_flags))
  7844. filemap_flush(inode->i_mapping);
  7845. }
  7846. if (delalloc_work->delay_iput)
  7847. btrfs_add_delayed_iput(inode);
  7848. else
  7849. iput(inode);
  7850. complete(&delalloc_work->completion);
  7851. }
  7852. struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
  7853. int wait, int delay_iput)
  7854. {
  7855. struct btrfs_delalloc_work *work;
  7856. work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
  7857. if (!work)
  7858. return NULL;
  7859. init_completion(&work->completion);
  7860. INIT_LIST_HEAD(&work->list);
  7861. work->inode = inode;
  7862. work->wait = wait;
  7863. work->delay_iput = delay_iput;
  7864. WARN_ON_ONCE(!inode);
  7865. btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
  7866. btrfs_run_delalloc_work, NULL, NULL);
  7867. return work;
  7868. }
  7869. void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
  7870. {
  7871. wait_for_completion(&work->completion);
  7872. kmem_cache_free(btrfs_delalloc_work_cachep, work);
  7873. }
  7874. /*
  7875. * some fairly slow code that needs optimization. This walks the list
  7876. * of all the inodes with pending delalloc and forces them to disk.
  7877. */
  7878. static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
  7879. int nr)
  7880. {
  7881. struct btrfs_inode *binode;
  7882. struct inode *inode;
  7883. struct btrfs_delalloc_work *work, *next;
  7884. struct list_head works;
  7885. struct list_head splice;
  7886. int ret = 0;
  7887. INIT_LIST_HEAD(&works);
  7888. INIT_LIST_HEAD(&splice);
  7889. mutex_lock(&root->delalloc_mutex);
  7890. spin_lock(&root->delalloc_lock);
  7891. list_splice_init(&root->delalloc_inodes, &splice);
  7892. while (!list_empty(&splice)) {
  7893. binode = list_entry(splice.next, struct btrfs_inode,
  7894. delalloc_inodes);
  7895. list_move_tail(&binode->delalloc_inodes,
  7896. &root->delalloc_inodes);
  7897. inode = igrab(&binode->vfs_inode);
  7898. if (!inode) {
  7899. cond_resched_lock(&root->delalloc_lock);
  7900. continue;
  7901. }
  7902. spin_unlock(&root->delalloc_lock);
  7903. work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
  7904. if (!work) {
  7905. if (delay_iput)
  7906. btrfs_add_delayed_iput(inode);
  7907. else
  7908. iput(inode);
  7909. ret = -ENOMEM;
  7910. goto out;
  7911. }
  7912. list_add_tail(&work->list, &works);
  7913. btrfs_queue_work(root->fs_info->flush_workers,
  7914. &work->work);
  7915. ret++;
  7916. if (nr != -1 && ret >= nr)
  7917. goto out;
  7918. cond_resched();
  7919. spin_lock(&root->delalloc_lock);
  7920. }
  7921. spin_unlock(&root->delalloc_lock);
  7922. out:
  7923. list_for_each_entry_safe(work, next, &works, list) {
  7924. list_del_init(&work->list);
  7925. btrfs_wait_and_free_delalloc_work(work);
  7926. }
  7927. if (!list_empty_careful(&splice)) {
  7928. spin_lock(&root->delalloc_lock);
  7929. list_splice_tail(&splice, &root->delalloc_inodes);
  7930. spin_unlock(&root->delalloc_lock);
  7931. }
  7932. mutex_unlock(&root->delalloc_mutex);
  7933. return ret;
  7934. }
  7935. int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
  7936. {
  7937. int ret;
  7938. if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
  7939. return -EROFS;
  7940. ret = __start_delalloc_inodes(root, delay_iput, -1);
  7941. if (ret > 0)
  7942. ret = 0;
  7943. /*
  7944. * the filemap_flush will queue IO into the worker threads, but
  7945. * we have to make sure the IO is actually started and that
  7946. * ordered extents get created before we return
  7947. */
  7948. atomic_inc(&root->fs_info->async_submit_draining);
  7949. while (atomic_read(&root->fs_info->nr_async_submits) ||
  7950. atomic_read(&root->fs_info->async_delalloc_pages)) {
  7951. wait_event(root->fs_info->async_submit_wait,
  7952. (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
  7953. atomic_read(&root->fs_info->async_delalloc_pages) == 0));
  7954. }
  7955. atomic_dec(&root->fs_info->async_submit_draining);
  7956. return ret;
  7957. }
  7958. int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
  7959. int nr)
  7960. {
  7961. struct btrfs_root *root;
  7962. struct list_head splice;
  7963. int ret;
  7964. if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
  7965. return -EROFS;
  7966. INIT_LIST_HEAD(&splice);
  7967. mutex_lock(&fs_info->delalloc_root_mutex);
  7968. spin_lock(&fs_info->delalloc_root_lock);
  7969. list_splice_init(&fs_info->delalloc_roots, &splice);
  7970. while (!list_empty(&splice) && nr) {
  7971. root = list_first_entry(&splice, struct btrfs_root,
  7972. delalloc_root);
  7973. root = btrfs_grab_fs_root(root);
  7974. BUG_ON(!root);
  7975. list_move_tail(&root->delalloc_root,
  7976. &fs_info->delalloc_roots);
  7977. spin_unlock(&fs_info->delalloc_root_lock);
  7978. ret = __start_delalloc_inodes(root, delay_iput, nr);
  7979. btrfs_put_fs_root(root);
  7980. if (ret < 0)
  7981. goto out;
  7982. if (nr != -1) {
  7983. nr -= ret;
  7984. WARN_ON(nr < 0);
  7985. }
  7986. spin_lock(&fs_info->delalloc_root_lock);
  7987. }
  7988. spin_unlock(&fs_info->delalloc_root_lock);
  7989. ret = 0;
  7990. atomic_inc(&fs_info->async_submit_draining);
  7991. while (atomic_read(&fs_info->nr_async_submits) ||
  7992. atomic_read(&fs_info->async_delalloc_pages)) {
  7993. wait_event(fs_info->async_submit_wait,
  7994. (atomic_read(&fs_info->nr_async_submits) == 0 &&
  7995. atomic_read(&fs_info->async_delalloc_pages) == 0));
  7996. }
  7997. atomic_dec(&fs_info->async_submit_draining);
  7998. out:
  7999. if (!list_empty_careful(&splice)) {
  8000. spin_lock(&fs_info->delalloc_root_lock);
  8001. list_splice_tail(&splice, &fs_info->delalloc_roots);
  8002. spin_unlock(&fs_info->delalloc_root_lock);
  8003. }
  8004. mutex_unlock(&fs_info->delalloc_root_mutex);
  8005. return ret;
  8006. }
  8007. static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
  8008. const char *symname)
  8009. {
  8010. struct btrfs_trans_handle *trans;
  8011. struct btrfs_root *root = BTRFS_I(dir)->root;
  8012. struct btrfs_path *path;
  8013. struct btrfs_key key;
  8014. struct inode *inode = NULL;
  8015. int err;
  8016. int drop_inode = 0;
  8017. u64 objectid;
  8018. u64 index = 0;
  8019. int name_len;
  8020. int datasize;
  8021. unsigned long ptr;
  8022. struct btrfs_file_extent_item *ei;
  8023. struct extent_buffer *leaf;
  8024. name_len = strlen(symname);
  8025. if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
  8026. return -ENAMETOOLONG;
  8027. /*
  8028. * 2 items for inode item and ref
  8029. * 2 items for dir items
  8030. * 1 item for xattr if selinux is on
  8031. */
  8032. trans = btrfs_start_transaction(root, 5);
  8033. if (IS_ERR(trans))
  8034. return PTR_ERR(trans);
  8035. err = btrfs_find_free_ino(root, &objectid);
  8036. if (err)
  8037. goto out_unlock;
  8038. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  8039. dentry->d_name.len, btrfs_ino(dir), objectid,
  8040. S_IFLNK|S_IRWXUGO, &index);
  8041. if (IS_ERR(inode)) {
  8042. err = PTR_ERR(inode);
  8043. goto out_unlock;
  8044. }
  8045. /*
  8046. * If the active LSM wants to access the inode during
  8047. * d_instantiate it needs these. Smack checks to see
  8048. * if the filesystem supports xattrs by looking at the
  8049. * ops vector.
  8050. */
  8051. inode->i_fop = &btrfs_file_operations;
  8052. inode->i_op = &btrfs_file_inode_operations;
  8053. inode->i_mapping->a_ops = &btrfs_aops;
  8054. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  8055. err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
  8056. if (err)
  8057. goto out_unlock_inode;
  8058. err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
  8059. if (err)
  8060. goto out_unlock_inode;
  8061. path = btrfs_alloc_path();
  8062. if (!path) {
  8063. err = -ENOMEM;
  8064. goto out_unlock_inode;
  8065. }
  8066. key.objectid = btrfs_ino(inode);
  8067. key.offset = 0;
  8068. key.type = BTRFS_EXTENT_DATA_KEY;
  8069. datasize = btrfs_file_extent_calc_inline_size(name_len);
  8070. err = btrfs_insert_empty_item(trans, root, path, &key,
  8071. datasize);
  8072. if (err) {
  8073. btrfs_free_path(path);
  8074. goto out_unlock_inode;
  8075. }
  8076. leaf = path->nodes[0];
  8077. ei = btrfs_item_ptr(leaf, path->slots[0],
  8078. struct btrfs_file_extent_item);
  8079. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  8080. btrfs_set_file_extent_type(leaf, ei,
  8081. BTRFS_FILE_EXTENT_INLINE);
  8082. btrfs_set_file_extent_encryption(leaf, ei, 0);
  8083. btrfs_set_file_extent_compression(leaf, ei, 0);
  8084. btrfs_set_file_extent_other_encoding(leaf, ei, 0);
  8085. btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
  8086. ptr = btrfs_file_extent_inline_start(ei);
  8087. write_extent_buffer(leaf, symname, ptr, name_len);
  8088. btrfs_mark_buffer_dirty(leaf);
  8089. btrfs_free_path(path);
  8090. inode->i_op = &btrfs_symlink_inode_operations;
  8091. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  8092. inode_set_bytes(inode, name_len);
  8093. btrfs_i_size_write(inode, name_len);
  8094. err = btrfs_update_inode(trans, root, inode);
  8095. if (err) {
  8096. drop_inode = 1;
  8097. goto out_unlock_inode;
  8098. }
  8099. unlock_new_inode(inode);
  8100. d_instantiate(dentry, inode);
  8101. out_unlock:
  8102. btrfs_end_transaction(trans, root);
  8103. if (drop_inode) {
  8104. inode_dec_link_count(inode);
  8105. iput(inode);
  8106. }
  8107. btrfs_btree_balance_dirty(root);
  8108. return err;
  8109. out_unlock_inode:
  8110. drop_inode = 1;
  8111. unlock_new_inode(inode);
  8112. goto out_unlock;
  8113. }
  8114. static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
  8115. u64 start, u64 num_bytes, u64 min_size,
  8116. loff_t actual_len, u64 *alloc_hint,
  8117. struct btrfs_trans_handle *trans)
  8118. {
  8119. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  8120. struct extent_map *em;
  8121. struct btrfs_root *root = BTRFS_I(inode)->root;
  8122. struct btrfs_key ins;
  8123. u64 cur_offset = start;
  8124. u64 i_size;
  8125. u64 cur_bytes;
  8126. int ret = 0;
  8127. bool own_trans = true;
  8128. if (trans)
  8129. own_trans = false;
  8130. while (num_bytes > 0) {
  8131. if (own_trans) {
  8132. trans = btrfs_start_transaction(root, 3);
  8133. if (IS_ERR(trans)) {
  8134. ret = PTR_ERR(trans);
  8135. break;
  8136. }
  8137. }
  8138. cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
  8139. cur_bytes = max(cur_bytes, min_size);
  8140. ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
  8141. *alloc_hint, &ins, 1, 0);
  8142. if (ret) {
  8143. if (own_trans)
  8144. btrfs_end_transaction(trans, root);
  8145. break;
  8146. }
  8147. ret = insert_reserved_file_extent(trans, inode,
  8148. cur_offset, ins.objectid,
  8149. ins.offset, ins.offset,
  8150. ins.offset, 0, 0, 0,
  8151. BTRFS_FILE_EXTENT_PREALLOC);
  8152. if (ret) {
  8153. btrfs_free_reserved_extent(root, ins.objectid,
  8154. ins.offset, 0);
  8155. btrfs_abort_transaction(trans, root, ret);
  8156. if (own_trans)
  8157. btrfs_end_transaction(trans, root);
  8158. break;
  8159. }
  8160. btrfs_drop_extent_cache(inode, cur_offset,
  8161. cur_offset + ins.offset -1, 0);
  8162. em = alloc_extent_map();
  8163. if (!em) {
  8164. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  8165. &BTRFS_I(inode)->runtime_flags);
  8166. goto next;
  8167. }
  8168. em->start = cur_offset;
  8169. em->orig_start = cur_offset;
  8170. em->len = ins.offset;
  8171. em->block_start = ins.objectid;
  8172. em->block_len = ins.offset;
  8173. em->orig_block_len = ins.offset;
  8174. em->ram_bytes = ins.offset;
  8175. em->bdev = root->fs_info->fs_devices->latest_bdev;
  8176. set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
  8177. em->generation = trans->transid;
  8178. while (1) {
  8179. write_lock(&em_tree->lock);
  8180. ret = add_extent_mapping(em_tree, em, 1);
  8181. write_unlock(&em_tree->lock);
  8182. if (ret != -EEXIST)
  8183. break;
  8184. btrfs_drop_extent_cache(inode, cur_offset,
  8185. cur_offset + ins.offset - 1,
  8186. 0);
  8187. }
  8188. free_extent_map(em);
  8189. next:
  8190. num_bytes -= ins.offset;
  8191. cur_offset += ins.offset;
  8192. *alloc_hint = ins.objectid + ins.offset;
  8193. inode_inc_iversion(inode);
  8194. inode->i_ctime = CURRENT_TIME;
  8195. BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
  8196. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  8197. (actual_len > inode->i_size) &&
  8198. (cur_offset > inode->i_size)) {
  8199. if (cur_offset > actual_len)
  8200. i_size = actual_len;
  8201. else
  8202. i_size = cur_offset;
  8203. i_size_write(inode, i_size);
  8204. btrfs_ordered_update_i_size(inode, i_size, NULL);
  8205. }
  8206. ret = btrfs_update_inode(trans, root, inode);
  8207. if (ret) {
  8208. btrfs_abort_transaction(trans, root, ret);
  8209. if (own_trans)
  8210. btrfs_end_transaction(trans, root);
  8211. break;
  8212. }
  8213. if (own_trans)
  8214. btrfs_end_transaction(trans, root);
  8215. }
  8216. return ret;
  8217. }
  8218. int btrfs_prealloc_file_range(struct inode *inode, int mode,
  8219. u64 start, u64 num_bytes, u64 min_size,
  8220. loff_t actual_len, u64 *alloc_hint)
  8221. {
  8222. return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
  8223. min_size, actual_len, alloc_hint,
  8224. NULL);
  8225. }
  8226. int btrfs_prealloc_file_range_trans(struct inode *inode,
  8227. struct btrfs_trans_handle *trans, int mode,
  8228. u64 start, u64 num_bytes, u64 min_size,
  8229. loff_t actual_len, u64 *alloc_hint)
  8230. {
  8231. return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
  8232. min_size, actual_len, alloc_hint, trans);
  8233. }
  8234. static int btrfs_set_page_dirty(struct page *page)
  8235. {
  8236. return __set_page_dirty_nobuffers(page);
  8237. }
  8238. static int btrfs_permission(struct inode *inode, int mask)
  8239. {
  8240. struct btrfs_root *root = BTRFS_I(inode)->root;
  8241. umode_t mode = inode->i_mode;
  8242. if (mask & MAY_WRITE &&
  8243. (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
  8244. if (btrfs_root_readonly(root))
  8245. return -EROFS;
  8246. if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
  8247. return -EACCES;
  8248. }
  8249. return generic_permission(inode, mask);
  8250. }
  8251. static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
  8252. {
  8253. struct btrfs_trans_handle *trans;
  8254. struct btrfs_root *root = BTRFS_I(dir)->root;
  8255. struct inode *inode = NULL;
  8256. u64 objectid;
  8257. u64 index;
  8258. int ret = 0;
  8259. /*
  8260. * 5 units required for adding orphan entry
  8261. */
  8262. trans = btrfs_start_transaction(root, 5);
  8263. if (IS_ERR(trans))
  8264. return PTR_ERR(trans);
  8265. ret = btrfs_find_free_ino(root, &objectid);
  8266. if (ret)
  8267. goto out;
  8268. inode = btrfs_new_inode(trans, root, dir, NULL, 0,
  8269. btrfs_ino(dir), objectid, mode, &index);
  8270. if (IS_ERR(inode)) {
  8271. ret = PTR_ERR(inode);
  8272. inode = NULL;
  8273. goto out;
  8274. }
  8275. inode->i_fop = &btrfs_file_operations;
  8276. inode->i_op = &btrfs_file_inode_operations;
  8277. inode->i_mapping->a_ops = &btrfs_aops;
  8278. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  8279. ret = btrfs_init_inode_security(trans, inode, dir, NULL);
  8280. if (ret)
  8281. goto out_inode;
  8282. ret = btrfs_update_inode(trans, root, inode);
  8283. if (ret)
  8284. goto out_inode;
  8285. ret = btrfs_orphan_add(trans, inode);
  8286. if (ret)
  8287. goto out_inode;
  8288. /*
  8289. * We set number of links to 0 in btrfs_new_inode(), and here we set
  8290. * it to 1 because d_tmpfile() will issue a warning if the count is 0,
  8291. * through:
  8292. *
  8293. * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
  8294. */
  8295. set_nlink(inode, 1);
  8296. unlock_new_inode(inode);
  8297. d_tmpfile(dentry, inode);
  8298. mark_inode_dirty(inode);
  8299. out:
  8300. btrfs_end_transaction(trans, root);
  8301. if (ret)
  8302. iput(inode);
  8303. btrfs_balance_delayed_items(root);
  8304. btrfs_btree_balance_dirty(root);
  8305. return ret;
  8306. out_inode:
  8307. unlock_new_inode(inode);
  8308. goto out;
  8309. }
  8310. /* Inspired by filemap_check_errors() */
  8311. int btrfs_inode_check_errors(struct inode *inode)
  8312. {
  8313. int ret = 0;
  8314. if (test_bit(AS_ENOSPC, &inode->i_mapping->flags) &&
  8315. test_and_clear_bit(AS_ENOSPC, &inode->i_mapping->flags))
  8316. ret = -ENOSPC;
  8317. if (test_bit(AS_EIO, &inode->i_mapping->flags) &&
  8318. test_and_clear_bit(AS_EIO, &inode->i_mapping->flags))
  8319. ret = -EIO;
  8320. return ret;
  8321. }
  8322. static const struct inode_operations btrfs_dir_inode_operations = {
  8323. .getattr = btrfs_getattr,
  8324. .lookup = btrfs_lookup,
  8325. .create = btrfs_create,
  8326. .unlink = btrfs_unlink,
  8327. .link = btrfs_link,
  8328. .mkdir = btrfs_mkdir,
  8329. .rmdir = btrfs_rmdir,
  8330. .rename2 = btrfs_rename2,
  8331. .symlink = btrfs_symlink,
  8332. .setattr = btrfs_setattr,
  8333. .mknod = btrfs_mknod,
  8334. .setxattr = btrfs_setxattr,
  8335. .getxattr = btrfs_getxattr,
  8336. .listxattr = btrfs_listxattr,
  8337. .removexattr = btrfs_removexattr,
  8338. .permission = btrfs_permission,
  8339. .get_acl = btrfs_get_acl,
  8340. .set_acl = btrfs_set_acl,
  8341. .update_time = btrfs_update_time,
  8342. .tmpfile = btrfs_tmpfile,
  8343. };
  8344. static const struct inode_operations btrfs_dir_ro_inode_operations = {
  8345. .lookup = btrfs_lookup,
  8346. .permission = btrfs_permission,
  8347. .get_acl = btrfs_get_acl,
  8348. .set_acl = btrfs_set_acl,
  8349. .update_time = btrfs_update_time,
  8350. };
  8351. static const struct file_operations btrfs_dir_file_operations = {
  8352. .llseek = generic_file_llseek,
  8353. .read = generic_read_dir,
  8354. .iterate = btrfs_real_readdir,
  8355. .unlocked_ioctl = btrfs_ioctl,
  8356. #ifdef CONFIG_COMPAT
  8357. .compat_ioctl = btrfs_ioctl,
  8358. #endif
  8359. .release = btrfs_release_file,
  8360. .fsync = btrfs_sync_file,
  8361. };
  8362. static struct extent_io_ops btrfs_extent_io_ops = {
  8363. .fill_delalloc = run_delalloc_range,
  8364. .submit_bio_hook = btrfs_submit_bio_hook,
  8365. .merge_bio_hook = btrfs_merge_bio_hook,
  8366. .readpage_end_io_hook = btrfs_readpage_end_io_hook,
  8367. .writepage_end_io_hook = btrfs_writepage_end_io_hook,
  8368. .writepage_start_hook = btrfs_writepage_start_hook,
  8369. .set_bit_hook = btrfs_set_bit_hook,
  8370. .clear_bit_hook = btrfs_clear_bit_hook,
  8371. .merge_extent_hook = btrfs_merge_extent_hook,
  8372. .split_extent_hook = btrfs_split_extent_hook,
  8373. };
  8374. /*
  8375. * btrfs doesn't support the bmap operation because swapfiles
  8376. * use bmap to make a mapping of extents in the file. They assume
  8377. * these extents won't change over the life of the file and they
  8378. * use the bmap result to do IO directly to the drive.
  8379. *
  8380. * the btrfs bmap call would return logical addresses that aren't
  8381. * suitable for IO and they also will change frequently as COW
  8382. * operations happen. So, swapfile + btrfs == corruption.
  8383. *
  8384. * For now we're avoiding this by dropping bmap.
  8385. */
  8386. static const struct address_space_operations btrfs_aops = {
  8387. .readpage = btrfs_readpage,
  8388. .writepage = btrfs_writepage,
  8389. .writepages = btrfs_writepages,
  8390. .readpages = btrfs_readpages,
  8391. .direct_IO = btrfs_direct_IO,
  8392. .invalidatepage = btrfs_invalidatepage,
  8393. .releasepage = btrfs_releasepage,
  8394. .set_page_dirty = btrfs_set_page_dirty,
  8395. .error_remove_page = generic_error_remove_page,
  8396. };
  8397. static const struct address_space_operations btrfs_symlink_aops = {
  8398. .readpage = btrfs_readpage,
  8399. .writepage = btrfs_writepage,
  8400. .invalidatepage = btrfs_invalidatepage,
  8401. .releasepage = btrfs_releasepage,
  8402. };
  8403. static const struct inode_operations btrfs_file_inode_operations = {
  8404. .getattr = btrfs_getattr,
  8405. .setattr = btrfs_setattr,
  8406. .setxattr = btrfs_setxattr,
  8407. .getxattr = btrfs_getxattr,
  8408. .listxattr = btrfs_listxattr,
  8409. .removexattr = btrfs_removexattr,
  8410. .permission = btrfs_permission,
  8411. .fiemap = btrfs_fiemap,
  8412. .get_acl = btrfs_get_acl,
  8413. .set_acl = btrfs_set_acl,
  8414. .update_time = btrfs_update_time,
  8415. };
  8416. static const struct inode_operations btrfs_special_inode_operations = {
  8417. .getattr = btrfs_getattr,
  8418. .setattr = btrfs_setattr,
  8419. .permission = btrfs_permission,
  8420. .setxattr = btrfs_setxattr,
  8421. .getxattr = btrfs_getxattr,
  8422. .listxattr = btrfs_listxattr,
  8423. .removexattr = btrfs_removexattr,
  8424. .get_acl = btrfs_get_acl,
  8425. .set_acl = btrfs_set_acl,
  8426. .update_time = btrfs_update_time,
  8427. };
  8428. static const struct inode_operations btrfs_symlink_inode_operations = {
  8429. .readlink = generic_readlink,
  8430. .follow_link = page_follow_link_light,
  8431. .put_link = page_put_link,
  8432. .getattr = btrfs_getattr,
  8433. .setattr = btrfs_setattr,
  8434. .permission = btrfs_permission,
  8435. .setxattr = btrfs_setxattr,
  8436. .getxattr = btrfs_getxattr,
  8437. .listxattr = btrfs_listxattr,
  8438. .removexattr = btrfs_removexattr,
  8439. .update_time = btrfs_update_time,
  8440. };
  8441. const struct dentry_operations btrfs_dentry_operations = {
  8442. .d_delete = btrfs_dentry_delete,
  8443. .d_release = btrfs_dentry_release,
  8444. };