inode.c 242 KB

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