inode.c 240 KB

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