inode.c 195 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338
  1. /*
  2. * Copyright (C) 2007 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/bio.h>
  20. #include <linux/buffer_head.h>
  21. #include <linux/file.h>
  22. #include <linux/fs.h>
  23. #include <linux/pagemap.h>
  24. #include <linux/highmem.h>
  25. #include <linux/time.h>
  26. #include <linux/init.h>
  27. #include <linux/string.h>
  28. #include <linux/backing-dev.h>
  29. #include <linux/mpage.h>
  30. #include <linux/swap.h>
  31. #include <linux/writeback.h>
  32. #include <linux/statfs.h>
  33. #include <linux/compat.h>
  34. #include <linux/bit_spinlock.h>
  35. #include <linux/xattr.h>
  36. #include <linux/posix_acl.h>
  37. #include <linux/falloc.h>
  38. #include <linux/slab.h>
  39. #include "compat.h"
  40. #include "ctree.h"
  41. #include "disk-io.h"
  42. #include "transaction.h"
  43. #include "btrfs_inode.h"
  44. #include "ioctl.h"
  45. #include "print-tree.h"
  46. #include "volumes.h"
  47. #include "ordered-data.h"
  48. #include "xattr.h"
  49. #include "tree-log.h"
  50. #include "compression.h"
  51. #include "locking.h"
  52. struct btrfs_iget_args {
  53. u64 ino;
  54. struct btrfs_root *root;
  55. };
  56. static const struct inode_operations btrfs_dir_inode_operations;
  57. static const struct inode_operations btrfs_symlink_inode_operations;
  58. static const struct inode_operations btrfs_dir_ro_inode_operations;
  59. static const struct inode_operations btrfs_special_inode_operations;
  60. static const struct inode_operations btrfs_file_inode_operations;
  61. static const struct address_space_operations btrfs_aops;
  62. static const struct address_space_operations btrfs_symlink_aops;
  63. static const struct file_operations btrfs_dir_file_operations;
  64. static struct extent_io_ops btrfs_extent_io_ops;
  65. static struct kmem_cache *btrfs_inode_cachep;
  66. struct kmem_cache *btrfs_trans_handle_cachep;
  67. struct kmem_cache *btrfs_transaction_cachep;
  68. struct kmem_cache *btrfs_path_cachep;
  69. #define S_SHIFT 12
  70. static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
  71. [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
  72. [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
  73. [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
  74. [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
  75. [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
  76. [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
  77. [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
  78. };
  79. static void btrfs_truncate(struct inode *inode);
  80. static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
  81. static noinline int cow_file_range(struct inode *inode,
  82. struct page *locked_page,
  83. u64 start, u64 end, int *page_started,
  84. unsigned long *nr_written, int unlock);
  85. static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
  86. struct inode *inode, struct inode *dir,
  87. const struct qstr *qstr)
  88. {
  89. int err;
  90. err = btrfs_init_acl(trans, inode, dir);
  91. if (!err)
  92. err = btrfs_xattr_security_init(trans, inode, dir, qstr);
  93. return err;
  94. }
  95. /*
  96. * this does all the hard work for inserting an inline extent into
  97. * the btree. The caller should have done a btrfs_drop_extents so that
  98. * no overlapping inline items exist in the btree
  99. */
  100. static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
  101. struct btrfs_root *root, struct inode *inode,
  102. u64 start, size_t size, size_t compressed_size,
  103. struct page **compressed_pages)
  104. {
  105. struct btrfs_key key;
  106. struct btrfs_path *path;
  107. struct extent_buffer *leaf;
  108. struct page *page = NULL;
  109. char *kaddr;
  110. unsigned long ptr;
  111. struct btrfs_file_extent_item *ei;
  112. int err = 0;
  113. int ret;
  114. size_t cur_size = size;
  115. size_t datasize;
  116. unsigned long offset;
  117. int use_compress = 0;
  118. if (compressed_size && compressed_pages) {
  119. use_compress = 1;
  120. cur_size = compressed_size;
  121. }
  122. path = btrfs_alloc_path();
  123. if (!path)
  124. return -ENOMEM;
  125. path->leave_spinning = 1;
  126. btrfs_set_trans_block_group(trans, inode);
  127. key.objectid = inode->i_ino;
  128. key.offset = start;
  129. btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
  130. datasize = btrfs_file_extent_calc_inline_size(cur_size);
  131. inode_add_bytes(inode, size);
  132. ret = btrfs_insert_empty_item(trans, root, path, &key,
  133. datasize);
  134. BUG_ON(ret);
  135. if (ret) {
  136. err = ret;
  137. goto fail;
  138. }
  139. leaf = path->nodes[0];
  140. ei = btrfs_item_ptr(leaf, path->slots[0],
  141. struct btrfs_file_extent_item);
  142. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  143. btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
  144. btrfs_set_file_extent_encryption(leaf, ei, 0);
  145. btrfs_set_file_extent_other_encoding(leaf, ei, 0);
  146. btrfs_set_file_extent_ram_bytes(leaf, ei, size);
  147. ptr = btrfs_file_extent_inline_start(ei);
  148. if (use_compress) {
  149. struct page *cpage;
  150. int i = 0;
  151. while (compressed_size > 0) {
  152. cpage = compressed_pages[i];
  153. cur_size = min_t(unsigned long, compressed_size,
  154. PAGE_CACHE_SIZE);
  155. kaddr = kmap_atomic(cpage, KM_USER0);
  156. write_extent_buffer(leaf, kaddr, ptr, cur_size);
  157. kunmap_atomic(kaddr, KM_USER0);
  158. i++;
  159. ptr += cur_size;
  160. compressed_size -= cur_size;
  161. }
  162. btrfs_set_file_extent_compression(leaf, ei,
  163. BTRFS_COMPRESS_ZLIB);
  164. } else {
  165. page = find_get_page(inode->i_mapping,
  166. start >> PAGE_CACHE_SHIFT);
  167. btrfs_set_file_extent_compression(leaf, ei, 0);
  168. kaddr = kmap_atomic(page, KM_USER0);
  169. offset = start & (PAGE_CACHE_SIZE - 1);
  170. write_extent_buffer(leaf, kaddr + offset, ptr, size);
  171. kunmap_atomic(kaddr, KM_USER0);
  172. page_cache_release(page);
  173. }
  174. btrfs_mark_buffer_dirty(leaf);
  175. btrfs_free_path(path);
  176. /*
  177. * we're an inline extent, so nobody can
  178. * extend the file past i_size without locking
  179. * a page we already have locked.
  180. *
  181. * We must do any isize and inode updates
  182. * before we unlock the pages. Otherwise we
  183. * could end up racing with unlink.
  184. */
  185. BTRFS_I(inode)->disk_i_size = inode->i_size;
  186. btrfs_update_inode(trans, root, inode);
  187. return 0;
  188. fail:
  189. btrfs_free_path(path);
  190. return err;
  191. }
  192. /*
  193. * conditionally insert an inline extent into the file. This
  194. * does the checks required to make sure the data is small enough
  195. * to fit as an inline extent.
  196. */
  197. static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
  198. struct btrfs_root *root,
  199. struct inode *inode, u64 start, u64 end,
  200. size_t compressed_size,
  201. struct page **compressed_pages)
  202. {
  203. u64 isize = i_size_read(inode);
  204. u64 actual_end = min(end + 1, isize);
  205. u64 inline_len = actual_end - start;
  206. u64 aligned_end = (end + root->sectorsize - 1) &
  207. ~((u64)root->sectorsize - 1);
  208. u64 hint_byte;
  209. u64 data_len = inline_len;
  210. int ret;
  211. if (compressed_size)
  212. data_len = compressed_size;
  213. if (start > 0 ||
  214. actual_end >= PAGE_CACHE_SIZE ||
  215. data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
  216. (!compressed_size &&
  217. (actual_end & (root->sectorsize - 1)) == 0) ||
  218. end + 1 < isize ||
  219. data_len > root->fs_info->max_inline) {
  220. return 1;
  221. }
  222. ret = btrfs_drop_extents(trans, inode, start, aligned_end,
  223. &hint_byte, 1);
  224. BUG_ON(ret);
  225. if (isize > actual_end)
  226. inline_len = min_t(u64, isize, actual_end);
  227. ret = insert_inline_extent(trans, root, inode, start,
  228. inline_len, compressed_size,
  229. compressed_pages);
  230. BUG_ON(ret);
  231. btrfs_delalloc_release_metadata(inode, end + 1 - start);
  232. btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
  233. return 0;
  234. }
  235. struct async_extent {
  236. u64 start;
  237. u64 ram_size;
  238. u64 compressed_size;
  239. struct page **pages;
  240. unsigned long nr_pages;
  241. struct list_head list;
  242. };
  243. struct async_cow {
  244. struct inode *inode;
  245. struct btrfs_root *root;
  246. struct page *locked_page;
  247. u64 start;
  248. u64 end;
  249. struct list_head extents;
  250. struct btrfs_work work;
  251. };
  252. static noinline int add_async_extent(struct async_cow *cow,
  253. u64 start, u64 ram_size,
  254. u64 compressed_size,
  255. struct page **pages,
  256. unsigned long nr_pages)
  257. {
  258. struct async_extent *async_extent;
  259. async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
  260. async_extent->start = start;
  261. async_extent->ram_size = ram_size;
  262. async_extent->compressed_size = compressed_size;
  263. async_extent->pages = pages;
  264. async_extent->nr_pages = nr_pages;
  265. list_add_tail(&async_extent->list, &cow->extents);
  266. return 0;
  267. }
  268. /*
  269. * we create compressed extents in two phases. The first
  270. * phase compresses a range of pages that have already been
  271. * locked (both pages and state bits are locked).
  272. *
  273. * This is done inside an ordered work queue, and the compression
  274. * is spread across many cpus. The actual IO submission is step
  275. * two, and the ordered work queue takes care of making sure that
  276. * happens in the same order things were put onto the queue by
  277. * writepages and friends.
  278. *
  279. * If this code finds it can't get good compression, it puts an
  280. * entry onto the work queue to write the uncompressed bytes. This
  281. * makes sure that both compressed inodes and uncompressed inodes
  282. * are written in the same order that pdflush sent them down.
  283. */
  284. static noinline int compress_file_range(struct inode *inode,
  285. struct page *locked_page,
  286. u64 start, u64 end,
  287. struct async_cow *async_cow,
  288. int *num_added)
  289. {
  290. struct btrfs_root *root = BTRFS_I(inode)->root;
  291. struct btrfs_trans_handle *trans;
  292. u64 num_bytes;
  293. u64 blocksize = root->sectorsize;
  294. u64 actual_end;
  295. u64 isize = i_size_read(inode);
  296. int ret = 0;
  297. struct page **pages = NULL;
  298. unsigned long nr_pages;
  299. unsigned long nr_pages_ret = 0;
  300. unsigned long total_compressed = 0;
  301. unsigned long total_in = 0;
  302. unsigned long max_compressed = 128 * 1024;
  303. unsigned long max_uncompressed = 128 * 1024;
  304. int i;
  305. int will_compress;
  306. actual_end = min_t(u64, isize, end + 1);
  307. again:
  308. will_compress = 0;
  309. nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
  310. nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
  311. /*
  312. * we don't want to send crud past the end of i_size through
  313. * compression, that's just a waste of CPU time. So, if the
  314. * end of the file is before the start of our current
  315. * requested range of bytes, we bail out to the uncompressed
  316. * cleanup code that can deal with all of this.
  317. *
  318. * It isn't really the fastest way to fix things, but this is a
  319. * very uncommon corner.
  320. */
  321. if (actual_end <= start)
  322. goto cleanup_and_bail_uncompressed;
  323. total_compressed = actual_end - start;
  324. /* we want to make sure that amount of ram required to uncompress
  325. * an extent is reasonable, so we limit the total size in ram
  326. * of a compressed extent to 128k. This is a crucial number
  327. * because it also controls how easily we can spread reads across
  328. * cpus for decompression.
  329. *
  330. * We also want to make sure the amount of IO required to do
  331. * a random read is reasonably small, so we limit the size of
  332. * a compressed extent to 128k.
  333. */
  334. total_compressed = min(total_compressed, max_uncompressed);
  335. num_bytes = (end - start + blocksize) & ~(blocksize - 1);
  336. num_bytes = max(blocksize, num_bytes);
  337. total_in = 0;
  338. ret = 0;
  339. /*
  340. * we do compression for mount -o compress and when the
  341. * inode has not been flagged as nocompress. This flag can
  342. * change at any time if we discover bad compression ratios.
  343. */
  344. if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
  345. (btrfs_test_opt(root, COMPRESS) ||
  346. (BTRFS_I(inode)->force_compress))) {
  347. WARN_ON(pages);
  348. pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
  349. ret = btrfs_zlib_compress_pages(inode->i_mapping, start,
  350. total_compressed, pages,
  351. nr_pages, &nr_pages_ret,
  352. &total_in,
  353. &total_compressed,
  354. max_compressed);
  355. if (!ret) {
  356. unsigned long offset = total_compressed &
  357. (PAGE_CACHE_SIZE - 1);
  358. struct page *page = pages[nr_pages_ret - 1];
  359. char *kaddr;
  360. /* zero the tail end of the last page, we might be
  361. * sending it down to disk
  362. */
  363. if (offset) {
  364. kaddr = kmap_atomic(page, KM_USER0);
  365. memset(kaddr + offset, 0,
  366. PAGE_CACHE_SIZE - offset);
  367. kunmap_atomic(kaddr, KM_USER0);
  368. }
  369. will_compress = 1;
  370. }
  371. }
  372. if (start == 0) {
  373. trans = btrfs_join_transaction(root, 1);
  374. BUG_ON(!trans);
  375. btrfs_set_trans_block_group(trans, inode);
  376. trans->block_rsv = &root->fs_info->delalloc_block_rsv;
  377. /* lets try to make an inline extent */
  378. if (ret || total_in < (actual_end - start)) {
  379. /* we didn't compress the entire range, try
  380. * to make an uncompressed inline extent.
  381. */
  382. ret = cow_file_range_inline(trans, root, inode,
  383. start, end, 0, NULL);
  384. } else {
  385. /* try making a compressed inline extent */
  386. ret = cow_file_range_inline(trans, root, inode,
  387. start, end,
  388. total_compressed, pages);
  389. }
  390. if (ret == 0) {
  391. /*
  392. * inline extent creation worked, we don't need
  393. * to create any more async work items. Unlock
  394. * and free up our temp pages.
  395. */
  396. extent_clear_unlock_delalloc(inode,
  397. &BTRFS_I(inode)->io_tree,
  398. start, end, NULL,
  399. EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
  400. EXTENT_CLEAR_DELALLOC |
  401. EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
  402. btrfs_end_transaction(trans, root);
  403. goto free_pages_out;
  404. }
  405. btrfs_end_transaction(trans, root);
  406. }
  407. if (will_compress) {
  408. /*
  409. * we aren't doing an inline extent round the compressed size
  410. * up to a block size boundary so the allocator does sane
  411. * things
  412. */
  413. total_compressed = (total_compressed + blocksize - 1) &
  414. ~(blocksize - 1);
  415. /*
  416. * one last check to make sure the compression is really a
  417. * win, compare the page count read with the blocks on disk
  418. */
  419. total_in = (total_in + PAGE_CACHE_SIZE - 1) &
  420. ~(PAGE_CACHE_SIZE - 1);
  421. if (total_compressed >= total_in) {
  422. will_compress = 0;
  423. } else {
  424. num_bytes = total_in;
  425. }
  426. }
  427. if (!will_compress && pages) {
  428. /*
  429. * the compression code ran but failed to make things smaller,
  430. * free any pages it allocated and our page pointer array
  431. */
  432. for (i = 0; i < nr_pages_ret; i++) {
  433. WARN_ON(pages[i]->mapping);
  434. page_cache_release(pages[i]);
  435. }
  436. kfree(pages);
  437. pages = NULL;
  438. total_compressed = 0;
  439. nr_pages_ret = 0;
  440. /* flag the file so we don't compress in the future */
  441. if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
  442. !(BTRFS_I(inode)->force_compress)) {
  443. BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
  444. }
  445. }
  446. if (will_compress) {
  447. *num_added += 1;
  448. /* the async work queues will take care of doing actual
  449. * allocation on disk for these compressed pages,
  450. * and will submit them to the elevator.
  451. */
  452. add_async_extent(async_cow, start, num_bytes,
  453. total_compressed, pages, nr_pages_ret);
  454. if (start + num_bytes < end) {
  455. start += num_bytes;
  456. pages = NULL;
  457. cond_resched();
  458. goto again;
  459. }
  460. } else {
  461. cleanup_and_bail_uncompressed:
  462. /*
  463. * No compression, but we still need to write the pages in
  464. * the file we've been given so far. redirty the locked
  465. * page if it corresponds to our extent and set things up
  466. * for the async work queue to run cow_file_range to do
  467. * the normal delalloc dance
  468. */
  469. if (page_offset(locked_page) >= start &&
  470. page_offset(locked_page) <= end) {
  471. __set_page_dirty_nobuffers(locked_page);
  472. /* unlocked later on in the async handlers */
  473. }
  474. add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0);
  475. *num_added += 1;
  476. }
  477. out:
  478. return 0;
  479. free_pages_out:
  480. for (i = 0; i < nr_pages_ret; i++) {
  481. WARN_ON(pages[i]->mapping);
  482. page_cache_release(pages[i]);
  483. }
  484. kfree(pages);
  485. goto out;
  486. }
  487. /*
  488. * phase two of compressed writeback. This is the ordered portion
  489. * of the code, which only gets called in the order the work was
  490. * queued. We walk all the async extents created by compress_file_range
  491. * and send them down to the disk.
  492. */
  493. static noinline int submit_compressed_extents(struct inode *inode,
  494. struct async_cow *async_cow)
  495. {
  496. struct async_extent *async_extent;
  497. u64 alloc_hint = 0;
  498. struct btrfs_trans_handle *trans;
  499. struct btrfs_key ins;
  500. struct extent_map *em;
  501. struct btrfs_root *root = BTRFS_I(inode)->root;
  502. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  503. struct extent_io_tree *io_tree;
  504. int ret = 0;
  505. if (list_empty(&async_cow->extents))
  506. return 0;
  507. while (!list_empty(&async_cow->extents)) {
  508. async_extent = list_entry(async_cow->extents.next,
  509. struct async_extent, list);
  510. list_del(&async_extent->list);
  511. io_tree = &BTRFS_I(inode)->io_tree;
  512. retry:
  513. /* did the compression code fall back to uncompressed IO? */
  514. if (!async_extent->pages) {
  515. int page_started = 0;
  516. unsigned long nr_written = 0;
  517. lock_extent(io_tree, async_extent->start,
  518. async_extent->start +
  519. async_extent->ram_size - 1, GFP_NOFS);
  520. /* allocate blocks */
  521. ret = cow_file_range(inode, async_cow->locked_page,
  522. async_extent->start,
  523. async_extent->start +
  524. async_extent->ram_size - 1,
  525. &page_started, &nr_written, 0);
  526. /*
  527. * if page_started, cow_file_range inserted an
  528. * inline extent and took care of all the unlocking
  529. * and IO for us. Otherwise, we need to submit
  530. * all those pages down to the drive.
  531. */
  532. if (!page_started && !ret)
  533. extent_write_locked_range(io_tree,
  534. inode, async_extent->start,
  535. async_extent->start +
  536. async_extent->ram_size - 1,
  537. btrfs_get_extent,
  538. WB_SYNC_ALL);
  539. kfree(async_extent);
  540. cond_resched();
  541. continue;
  542. }
  543. lock_extent(io_tree, async_extent->start,
  544. async_extent->start + async_extent->ram_size - 1,
  545. GFP_NOFS);
  546. trans = btrfs_join_transaction(root, 1);
  547. ret = btrfs_reserve_extent(trans, root,
  548. async_extent->compressed_size,
  549. async_extent->compressed_size,
  550. 0, alloc_hint,
  551. (u64)-1, &ins, 1);
  552. btrfs_end_transaction(trans, root);
  553. if (ret) {
  554. int i;
  555. for (i = 0; i < async_extent->nr_pages; i++) {
  556. WARN_ON(async_extent->pages[i]->mapping);
  557. page_cache_release(async_extent->pages[i]);
  558. }
  559. kfree(async_extent->pages);
  560. async_extent->nr_pages = 0;
  561. async_extent->pages = NULL;
  562. unlock_extent(io_tree, async_extent->start,
  563. async_extent->start +
  564. async_extent->ram_size - 1, GFP_NOFS);
  565. goto retry;
  566. }
  567. /*
  568. * here we're doing allocation and writeback of the
  569. * compressed pages
  570. */
  571. btrfs_drop_extent_cache(inode, async_extent->start,
  572. async_extent->start +
  573. async_extent->ram_size - 1, 0);
  574. em = alloc_extent_map(GFP_NOFS);
  575. em->start = async_extent->start;
  576. em->len = async_extent->ram_size;
  577. em->orig_start = em->start;
  578. em->block_start = ins.objectid;
  579. em->block_len = ins.offset;
  580. em->bdev = root->fs_info->fs_devices->latest_bdev;
  581. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  582. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  583. while (1) {
  584. write_lock(&em_tree->lock);
  585. ret = add_extent_mapping(em_tree, em);
  586. write_unlock(&em_tree->lock);
  587. if (ret != -EEXIST) {
  588. free_extent_map(em);
  589. break;
  590. }
  591. btrfs_drop_extent_cache(inode, async_extent->start,
  592. async_extent->start +
  593. async_extent->ram_size - 1, 0);
  594. }
  595. ret = btrfs_add_ordered_extent(inode, async_extent->start,
  596. ins.objectid,
  597. async_extent->ram_size,
  598. ins.offset,
  599. BTRFS_ORDERED_COMPRESSED);
  600. BUG_ON(ret);
  601. /*
  602. * clear dirty, set writeback and unlock the pages.
  603. */
  604. extent_clear_unlock_delalloc(inode,
  605. &BTRFS_I(inode)->io_tree,
  606. async_extent->start,
  607. async_extent->start +
  608. async_extent->ram_size - 1,
  609. NULL, EXTENT_CLEAR_UNLOCK_PAGE |
  610. EXTENT_CLEAR_UNLOCK |
  611. EXTENT_CLEAR_DELALLOC |
  612. EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
  613. ret = btrfs_submit_compressed_write(inode,
  614. async_extent->start,
  615. async_extent->ram_size,
  616. ins.objectid,
  617. ins.offset, async_extent->pages,
  618. async_extent->nr_pages);
  619. BUG_ON(ret);
  620. alloc_hint = ins.objectid + ins.offset;
  621. kfree(async_extent);
  622. cond_resched();
  623. }
  624. return 0;
  625. }
  626. static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
  627. u64 num_bytes)
  628. {
  629. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  630. struct extent_map *em;
  631. u64 alloc_hint = 0;
  632. read_lock(&em_tree->lock);
  633. em = search_extent_mapping(em_tree, start, num_bytes);
  634. if (em) {
  635. /*
  636. * if block start isn't an actual block number then find the
  637. * first block in this inode and use that as a hint. If that
  638. * block is also bogus then just don't worry about it.
  639. */
  640. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  641. free_extent_map(em);
  642. em = search_extent_mapping(em_tree, 0, 0);
  643. if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
  644. alloc_hint = em->block_start;
  645. if (em)
  646. free_extent_map(em);
  647. } else {
  648. alloc_hint = em->block_start;
  649. free_extent_map(em);
  650. }
  651. }
  652. read_unlock(&em_tree->lock);
  653. return alloc_hint;
  654. }
  655. /*
  656. * when extent_io.c finds a delayed allocation range in the file,
  657. * the call backs end up in this code. The basic idea is to
  658. * allocate extents on disk for the range, and create ordered data structs
  659. * in ram to track those extents.
  660. *
  661. * locked_page is the page that writepage had locked already. We use
  662. * it to make sure we don't do extra locks or unlocks.
  663. *
  664. * *page_started is set to one if we unlock locked_page and do everything
  665. * required to start IO on it. It may be clean and already done with
  666. * IO when we return.
  667. */
  668. static noinline int cow_file_range(struct inode *inode,
  669. struct page *locked_page,
  670. u64 start, u64 end, int *page_started,
  671. unsigned long *nr_written,
  672. int unlock)
  673. {
  674. struct btrfs_root *root = BTRFS_I(inode)->root;
  675. struct btrfs_trans_handle *trans;
  676. u64 alloc_hint = 0;
  677. u64 num_bytes;
  678. unsigned long ram_size;
  679. u64 disk_num_bytes;
  680. u64 cur_alloc_size;
  681. u64 blocksize = root->sectorsize;
  682. struct btrfs_key ins;
  683. struct extent_map *em;
  684. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  685. int ret = 0;
  686. BUG_ON(root == root->fs_info->tree_root);
  687. trans = btrfs_join_transaction(root, 1);
  688. BUG_ON(!trans);
  689. btrfs_set_trans_block_group(trans, inode);
  690. trans->block_rsv = &root->fs_info->delalloc_block_rsv;
  691. num_bytes = (end - start + blocksize) & ~(blocksize - 1);
  692. num_bytes = max(blocksize, num_bytes);
  693. disk_num_bytes = num_bytes;
  694. ret = 0;
  695. if (start == 0) {
  696. /* lets try to make an inline extent */
  697. ret = cow_file_range_inline(trans, root, inode,
  698. start, end, 0, NULL);
  699. if (ret == 0) {
  700. extent_clear_unlock_delalloc(inode,
  701. &BTRFS_I(inode)->io_tree,
  702. start, end, NULL,
  703. EXTENT_CLEAR_UNLOCK_PAGE |
  704. EXTENT_CLEAR_UNLOCK |
  705. EXTENT_CLEAR_DELALLOC |
  706. EXTENT_CLEAR_DIRTY |
  707. EXTENT_SET_WRITEBACK |
  708. EXTENT_END_WRITEBACK);
  709. *nr_written = *nr_written +
  710. (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
  711. *page_started = 1;
  712. ret = 0;
  713. goto out;
  714. }
  715. }
  716. BUG_ON(disk_num_bytes >
  717. btrfs_super_total_bytes(&root->fs_info->super_copy));
  718. alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
  719. btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
  720. while (disk_num_bytes > 0) {
  721. unsigned long op;
  722. cur_alloc_size = disk_num_bytes;
  723. ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
  724. root->sectorsize, 0, alloc_hint,
  725. (u64)-1, &ins, 1);
  726. BUG_ON(ret);
  727. em = alloc_extent_map(GFP_NOFS);
  728. em->start = start;
  729. em->orig_start = em->start;
  730. ram_size = ins.offset;
  731. em->len = ins.offset;
  732. em->block_start = ins.objectid;
  733. em->block_len = ins.offset;
  734. em->bdev = root->fs_info->fs_devices->latest_bdev;
  735. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  736. while (1) {
  737. write_lock(&em_tree->lock);
  738. ret = add_extent_mapping(em_tree, em);
  739. write_unlock(&em_tree->lock);
  740. if (ret != -EEXIST) {
  741. free_extent_map(em);
  742. break;
  743. }
  744. btrfs_drop_extent_cache(inode, start,
  745. start + ram_size - 1, 0);
  746. }
  747. cur_alloc_size = ins.offset;
  748. ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
  749. ram_size, cur_alloc_size, 0);
  750. BUG_ON(ret);
  751. if (root->root_key.objectid ==
  752. BTRFS_DATA_RELOC_TREE_OBJECTID) {
  753. ret = btrfs_reloc_clone_csums(inode, start,
  754. cur_alloc_size);
  755. BUG_ON(ret);
  756. }
  757. if (disk_num_bytes < cur_alloc_size)
  758. break;
  759. /* we're not doing compressed IO, don't unlock the first
  760. * page (which the caller expects to stay locked), don't
  761. * clear any dirty bits and don't set any writeback bits
  762. *
  763. * Do set the Private2 bit so we know this page was properly
  764. * setup for writepage
  765. */
  766. op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
  767. op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
  768. EXTENT_SET_PRIVATE2;
  769. extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
  770. start, start + ram_size - 1,
  771. locked_page, op);
  772. disk_num_bytes -= cur_alloc_size;
  773. num_bytes -= cur_alloc_size;
  774. alloc_hint = ins.objectid + ins.offset;
  775. start += cur_alloc_size;
  776. }
  777. out:
  778. ret = 0;
  779. btrfs_end_transaction(trans, root);
  780. return ret;
  781. }
  782. /*
  783. * work queue call back to started compression on a file and pages
  784. */
  785. static noinline void async_cow_start(struct btrfs_work *work)
  786. {
  787. struct async_cow *async_cow;
  788. int num_added = 0;
  789. async_cow = container_of(work, struct async_cow, work);
  790. compress_file_range(async_cow->inode, async_cow->locked_page,
  791. async_cow->start, async_cow->end, async_cow,
  792. &num_added);
  793. if (num_added == 0)
  794. async_cow->inode = NULL;
  795. }
  796. /*
  797. * work queue call back to submit previously compressed pages
  798. */
  799. static noinline void async_cow_submit(struct btrfs_work *work)
  800. {
  801. struct async_cow *async_cow;
  802. struct btrfs_root *root;
  803. unsigned long nr_pages;
  804. async_cow = container_of(work, struct async_cow, work);
  805. root = async_cow->root;
  806. nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
  807. PAGE_CACHE_SHIFT;
  808. atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
  809. if (atomic_read(&root->fs_info->async_delalloc_pages) <
  810. 5 * 1042 * 1024 &&
  811. waitqueue_active(&root->fs_info->async_submit_wait))
  812. wake_up(&root->fs_info->async_submit_wait);
  813. if (async_cow->inode)
  814. submit_compressed_extents(async_cow->inode, async_cow);
  815. }
  816. static noinline void async_cow_free(struct btrfs_work *work)
  817. {
  818. struct async_cow *async_cow;
  819. async_cow = container_of(work, struct async_cow, work);
  820. kfree(async_cow);
  821. }
  822. static int cow_file_range_async(struct inode *inode, struct page *locked_page,
  823. u64 start, u64 end, int *page_started,
  824. unsigned long *nr_written)
  825. {
  826. struct async_cow *async_cow;
  827. struct btrfs_root *root = BTRFS_I(inode)->root;
  828. unsigned long nr_pages;
  829. u64 cur_end;
  830. int limit = 10 * 1024 * 1042;
  831. clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
  832. 1, 0, NULL, GFP_NOFS);
  833. while (start < end) {
  834. async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
  835. async_cow->inode = inode;
  836. async_cow->root = root;
  837. async_cow->locked_page = locked_page;
  838. async_cow->start = start;
  839. if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
  840. cur_end = end;
  841. else
  842. cur_end = min(end, start + 512 * 1024 - 1);
  843. async_cow->end = cur_end;
  844. INIT_LIST_HEAD(&async_cow->extents);
  845. async_cow->work.func = async_cow_start;
  846. async_cow->work.ordered_func = async_cow_submit;
  847. async_cow->work.ordered_free = async_cow_free;
  848. async_cow->work.flags = 0;
  849. nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
  850. PAGE_CACHE_SHIFT;
  851. atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
  852. btrfs_queue_worker(&root->fs_info->delalloc_workers,
  853. &async_cow->work);
  854. if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
  855. wait_event(root->fs_info->async_submit_wait,
  856. (atomic_read(&root->fs_info->async_delalloc_pages) <
  857. limit));
  858. }
  859. while (atomic_read(&root->fs_info->async_submit_draining) &&
  860. atomic_read(&root->fs_info->async_delalloc_pages)) {
  861. wait_event(root->fs_info->async_submit_wait,
  862. (atomic_read(&root->fs_info->async_delalloc_pages) ==
  863. 0));
  864. }
  865. *nr_written += nr_pages;
  866. start = cur_end + 1;
  867. }
  868. *page_started = 1;
  869. return 0;
  870. }
  871. static noinline int csum_exist_in_range(struct btrfs_root *root,
  872. u64 bytenr, u64 num_bytes)
  873. {
  874. int ret;
  875. struct btrfs_ordered_sum *sums;
  876. LIST_HEAD(list);
  877. ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
  878. bytenr + num_bytes - 1, &list);
  879. if (ret == 0 && list_empty(&list))
  880. return 0;
  881. while (!list_empty(&list)) {
  882. sums = list_entry(list.next, struct btrfs_ordered_sum, list);
  883. list_del(&sums->list);
  884. kfree(sums);
  885. }
  886. return 1;
  887. }
  888. /*
  889. * when nowcow writeback call back. This checks for snapshots or COW copies
  890. * of the extents that exist in the file, and COWs the file as required.
  891. *
  892. * If no cow copies or snapshots exist, we write directly to the existing
  893. * blocks on disk
  894. */
  895. static noinline int run_delalloc_nocow(struct inode *inode,
  896. struct page *locked_page,
  897. u64 start, u64 end, int *page_started, int force,
  898. unsigned long *nr_written)
  899. {
  900. struct btrfs_root *root = BTRFS_I(inode)->root;
  901. struct btrfs_trans_handle *trans;
  902. struct extent_buffer *leaf;
  903. struct btrfs_path *path;
  904. struct btrfs_file_extent_item *fi;
  905. struct btrfs_key found_key;
  906. u64 cow_start;
  907. u64 cur_offset;
  908. u64 extent_end;
  909. u64 extent_offset;
  910. u64 disk_bytenr;
  911. u64 num_bytes;
  912. int extent_type;
  913. int ret;
  914. int type;
  915. int nocow;
  916. int check_prev = 1;
  917. bool nolock = false;
  918. path = btrfs_alloc_path();
  919. BUG_ON(!path);
  920. if (root == root->fs_info->tree_root) {
  921. nolock = true;
  922. trans = btrfs_join_transaction_nolock(root, 1);
  923. } else {
  924. trans = btrfs_join_transaction(root, 1);
  925. }
  926. BUG_ON(!trans);
  927. cow_start = (u64)-1;
  928. cur_offset = start;
  929. while (1) {
  930. ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
  931. cur_offset, 0);
  932. BUG_ON(ret < 0);
  933. if (ret > 0 && path->slots[0] > 0 && check_prev) {
  934. leaf = path->nodes[0];
  935. btrfs_item_key_to_cpu(leaf, &found_key,
  936. path->slots[0] - 1);
  937. if (found_key.objectid == inode->i_ino &&
  938. found_key.type == BTRFS_EXTENT_DATA_KEY)
  939. path->slots[0]--;
  940. }
  941. check_prev = 0;
  942. next_slot:
  943. leaf = path->nodes[0];
  944. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  945. ret = btrfs_next_leaf(root, path);
  946. if (ret < 0)
  947. BUG_ON(1);
  948. if (ret > 0)
  949. break;
  950. leaf = path->nodes[0];
  951. }
  952. nocow = 0;
  953. disk_bytenr = 0;
  954. num_bytes = 0;
  955. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  956. if (found_key.objectid > inode->i_ino ||
  957. found_key.type > BTRFS_EXTENT_DATA_KEY ||
  958. found_key.offset > end)
  959. break;
  960. if (found_key.offset > cur_offset) {
  961. extent_end = found_key.offset;
  962. extent_type = 0;
  963. goto out_check;
  964. }
  965. fi = btrfs_item_ptr(leaf, path->slots[0],
  966. struct btrfs_file_extent_item);
  967. extent_type = btrfs_file_extent_type(leaf, fi);
  968. if (extent_type == BTRFS_FILE_EXTENT_REG ||
  969. extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
  970. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  971. extent_offset = btrfs_file_extent_offset(leaf, fi);
  972. extent_end = found_key.offset +
  973. btrfs_file_extent_num_bytes(leaf, fi);
  974. if (extent_end <= start) {
  975. path->slots[0]++;
  976. goto next_slot;
  977. }
  978. if (disk_bytenr == 0)
  979. goto out_check;
  980. if (btrfs_file_extent_compression(leaf, fi) ||
  981. btrfs_file_extent_encryption(leaf, fi) ||
  982. btrfs_file_extent_other_encoding(leaf, fi))
  983. goto out_check;
  984. if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
  985. goto out_check;
  986. if (btrfs_extent_readonly(root, disk_bytenr))
  987. goto out_check;
  988. if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
  989. found_key.offset -
  990. extent_offset, disk_bytenr))
  991. goto out_check;
  992. disk_bytenr += extent_offset;
  993. disk_bytenr += cur_offset - found_key.offset;
  994. num_bytes = min(end + 1, extent_end) - cur_offset;
  995. /*
  996. * force cow if csum exists in the range.
  997. * this ensure that csum for a given extent are
  998. * either valid or do not exist.
  999. */
  1000. if (csum_exist_in_range(root, disk_bytenr, num_bytes))
  1001. goto out_check;
  1002. nocow = 1;
  1003. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  1004. extent_end = found_key.offset +
  1005. btrfs_file_extent_inline_len(leaf, fi);
  1006. extent_end = ALIGN(extent_end, root->sectorsize);
  1007. } else {
  1008. BUG_ON(1);
  1009. }
  1010. out_check:
  1011. if (extent_end <= start) {
  1012. path->slots[0]++;
  1013. goto next_slot;
  1014. }
  1015. if (!nocow) {
  1016. if (cow_start == (u64)-1)
  1017. cow_start = cur_offset;
  1018. cur_offset = extent_end;
  1019. if (cur_offset > end)
  1020. break;
  1021. path->slots[0]++;
  1022. goto next_slot;
  1023. }
  1024. btrfs_release_path(root, path);
  1025. if (cow_start != (u64)-1) {
  1026. ret = cow_file_range(inode, locked_page, cow_start,
  1027. found_key.offset - 1, page_started,
  1028. nr_written, 1);
  1029. BUG_ON(ret);
  1030. cow_start = (u64)-1;
  1031. }
  1032. if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
  1033. struct extent_map *em;
  1034. struct extent_map_tree *em_tree;
  1035. em_tree = &BTRFS_I(inode)->extent_tree;
  1036. em = alloc_extent_map(GFP_NOFS);
  1037. em->start = cur_offset;
  1038. em->orig_start = em->start;
  1039. em->len = num_bytes;
  1040. em->block_len = num_bytes;
  1041. em->block_start = disk_bytenr;
  1042. em->bdev = root->fs_info->fs_devices->latest_bdev;
  1043. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  1044. while (1) {
  1045. write_lock(&em_tree->lock);
  1046. ret = add_extent_mapping(em_tree, em);
  1047. write_unlock(&em_tree->lock);
  1048. if (ret != -EEXIST) {
  1049. free_extent_map(em);
  1050. break;
  1051. }
  1052. btrfs_drop_extent_cache(inode, em->start,
  1053. em->start + em->len - 1, 0);
  1054. }
  1055. type = BTRFS_ORDERED_PREALLOC;
  1056. } else {
  1057. type = BTRFS_ORDERED_NOCOW;
  1058. }
  1059. ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
  1060. num_bytes, num_bytes, type);
  1061. BUG_ON(ret);
  1062. if (root->root_key.objectid ==
  1063. BTRFS_DATA_RELOC_TREE_OBJECTID) {
  1064. ret = btrfs_reloc_clone_csums(inode, cur_offset,
  1065. num_bytes);
  1066. BUG_ON(ret);
  1067. }
  1068. extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
  1069. cur_offset, cur_offset + num_bytes - 1,
  1070. locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
  1071. EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
  1072. EXTENT_SET_PRIVATE2);
  1073. cur_offset = extent_end;
  1074. if (cur_offset > end)
  1075. break;
  1076. }
  1077. btrfs_release_path(root, path);
  1078. if (cur_offset <= end && cow_start == (u64)-1)
  1079. cow_start = cur_offset;
  1080. if (cow_start != (u64)-1) {
  1081. ret = cow_file_range(inode, locked_page, cow_start, end,
  1082. page_started, nr_written, 1);
  1083. BUG_ON(ret);
  1084. }
  1085. if (nolock) {
  1086. ret = btrfs_end_transaction_nolock(trans, root);
  1087. BUG_ON(ret);
  1088. } else {
  1089. ret = btrfs_end_transaction(trans, root);
  1090. BUG_ON(ret);
  1091. }
  1092. btrfs_free_path(path);
  1093. return 0;
  1094. }
  1095. /*
  1096. * extent_io.c call back to do delayed allocation processing
  1097. */
  1098. static int run_delalloc_range(struct inode *inode, struct page *locked_page,
  1099. u64 start, u64 end, int *page_started,
  1100. unsigned long *nr_written)
  1101. {
  1102. int ret;
  1103. struct btrfs_root *root = BTRFS_I(inode)->root;
  1104. if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
  1105. ret = run_delalloc_nocow(inode, locked_page, start, end,
  1106. page_started, 1, nr_written);
  1107. else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
  1108. ret = run_delalloc_nocow(inode, locked_page, start, end,
  1109. page_started, 0, nr_written);
  1110. else if (!btrfs_test_opt(root, COMPRESS) &&
  1111. !(BTRFS_I(inode)->force_compress))
  1112. ret = cow_file_range(inode, locked_page, start, end,
  1113. page_started, nr_written, 1);
  1114. else
  1115. ret = cow_file_range_async(inode, locked_page, start, end,
  1116. page_started, nr_written);
  1117. return ret;
  1118. }
  1119. static int btrfs_split_extent_hook(struct inode *inode,
  1120. struct extent_state *orig, u64 split)
  1121. {
  1122. /* not delalloc, ignore it */
  1123. if (!(orig->state & EXTENT_DELALLOC))
  1124. return 0;
  1125. atomic_inc(&BTRFS_I(inode)->outstanding_extents);
  1126. return 0;
  1127. }
  1128. /*
  1129. * extent_io.c merge_extent_hook, used to track merged delayed allocation
  1130. * extents so we can keep track of new extents that are just merged onto old
  1131. * extents, such as when we are doing sequential writes, so we can properly
  1132. * account for the metadata space we'll need.
  1133. */
  1134. static int btrfs_merge_extent_hook(struct inode *inode,
  1135. struct extent_state *new,
  1136. struct extent_state *other)
  1137. {
  1138. /* not delalloc, ignore it */
  1139. if (!(other->state & EXTENT_DELALLOC))
  1140. return 0;
  1141. atomic_dec(&BTRFS_I(inode)->outstanding_extents);
  1142. return 0;
  1143. }
  1144. /*
  1145. * extent_io.c set_bit_hook, used to track delayed allocation
  1146. * bytes in this file, and to maintain the list of inodes that
  1147. * have pending delalloc work to be done.
  1148. */
  1149. static int btrfs_set_bit_hook(struct inode *inode,
  1150. struct extent_state *state, int *bits)
  1151. {
  1152. /*
  1153. * set_bit and clear bit hooks normally require _irqsave/restore
  1154. * but in this case, we are only testeing for the DELALLOC
  1155. * bit, which is only set or cleared with irqs on
  1156. */
  1157. if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
  1158. struct btrfs_root *root = BTRFS_I(inode)->root;
  1159. u64 len = state->end + 1 - state->start;
  1160. int do_list = (root->root_key.objectid !=
  1161. BTRFS_ROOT_TREE_OBJECTID);
  1162. if (*bits & EXTENT_FIRST_DELALLOC)
  1163. *bits &= ~EXTENT_FIRST_DELALLOC;
  1164. else
  1165. atomic_inc(&BTRFS_I(inode)->outstanding_extents);
  1166. spin_lock(&root->fs_info->delalloc_lock);
  1167. BTRFS_I(inode)->delalloc_bytes += len;
  1168. root->fs_info->delalloc_bytes += len;
  1169. if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
  1170. list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
  1171. &root->fs_info->delalloc_inodes);
  1172. }
  1173. spin_unlock(&root->fs_info->delalloc_lock);
  1174. }
  1175. return 0;
  1176. }
  1177. /*
  1178. * extent_io.c clear_bit_hook, see set_bit_hook for why
  1179. */
  1180. static int btrfs_clear_bit_hook(struct inode *inode,
  1181. struct extent_state *state, int *bits)
  1182. {
  1183. /*
  1184. * set_bit and clear bit hooks normally require _irqsave/restore
  1185. * but in this case, we are only testeing for the DELALLOC
  1186. * bit, which is only set or cleared with irqs on
  1187. */
  1188. if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
  1189. struct btrfs_root *root = BTRFS_I(inode)->root;
  1190. u64 len = state->end + 1 - state->start;
  1191. int do_list = (root->root_key.objectid !=
  1192. BTRFS_ROOT_TREE_OBJECTID);
  1193. if (*bits & EXTENT_FIRST_DELALLOC)
  1194. *bits &= ~EXTENT_FIRST_DELALLOC;
  1195. else if (!(*bits & EXTENT_DO_ACCOUNTING))
  1196. atomic_dec(&BTRFS_I(inode)->outstanding_extents);
  1197. if (*bits & EXTENT_DO_ACCOUNTING)
  1198. btrfs_delalloc_release_metadata(inode, len);
  1199. if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
  1200. && do_list)
  1201. btrfs_free_reserved_data_space(inode, len);
  1202. spin_lock(&root->fs_info->delalloc_lock);
  1203. root->fs_info->delalloc_bytes -= len;
  1204. BTRFS_I(inode)->delalloc_bytes -= len;
  1205. if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
  1206. !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
  1207. list_del_init(&BTRFS_I(inode)->delalloc_inodes);
  1208. }
  1209. spin_unlock(&root->fs_info->delalloc_lock);
  1210. }
  1211. return 0;
  1212. }
  1213. /*
  1214. * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
  1215. * we don't create bios that span stripes or chunks
  1216. */
  1217. int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
  1218. size_t size, struct bio *bio,
  1219. unsigned long bio_flags)
  1220. {
  1221. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  1222. struct btrfs_mapping_tree *map_tree;
  1223. u64 logical = (u64)bio->bi_sector << 9;
  1224. u64 length = 0;
  1225. u64 map_length;
  1226. int ret;
  1227. if (bio_flags & EXTENT_BIO_COMPRESSED)
  1228. return 0;
  1229. length = bio->bi_size;
  1230. map_tree = &root->fs_info->mapping_tree;
  1231. map_length = length;
  1232. ret = btrfs_map_block(map_tree, READ, logical,
  1233. &map_length, NULL, 0);
  1234. if (map_length < length + size)
  1235. return 1;
  1236. return ret;
  1237. }
  1238. /*
  1239. * in order to insert checksums into the metadata in large chunks,
  1240. * we wait until bio submission time. All the pages in the bio are
  1241. * checksummed and sums are attached onto the ordered extent record.
  1242. *
  1243. * At IO completion time the cums attached on the ordered extent record
  1244. * are inserted into the btree
  1245. */
  1246. static int __btrfs_submit_bio_start(struct inode *inode, int rw,
  1247. struct bio *bio, int mirror_num,
  1248. unsigned long bio_flags,
  1249. u64 bio_offset)
  1250. {
  1251. struct btrfs_root *root = BTRFS_I(inode)->root;
  1252. int ret = 0;
  1253. ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
  1254. BUG_ON(ret);
  1255. return 0;
  1256. }
  1257. /*
  1258. * in order to insert checksums into the metadata in large chunks,
  1259. * we wait until bio submission time. All the pages in the bio are
  1260. * checksummed and sums are attached onto the ordered extent record.
  1261. *
  1262. * At IO completion time the cums attached on the ordered extent record
  1263. * are inserted into the btree
  1264. */
  1265. static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
  1266. int mirror_num, unsigned long bio_flags,
  1267. u64 bio_offset)
  1268. {
  1269. struct btrfs_root *root = BTRFS_I(inode)->root;
  1270. return btrfs_map_bio(root, rw, bio, mirror_num, 1);
  1271. }
  1272. /*
  1273. * extent_io.c submission hook. This does the right thing for csum calculation
  1274. * on write, or reading the csums from the tree before a read
  1275. */
  1276. static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
  1277. int mirror_num, unsigned long bio_flags,
  1278. u64 bio_offset)
  1279. {
  1280. struct btrfs_root *root = BTRFS_I(inode)->root;
  1281. int ret = 0;
  1282. int skip_sum;
  1283. skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  1284. if (root == root->fs_info->tree_root)
  1285. ret = btrfs_bio_wq_end_io(root->fs_info, bio, 2);
  1286. else
  1287. ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
  1288. BUG_ON(ret);
  1289. if (!(rw & REQ_WRITE)) {
  1290. if (bio_flags & EXTENT_BIO_COMPRESSED) {
  1291. return btrfs_submit_compressed_read(inode, bio,
  1292. mirror_num, bio_flags);
  1293. } else if (!skip_sum)
  1294. btrfs_lookup_bio_sums(root, inode, bio, NULL);
  1295. goto mapit;
  1296. } else if (!skip_sum) {
  1297. /* csum items have already been cloned */
  1298. if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
  1299. goto mapit;
  1300. /* we're doing a write, do the async checksumming */
  1301. return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
  1302. inode, rw, bio, mirror_num,
  1303. bio_flags, bio_offset,
  1304. __btrfs_submit_bio_start,
  1305. __btrfs_submit_bio_done);
  1306. }
  1307. mapit:
  1308. return btrfs_map_bio(root, rw, bio, mirror_num, 0);
  1309. }
  1310. /*
  1311. * given a list of ordered sums record them in the inode. This happens
  1312. * at IO completion time based on sums calculated at bio submission time.
  1313. */
  1314. static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
  1315. struct inode *inode, u64 file_offset,
  1316. struct list_head *list)
  1317. {
  1318. struct btrfs_ordered_sum *sum;
  1319. btrfs_set_trans_block_group(trans, inode);
  1320. list_for_each_entry(sum, list, list) {
  1321. btrfs_csum_file_blocks(trans,
  1322. BTRFS_I(inode)->root->fs_info->csum_root, sum);
  1323. }
  1324. return 0;
  1325. }
  1326. int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
  1327. struct extent_state **cached_state)
  1328. {
  1329. if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
  1330. WARN_ON(1);
  1331. return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
  1332. cached_state, GFP_NOFS);
  1333. }
  1334. /* see btrfs_writepage_start_hook for details on why this is required */
  1335. struct btrfs_writepage_fixup {
  1336. struct page *page;
  1337. struct btrfs_work work;
  1338. };
  1339. static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
  1340. {
  1341. struct btrfs_writepage_fixup *fixup;
  1342. struct btrfs_ordered_extent *ordered;
  1343. struct extent_state *cached_state = NULL;
  1344. struct page *page;
  1345. struct inode *inode;
  1346. u64 page_start;
  1347. u64 page_end;
  1348. fixup = container_of(work, struct btrfs_writepage_fixup, work);
  1349. page = fixup->page;
  1350. again:
  1351. lock_page(page);
  1352. if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
  1353. ClearPageChecked(page);
  1354. goto out_page;
  1355. }
  1356. inode = page->mapping->host;
  1357. page_start = page_offset(page);
  1358. page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
  1359. lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
  1360. &cached_state, GFP_NOFS);
  1361. /* already ordered? We're done */
  1362. if (PagePrivate2(page))
  1363. goto out;
  1364. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  1365. if (ordered) {
  1366. unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
  1367. page_end, &cached_state, GFP_NOFS);
  1368. unlock_page(page);
  1369. btrfs_start_ordered_extent(inode, ordered, 1);
  1370. goto again;
  1371. }
  1372. BUG();
  1373. btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
  1374. ClearPageChecked(page);
  1375. out:
  1376. unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
  1377. &cached_state, GFP_NOFS);
  1378. out_page:
  1379. unlock_page(page);
  1380. page_cache_release(page);
  1381. }
  1382. /*
  1383. * There are a few paths in the higher layers of the kernel that directly
  1384. * set the page dirty bit without asking the filesystem if it is a
  1385. * good idea. This causes problems because we want to make sure COW
  1386. * properly happens and the data=ordered rules are followed.
  1387. *
  1388. * In our case any range that doesn't have the ORDERED bit set
  1389. * hasn't been properly setup for IO. We kick off an async process
  1390. * to fix it up. The async helper will wait for ordered extents, set
  1391. * the delalloc bit and make it safe to write the page.
  1392. */
  1393. static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
  1394. {
  1395. struct inode *inode = page->mapping->host;
  1396. struct btrfs_writepage_fixup *fixup;
  1397. struct btrfs_root *root = BTRFS_I(inode)->root;
  1398. /* this page is properly in the ordered list */
  1399. if (TestClearPagePrivate2(page))
  1400. return 0;
  1401. if (PageChecked(page))
  1402. return -EAGAIN;
  1403. fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
  1404. if (!fixup)
  1405. return -EAGAIN;
  1406. SetPageChecked(page);
  1407. page_cache_get(page);
  1408. fixup->work.func = btrfs_writepage_fixup_worker;
  1409. fixup->page = page;
  1410. btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
  1411. return -EAGAIN;
  1412. }
  1413. static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
  1414. struct inode *inode, u64 file_pos,
  1415. u64 disk_bytenr, u64 disk_num_bytes,
  1416. u64 num_bytes, u64 ram_bytes,
  1417. u8 compression, u8 encryption,
  1418. u16 other_encoding, int extent_type)
  1419. {
  1420. struct btrfs_root *root = BTRFS_I(inode)->root;
  1421. struct btrfs_file_extent_item *fi;
  1422. struct btrfs_path *path;
  1423. struct extent_buffer *leaf;
  1424. struct btrfs_key ins;
  1425. u64 hint;
  1426. int ret;
  1427. path = btrfs_alloc_path();
  1428. BUG_ON(!path);
  1429. path->leave_spinning = 1;
  1430. /*
  1431. * we may be replacing one extent in the tree with another.
  1432. * The new extent is pinned in the extent map, and we don't want
  1433. * to drop it from the cache until it is completely in the btree.
  1434. *
  1435. * So, tell btrfs_drop_extents to leave this extent in the cache.
  1436. * the caller is expected to unpin it and allow it to be merged
  1437. * with the others.
  1438. */
  1439. ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
  1440. &hint, 0);
  1441. BUG_ON(ret);
  1442. ins.objectid = inode->i_ino;
  1443. ins.offset = file_pos;
  1444. ins.type = BTRFS_EXTENT_DATA_KEY;
  1445. ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
  1446. BUG_ON(ret);
  1447. leaf = path->nodes[0];
  1448. fi = btrfs_item_ptr(leaf, path->slots[0],
  1449. struct btrfs_file_extent_item);
  1450. btrfs_set_file_extent_generation(leaf, fi, trans->transid);
  1451. btrfs_set_file_extent_type(leaf, fi, extent_type);
  1452. btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
  1453. btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
  1454. btrfs_set_file_extent_offset(leaf, fi, 0);
  1455. btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
  1456. btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
  1457. btrfs_set_file_extent_compression(leaf, fi, compression);
  1458. btrfs_set_file_extent_encryption(leaf, fi, encryption);
  1459. btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
  1460. btrfs_unlock_up_safe(path, 1);
  1461. btrfs_set_lock_blocking(leaf);
  1462. btrfs_mark_buffer_dirty(leaf);
  1463. inode_add_bytes(inode, num_bytes);
  1464. ins.objectid = disk_bytenr;
  1465. ins.offset = disk_num_bytes;
  1466. ins.type = BTRFS_EXTENT_ITEM_KEY;
  1467. ret = btrfs_alloc_reserved_file_extent(trans, root,
  1468. root->root_key.objectid,
  1469. inode->i_ino, file_pos, &ins);
  1470. BUG_ON(ret);
  1471. btrfs_free_path(path);
  1472. return 0;
  1473. }
  1474. /*
  1475. * helper function for btrfs_finish_ordered_io, this
  1476. * just reads in some of the csum leaves to prime them into ram
  1477. * before we start the transaction. It limits the amount of btree
  1478. * reads required while inside the transaction.
  1479. */
  1480. /* as ordered data IO finishes, this gets called so we can finish
  1481. * an ordered extent if the range of bytes in the file it covers are
  1482. * fully written.
  1483. */
  1484. static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
  1485. {
  1486. struct btrfs_root *root = BTRFS_I(inode)->root;
  1487. struct btrfs_trans_handle *trans = NULL;
  1488. struct btrfs_ordered_extent *ordered_extent = NULL;
  1489. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  1490. struct extent_state *cached_state = NULL;
  1491. int compressed = 0;
  1492. int ret;
  1493. bool nolock = false;
  1494. ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
  1495. end - start + 1);
  1496. if (!ret)
  1497. return 0;
  1498. BUG_ON(!ordered_extent);
  1499. nolock = (root == root->fs_info->tree_root);
  1500. if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
  1501. BUG_ON(!list_empty(&ordered_extent->list));
  1502. ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
  1503. if (!ret) {
  1504. if (nolock)
  1505. trans = btrfs_join_transaction_nolock(root, 1);
  1506. else
  1507. trans = btrfs_join_transaction(root, 1);
  1508. BUG_ON(!trans);
  1509. btrfs_set_trans_block_group(trans, inode);
  1510. trans->block_rsv = &root->fs_info->delalloc_block_rsv;
  1511. ret = btrfs_update_inode(trans, root, inode);
  1512. BUG_ON(ret);
  1513. }
  1514. goto out;
  1515. }
  1516. lock_extent_bits(io_tree, ordered_extent->file_offset,
  1517. ordered_extent->file_offset + ordered_extent->len - 1,
  1518. 0, &cached_state, GFP_NOFS);
  1519. if (nolock)
  1520. trans = btrfs_join_transaction_nolock(root, 1);
  1521. else
  1522. trans = btrfs_join_transaction(root, 1);
  1523. btrfs_set_trans_block_group(trans, inode);
  1524. trans->block_rsv = &root->fs_info->delalloc_block_rsv;
  1525. if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
  1526. compressed = 1;
  1527. if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
  1528. BUG_ON(compressed);
  1529. ret = btrfs_mark_extent_written(trans, inode,
  1530. ordered_extent->file_offset,
  1531. ordered_extent->file_offset +
  1532. ordered_extent->len);
  1533. BUG_ON(ret);
  1534. } else {
  1535. BUG_ON(root == root->fs_info->tree_root);
  1536. ret = insert_reserved_file_extent(trans, inode,
  1537. ordered_extent->file_offset,
  1538. ordered_extent->start,
  1539. ordered_extent->disk_len,
  1540. ordered_extent->len,
  1541. ordered_extent->len,
  1542. compressed, 0, 0,
  1543. BTRFS_FILE_EXTENT_REG);
  1544. unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
  1545. ordered_extent->file_offset,
  1546. ordered_extent->len);
  1547. BUG_ON(ret);
  1548. }
  1549. unlock_extent_cached(io_tree, ordered_extent->file_offset,
  1550. ordered_extent->file_offset +
  1551. ordered_extent->len - 1, &cached_state, GFP_NOFS);
  1552. add_pending_csums(trans, inode, ordered_extent->file_offset,
  1553. &ordered_extent->list);
  1554. btrfs_ordered_update_i_size(inode, 0, ordered_extent);
  1555. ret = btrfs_update_inode(trans, root, inode);
  1556. BUG_ON(ret);
  1557. out:
  1558. if (nolock) {
  1559. if (trans)
  1560. btrfs_end_transaction_nolock(trans, root);
  1561. } else {
  1562. btrfs_delalloc_release_metadata(inode, ordered_extent->len);
  1563. if (trans)
  1564. btrfs_end_transaction(trans, root);
  1565. }
  1566. /* once for us */
  1567. btrfs_put_ordered_extent(ordered_extent);
  1568. /* once for the tree */
  1569. btrfs_put_ordered_extent(ordered_extent);
  1570. return 0;
  1571. }
  1572. static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
  1573. struct extent_state *state, int uptodate)
  1574. {
  1575. ClearPagePrivate2(page);
  1576. return btrfs_finish_ordered_io(page->mapping->host, start, end);
  1577. }
  1578. /*
  1579. * When IO fails, either with EIO or csum verification fails, we
  1580. * try other mirrors that might have a good copy of the data. This
  1581. * io_failure_record is used to record state as we go through all the
  1582. * mirrors. If another mirror has good data, the page is set up to date
  1583. * and things continue. If a good mirror can't be found, the original
  1584. * bio end_io callback is called to indicate things have failed.
  1585. */
  1586. struct io_failure_record {
  1587. struct page *page;
  1588. u64 start;
  1589. u64 len;
  1590. u64 logical;
  1591. unsigned long bio_flags;
  1592. int last_mirror;
  1593. };
  1594. static int btrfs_io_failed_hook(struct bio *failed_bio,
  1595. struct page *page, u64 start, u64 end,
  1596. struct extent_state *state)
  1597. {
  1598. struct io_failure_record *failrec = NULL;
  1599. u64 private;
  1600. struct extent_map *em;
  1601. struct inode *inode = page->mapping->host;
  1602. struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
  1603. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  1604. struct bio *bio;
  1605. int num_copies;
  1606. int ret;
  1607. int rw;
  1608. u64 logical;
  1609. ret = get_state_private(failure_tree, start, &private);
  1610. if (ret) {
  1611. failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
  1612. if (!failrec)
  1613. return -ENOMEM;
  1614. failrec->start = start;
  1615. failrec->len = end - start + 1;
  1616. failrec->last_mirror = 0;
  1617. failrec->bio_flags = 0;
  1618. read_lock(&em_tree->lock);
  1619. em = lookup_extent_mapping(em_tree, start, failrec->len);
  1620. if (em->start > start || em->start + em->len < start) {
  1621. free_extent_map(em);
  1622. em = NULL;
  1623. }
  1624. read_unlock(&em_tree->lock);
  1625. if (!em || IS_ERR(em)) {
  1626. kfree(failrec);
  1627. return -EIO;
  1628. }
  1629. logical = start - em->start;
  1630. logical = em->block_start + logical;
  1631. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  1632. logical = em->block_start;
  1633. failrec->bio_flags = EXTENT_BIO_COMPRESSED;
  1634. }
  1635. failrec->logical = logical;
  1636. free_extent_map(em);
  1637. set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
  1638. EXTENT_DIRTY, GFP_NOFS);
  1639. set_state_private(failure_tree, start,
  1640. (u64)(unsigned long)failrec);
  1641. } else {
  1642. failrec = (struct io_failure_record *)(unsigned long)private;
  1643. }
  1644. num_copies = btrfs_num_copies(
  1645. &BTRFS_I(inode)->root->fs_info->mapping_tree,
  1646. failrec->logical, failrec->len);
  1647. failrec->last_mirror++;
  1648. if (!state) {
  1649. spin_lock(&BTRFS_I(inode)->io_tree.lock);
  1650. state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
  1651. failrec->start,
  1652. EXTENT_LOCKED);
  1653. if (state && state->start != failrec->start)
  1654. state = NULL;
  1655. spin_unlock(&BTRFS_I(inode)->io_tree.lock);
  1656. }
  1657. if (!state || failrec->last_mirror > num_copies) {
  1658. set_state_private(failure_tree, failrec->start, 0);
  1659. clear_extent_bits(failure_tree, failrec->start,
  1660. failrec->start + failrec->len - 1,
  1661. EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
  1662. kfree(failrec);
  1663. return -EIO;
  1664. }
  1665. bio = bio_alloc(GFP_NOFS, 1);
  1666. bio->bi_private = state;
  1667. bio->bi_end_io = failed_bio->bi_end_io;
  1668. bio->bi_sector = failrec->logical >> 9;
  1669. bio->bi_bdev = failed_bio->bi_bdev;
  1670. bio->bi_size = 0;
  1671. bio_add_page(bio, page, failrec->len, start - page_offset(page));
  1672. if (failed_bio->bi_rw & REQ_WRITE)
  1673. rw = WRITE;
  1674. else
  1675. rw = READ;
  1676. BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
  1677. failrec->last_mirror,
  1678. failrec->bio_flags, 0);
  1679. return 0;
  1680. }
  1681. /*
  1682. * each time an IO finishes, we do a fast check in the IO failure tree
  1683. * to see if we need to process or clean up an io_failure_record
  1684. */
  1685. static int btrfs_clean_io_failures(struct inode *inode, u64 start)
  1686. {
  1687. u64 private;
  1688. u64 private_failure;
  1689. struct io_failure_record *failure;
  1690. int ret;
  1691. private = 0;
  1692. if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
  1693. (u64)-1, 1, EXTENT_DIRTY)) {
  1694. ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
  1695. start, &private_failure);
  1696. if (ret == 0) {
  1697. failure = (struct io_failure_record *)(unsigned long)
  1698. private_failure;
  1699. set_state_private(&BTRFS_I(inode)->io_failure_tree,
  1700. failure->start, 0);
  1701. clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
  1702. failure->start,
  1703. failure->start + failure->len - 1,
  1704. EXTENT_DIRTY | EXTENT_LOCKED,
  1705. GFP_NOFS);
  1706. kfree(failure);
  1707. }
  1708. }
  1709. return 0;
  1710. }
  1711. /*
  1712. * when reads are done, we need to check csums to verify the data is correct
  1713. * if there's a match, we allow the bio to finish. If not, we go through
  1714. * the io_failure_record routines to find good copies
  1715. */
  1716. static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
  1717. struct extent_state *state)
  1718. {
  1719. size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
  1720. struct inode *inode = page->mapping->host;
  1721. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  1722. char *kaddr;
  1723. u64 private = ~(u32)0;
  1724. int ret;
  1725. struct btrfs_root *root = BTRFS_I(inode)->root;
  1726. u32 csum = ~(u32)0;
  1727. if (PageChecked(page)) {
  1728. ClearPageChecked(page);
  1729. goto good;
  1730. }
  1731. if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
  1732. return 0;
  1733. if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
  1734. test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
  1735. clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
  1736. GFP_NOFS);
  1737. return 0;
  1738. }
  1739. if (state && state->start == start) {
  1740. private = state->private;
  1741. ret = 0;
  1742. } else {
  1743. ret = get_state_private(io_tree, start, &private);
  1744. }
  1745. kaddr = kmap_atomic(page, KM_USER0);
  1746. if (ret)
  1747. goto zeroit;
  1748. csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
  1749. btrfs_csum_final(csum, (char *)&csum);
  1750. if (csum != private)
  1751. goto zeroit;
  1752. kunmap_atomic(kaddr, KM_USER0);
  1753. good:
  1754. /* if the io failure tree for this inode is non-empty,
  1755. * check to see if we've recovered from a failed IO
  1756. */
  1757. btrfs_clean_io_failures(inode, start);
  1758. return 0;
  1759. zeroit:
  1760. if (printk_ratelimit()) {
  1761. printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
  1762. "private %llu\n", page->mapping->host->i_ino,
  1763. (unsigned long long)start, csum,
  1764. (unsigned long long)private);
  1765. }
  1766. memset(kaddr + offset, 1, end - start + 1);
  1767. flush_dcache_page(page);
  1768. kunmap_atomic(kaddr, KM_USER0);
  1769. if (private == 0)
  1770. return 0;
  1771. return -EIO;
  1772. }
  1773. struct delayed_iput {
  1774. struct list_head list;
  1775. struct inode *inode;
  1776. };
  1777. void btrfs_add_delayed_iput(struct inode *inode)
  1778. {
  1779. struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
  1780. struct delayed_iput *delayed;
  1781. if (atomic_add_unless(&inode->i_count, -1, 1))
  1782. return;
  1783. delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
  1784. delayed->inode = inode;
  1785. spin_lock(&fs_info->delayed_iput_lock);
  1786. list_add_tail(&delayed->list, &fs_info->delayed_iputs);
  1787. spin_unlock(&fs_info->delayed_iput_lock);
  1788. }
  1789. void btrfs_run_delayed_iputs(struct btrfs_root *root)
  1790. {
  1791. LIST_HEAD(list);
  1792. struct btrfs_fs_info *fs_info = root->fs_info;
  1793. struct delayed_iput *delayed;
  1794. int empty;
  1795. spin_lock(&fs_info->delayed_iput_lock);
  1796. empty = list_empty(&fs_info->delayed_iputs);
  1797. spin_unlock(&fs_info->delayed_iput_lock);
  1798. if (empty)
  1799. return;
  1800. down_read(&root->fs_info->cleanup_work_sem);
  1801. spin_lock(&fs_info->delayed_iput_lock);
  1802. list_splice_init(&fs_info->delayed_iputs, &list);
  1803. spin_unlock(&fs_info->delayed_iput_lock);
  1804. while (!list_empty(&list)) {
  1805. delayed = list_entry(list.next, struct delayed_iput, list);
  1806. list_del(&delayed->list);
  1807. iput(delayed->inode);
  1808. kfree(delayed);
  1809. }
  1810. up_read(&root->fs_info->cleanup_work_sem);
  1811. }
  1812. /*
  1813. * calculate extra metadata reservation when snapshotting a subvolume
  1814. * contains orphan files.
  1815. */
  1816. void btrfs_orphan_pre_snapshot(struct btrfs_trans_handle *trans,
  1817. struct btrfs_pending_snapshot *pending,
  1818. u64 *bytes_to_reserve)
  1819. {
  1820. struct btrfs_root *root;
  1821. struct btrfs_block_rsv *block_rsv;
  1822. u64 num_bytes;
  1823. int index;
  1824. root = pending->root;
  1825. if (!root->orphan_block_rsv || list_empty(&root->orphan_list))
  1826. return;
  1827. block_rsv = root->orphan_block_rsv;
  1828. /* orphan block reservation for the snapshot */
  1829. num_bytes = block_rsv->size;
  1830. /*
  1831. * after the snapshot is created, COWing tree blocks may use more
  1832. * space than it frees. So we should make sure there is enough
  1833. * reserved space.
  1834. */
  1835. index = trans->transid & 0x1;
  1836. if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) {
  1837. num_bytes += block_rsv->size -
  1838. (block_rsv->reserved + block_rsv->freed[index]);
  1839. }
  1840. *bytes_to_reserve += num_bytes;
  1841. }
  1842. void btrfs_orphan_post_snapshot(struct btrfs_trans_handle *trans,
  1843. struct btrfs_pending_snapshot *pending)
  1844. {
  1845. struct btrfs_root *root = pending->root;
  1846. struct btrfs_root *snap = pending->snap;
  1847. struct btrfs_block_rsv *block_rsv;
  1848. u64 num_bytes;
  1849. int index;
  1850. int ret;
  1851. if (!root->orphan_block_rsv || list_empty(&root->orphan_list))
  1852. return;
  1853. /* refill source subvolume's orphan block reservation */
  1854. block_rsv = root->orphan_block_rsv;
  1855. index = trans->transid & 0x1;
  1856. if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) {
  1857. num_bytes = block_rsv->size -
  1858. (block_rsv->reserved + block_rsv->freed[index]);
  1859. ret = btrfs_block_rsv_migrate(&pending->block_rsv,
  1860. root->orphan_block_rsv,
  1861. num_bytes);
  1862. BUG_ON(ret);
  1863. }
  1864. /* setup orphan block reservation for the snapshot */
  1865. block_rsv = btrfs_alloc_block_rsv(snap);
  1866. BUG_ON(!block_rsv);
  1867. btrfs_add_durable_block_rsv(root->fs_info, block_rsv);
  1868. snap->orphan_block_rsv = block_rsv;
  1869. num_bytes = root->orphan_block_rsv->size;
  1870. ret = btrfs_block_rsv_migrate(&pending->block_rsv,
  1871. block_rsv, num_bytes);
  1872. BUG_ON(ret);
  1873. #if 0
  1874. /* insert orphan item for the snapshot */
  1875. WARN_ON(!root->orphan_item_inserted);
  1876. ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
  1877. snap->root_key.objectid);
  1878. BUG_ON(ret);
  1879. snap->orphan_item_inserted = 1;
  1880. #endif
  1881. }
  1882. enum btrfs_orphan_cleanup_state {
  1883. ORPHAN_CLEANUP_STARTED = 1,
  1884. ORPHAN_CLEANUP_DONE = 2,
  1885. };
  1886. /*
  1887. * This is called in transaction commmit time. If there are no orphan
  1888. * files in the subvolume, it removes orphan item and frees block_rsv
  1889. * structure.
  1890. */
  1891. void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
  1892. struct btrfs_root *root)
  1893. {
  1894. int ret;
  1895. if (!list_empty(&root->orphan_list) ||
  1896. root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
  1897. return;
  1898. if (root->orphan_item_inserted &&
  1899. btrfs_root_refs(&root->root_item) > 0) {
  1900. ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
  1901. root->root_key.objectid);
  1902. BUG_ON(ret);
  1903. root->orphan_item_inserted = 0;
  1904. }
  1905. if (root->orphan_block_rsv) {
  1906. WARN_ON(root->orphan_block_rsv->size > 0);
  1907. btrfs_free_block_rsv(root, root->orphan_block_rsv);
  1908. root->orphan_block_rsv = NULL;
  1909. }
  1910. }
  1911. /*
  1912. * This creates an orphan entry for the given inode in case something goes
  1913. * wrong in the middle of an unlink/truncate.
  1914. *
  1915. * NOTE: caller of this function should reserve 5 units of metadata for
  1916. * this function.
  1917. */
  1918. int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
  1919. {
  1920. struct btrfs_root *root = BTRFS_I(inode)->root;
  1921. struct btrfs_block_rsv *block_rsv = NULL;
  1922. int reserve = 0;
  1923. int insert = 0;
  1924. int ret;
  1925. if (!root->orphan_block_rsv) {
  1926. block_rsv = btrfs_alloc_block_rsv(root);
  1927. BUG_ON(!block_rsv);
  1928. }
  1929. spin_lock(&root->orphan_lock);
  1930. if (!root->orphan_block_rsv) {
  1931. root->orphan_block_rsv = block_rsv;
  1932. } else if (block_rsv) {
  1933. btrfs_free_block_rsv(root, block_rsv);
  1934. block_rsv = NULL;
  1935. }
  1936. if (list_empty(&BTRFS_I(inode)->i_orphan)) {
  1937. list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
  1938. #if 0
  1939. /*
  1940. * For proper ENOSPC handling, we should do orphan
  1941. * cleanup when mounting. But this introduces backward
  1942. * compatibility issue.
  1943. */
  1944. if (!xchg(&root->orphan_item_inserted, 1))
  1945. insert = 2;
  1946. else
  1947. insert = 1;
  1948. #endif
  1949. insert = 1;
  1950. } else {
  1951. WARN_ON(!BTRFS_I(inode)->orphan_meta_reserved);
  1952. }
  1953. if (!BTRFS_I(inode)->orphan_meta_reserved) {
  1954. BTRFS_I(inode)->orphan_meta_reserved = 1;
  1955. reserve = 1;
  1956. }
  1957. spin_unlock(&root->orphan_lock);
  1958. if (block_rsv)
  1959. btrfs_add_durable_block_rsv(root->fs_info, block_rsv);
  1960. /* grab metadata reservation from transaction handle */
  1961. if (reserve) {
  1962. ret = btrfs_orphan_reserve_metadata(trans, inode);
  1963. BUG_ON(ret);
  1964. }
  1965. /* insert an orphan item to track this unlinked/truncated file */
  1966. if (insert >= 1) {
  1967. ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
  1968. BUG_ON(ret);
  1969. }
  1970. /* insert an orphan item to track subvolume contains orphan files */
  1971. if (insert >= 2) {
  1972. ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
  1973. root->root_key.objectid);
  1974. BUG_ON(ret);
  1975. }
  1976. return 0;
  1977. }
  1978. /*
  1979. * We have done the truncate/delete so we can go ahead and remove the orphan
  1980. * item for this particular inode.
  1981. */
  1982. int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
  1983. {
  1984. struct btrfs_root *root = BTRFS_I(inode)->root;
  1985. int delete_item = 0;
  1986. int release_rsv = 0;
  1987. int ret = 0;
  1988. spin_lock(&root->orphan_lock);
  1989. if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
  1990. list_del_init(&BTRFS_I(inode)->i_orphan);
  1991. delete_item = 1;
  1992. }
  1993. if (BTRFS_I(inode)->orphan_meta_reserved) {
  1994. BTRFS_I(inode)->orphan_meta_reserved = 0;
  1995. release_rsv = 1;
  1996. }
  1997. spin_unlock(&root->orphan_lock);
  1998. if (trans && delete_item) {
  1999. ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
  2000. BUG_ON(ret);
  2001. }
  2002. if (release_rsv)
  2003. btrfs_orphan_release_metadata(inode);
  2004. return 0;
  2005. }
  2006. /*
  2007. * this cleans up any orphans that may be left on the list from the last use
  2008. * of this root.
  2009. */
  2010. void btrfs_orphan_cleanup(struct btrfs_root *root)
  2011. {
  2012. struct btrfs_path *path;
  2013. struct extent_buffer *leaf;
  2014. struct btrfs_key key, found_key;
  2015. struct btrfs_trans_handle *trans;
  2016. struct inode *inode;
  2017. int ret = 0, nr_unlink = 0, nr_truncate = 0;
  2018. if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
  2019. return;
  2020. path = btrfs_alloc_path();
  2021. BUG_ON(!path);
  2022. path->reada = -1;
  2023. key.objectid = BTRFS_ORPHAN_OBJECTID;
  2024. btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
  2025. key.offset = (u64)-1;
  2026. while (1) {
  2027. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2028. if (ret < 0) {
  2029. printk(KERN_ERR "Error searching slot for orphan: %d"
  2030. "\n", ret);
  2031. break;
  2032. }
  2033. /*
  2034. * if ret == 0 means we found what we were searching for, which
  2035. * is weird, but possible, so only screw with path if we didnt
  2036. * find the key and see if we have stuff that matches
  2037. */
  2038. if (ret > 0) {
  2039. if (path->slots[0] == 0)
  2040. break;
  2041. path->slots[0]--;
  2042. }
  2043. /* pull out the item */
  2044. leaf = path->nodes[0];
  2045. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2046. /* make sure the item matches what we want */
  2047. if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
  2048. break;
  2049. if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
  2050. break;
  2051. /* release the path since we're done with it */
  2052. btrfs_release_path(root, path);
  2053. /*
  2054. * this is where we are basically btrfs_lookup, without the
  2055. * crossing root thing. we store the inode number in the
  2056. * offset of the orphan item.
  2057. */
  2058. found_key.objectid = found_key.offset;
  2059. found_key.type = BTRFS_INODE_ITEM_KEY;
  2060. found_key.offset = 0;
  2061. inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
  2062. BUG_ON(IS_ERR(inode));
  2063. /*
  2064. * add this inode to the orphan list so btrfs_orphan_del does
  2065. * the proper thing when we hit it
  2066. */
  2067. spin_lock(&root->orphan_lock);
  2068. list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
  2069. spin_unlock(&root->orphan_lock);
  2070. /*
  2071. * if this is a bad inode, means we actually succeeded in
  2072. * removing the inode, but not the orphan record, which means
  2073. * we need to manually delete the orphan since iput will just
  2074. * do a destroy_inode
  2075. */
  2076. if (is_bad_inode(inode)) {
  2077. trans = btrfs_start_transaction(root, 0);
  2078. btrfs_orphan_del(trans, inode);
  2079. btrfs_end_transaction(trans, root);
  2080. iput(inode);
  2081. continue;
  2082. }
  2083. /* if we have links, this was a truncate, lets do that */
  2084. if (inode->i_nlink) {
  2085. nr_truncate++;
  2086. btrfs_truncate(inode);
  2087. } else {
  2088. nr_unlink++;
  2089. }
  2090. /* this will do delete_inode and everything for us */
  2091. iput(inode);
  2092. }
  2093. btrfs_free_path(path);
  2094. root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
  2095. if (root->orphan_block_rsv)
  2096. btrfs_block_rsv_release(root, root->orphan_block_rsv,
  2097. (u64)-1);
  2098. if (root->orphan_block_rsv || root->orphan_item_inserted) {
  2099. trans = btrfs_join_transaction(root, 1);
  2100. btrfs_end_transaction(trans, root);
  2101. }
  2102. if (nr_unlink)
  2103. printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
  2104. if (nr_truncate)
  2105. printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
  2106. }
  2107. /*
  2108. * very simple check to peek ahead in the leaf looking for xattrs. If we
  2109. * don't find any xattrs, we know there can't be any acls.
  2110. *
  2111. * slot is the slot the inode is in, objectid is the objectid of the inode
  2112. */
  2113. static noinline int acls_after_inode_item(struct extent_buffer *leaf,
  2114. int slot, u64 objectid)
  2115. {
  2116. u32 nritems = btrfs_header_nritems(leaf);
  2117. struct btrfs_key found_key;
  2118. int scanned = 0;
  2119. slot++;
  2120. while (slot < nritems) {
  2121. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  2122. /* we found a different objectid, there must not be acls */
  2123. if (found_key.objectid != objectid)
  2124. return 0;
  2125. /* we found an xattr, assume we've got an acl */
  2126. if (found_key.type == BTRFS_XATTR_ITEM_KEY)
  2127. return 1;
  2128. /*
  2129. * we found a key greater than an xattr key, there can't
  2130. * be any acls later on
  2131. */
  2132. if (found_key.type > BTRFS_XATTR_ITEM_KEY)
  2133. return 0;
  2134. slot++;
  2135. scanned++;
  2136. /*
  2137. * it goes inode, inode backrefs, xattrs, extents,
  2138. * so if there are a ton of hard links to an inode there can
  2139. * be a lot of backrefs. Don't waste time searching too hard,
  2140. * this is just an optimization
  2141. */
  2142. if (scanned >= 8)
  2143. break;
  2144. }
  2145. /* we hit the end of the leaf before we found an xattr or
  2146. * something larger than an xattr. We have to assume the inode
  2147. * has acls
  2148. */
  2149. return 1;
  2150. }
  2151. /*
  2152. * read an inode from the btree into the in-memory inode
  2153. */
  2154. static void btrfs_read_locked_inode(struct inode *inode)
  2155. {
  2156. struct btrfs_path *path;
  2157. struct extent_buffer *leaf;
  2158. struct btrfs_inode_item *inode_item;
  2159. struct btrfs_timespec *tspec;
  2160. struct btrfs_root *root = BTRFS_I(inode)->root;
  2161. struct btrfs_key location;
  2162. int maybe_acls;
  2163. u64 alloc_group_block;
  2164. u32 rdev;
  2165. int ret;
  2166. path = btrfs_alloc_path();
  2167. BUG_ON(!path);
  2168. memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
  2169. ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
  2170. if (ret)
  2171. goto make_bad;
  2172. leaf = path->nodes[0];
  2173. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  2174. struct btrfs_inode_item);
  2175. inode->i_mode = btrfs_inode_mode(leaf, inode_item);
  2176. inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
  2177. inode->i_uid = btrfs_inode_uid(leaf, inode_item);
  2178. inode->i_gid = btrfs_inode_gid(leaf, inode_item);
  2179. btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
  2180. tspec = btrfs_inode_atime(inode_item);
  2181. inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  2182. inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  2183. tspec = btrfs_inode_mtime(inode_item);
  2184. inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  2185. inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  2186. tspec = btrfs_inode_ctime(inode_item);
  2187. inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  2188. inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  2189. inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
  2190. BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
  2191. BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
  2192. inode->i_generation = BTRFS_I(inode)->generation;
  2193. inode->i_rdev = 0;
  2194. rdev = btrfs_inode_rdev(leaf, inode_item);
  2195. BTRFS_I(inode)->index_cnt = (u64)-1;
  2196. BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
  2197. alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
  2198. /*
  2199. * try to precache a NULL acl entry for files that don't have
  2200. * any xattrs or acls
  2201. */
  2202. maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino);
  2203. if (!maybe_acls)
  2204. cache_no_acl(inode);
  2205. BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
  2206. alloc_group_block, 0);
  2207. btrfs_free_path(path);
  2208. inode_item = NULL;
  2209. switch (inode->i_mode & S_IFMT) {
  2210. case S_IFREG:
  2211. inode->i_mapping->a_ops = &btrfs_aops;
  2212. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  2213. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  2214. inode->i_fop = &btrfs_file_operations;
  2215. inode->i_op = &btrfs_file_inode_operations;
  2216. break;
  2217. case S_IFDIR:
  2218. inode->i_fop = &btrfs_dir_file_operations;
  2219. if (root == root->fs_info->tree_root)
  2220. inode->i_op = &btrfs_dir_ro_inode_operations;
  2221. else
  2222. inode->i_op = &btrfs_dir_inode_operations;
  2223. break;
  2224. case S_IFLNK:
  2225. inode->i_op = &btrfs_symlink_inode_operations;
  2226. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  2227. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  2228. break;
  2229. default:
  2230. inode->i_op = &btrfs_special_inode_operations;
  2231. init_special_inode(inode, inode->i_mode, rdev);
  2232. break;
  2233. }
  2234. btrfs_update_iflags(inode);
  2235. return;
  2236. make_bad:
  2237. btrfs_free_path(path);
  2238. make_bad_inode(inode);
  2239. }
  2240. /*
  2241. * given a leaf and an inode, copy the inode fields into the leaf
  2242. */
  2243. static void fill_inode_item(struct btrfs_trans_handle *trans,
  2244. struct extent_buffer *leaf,
  2245. struct btrfs_inode_item *item,
  2246. struct inode *inode)
  2247. {
  2248. btrfs_set_inode_uid(leaf, item, inode->i_uid);
  2249. btrfs_set_inode_gid(leaf, item, inode->i_gid);
  2250. btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
  2251. btrfs_set_inode_mode(leaf, item, inode->i_mode);
  2252. btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
  2253. btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
  2254. inode->i_atime.tv_sec);
  2255. btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
  2256. inode->i_atime.tv_nsec);
  2257. btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
  2258. inode->i_mtime.tv_sec);
  2259. btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
  2260. inode->i_mtime.tv_nsec);
  2261. btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
  2262. inode->i_ctime.tv_sec);
  2263. btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
  2264. inode->i_ctime.tv_nsec);
  2265. btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
  2266. btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
  2267. btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
  2268. btrfs_set_inode_transid(leaf, item, trans->transid);
  2269. btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
  2270. btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
  2271. btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
  2272. }
  2273. /*
  2274. * copy everything in the in-memory inode into the btree.
  2275. */
  2276. noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
  2277. struct btrfs_root *root, struct inode *inode)
  2278. {
  2279. struct btrfs_inode_item *inode_item;
  2280. struct btrfs_path *path;
  2281. struct extent_buffer *leaf;
  2282. int ret;
  2283. path = btrfs_alloc_path();
  2284. BUG_ON(!path);
  2285. path->leave_spinning = 1;
  2286. ret = btrfs_lookup_inode(trans, root, path,
  2287. &BTRFS_I(inode)->location, 1);
  2288. if (ret) {
  2289. if (ret > 0)
  2290. ret = -ENOENT;
  2291. goto failed;
  2292. }
  2293. btrfs_unlock_up_safe(path, 1);
  2294. leaf = path->nodes[0];
  2295. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  2296. struct btrfs_inode_item);
  2297. fill_inode_item(trans, leaf, inode_item, inode);
  2298. btrfs_mark_buffer_dirty(leaf);
  2299. btrfs_set_inode_last_trans(trans, inode);
  2300. ret = 0;
  2301. failed:
  2302. btrfs_free_path(path);
  2303. return ret;
  2304. }
  2305. /*
  2306. * unlink helper that gets used here in inode.c and in the tree logging
  2307. * recovery code. It remove a link in a directory with a given name, and
  2308. * also drops the back refs in the inode to the directory
  2309. */
  2310. int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
  2311. struct btrfs_root *root,
  2312. struct inode *dir, struct inode *inode,
  2313. const char *name, int name_len)
  2314. {
  2315. struct btrfs_path *path;
  2316. int ret = 0;
  2317. struct extent_buffer *leaf;
  2318. struct btrfs_dir_item *di;
  2319. struct btrfs_key key;
  2320. u64 index;
  2321. path = btrfs_alloc_path();
  2322. if (!path) {
  2323. ret = -ENOMEM;
  2324. goto err;
  2325. }
  2326. path->leave_spinning = 1;
  2327. di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
  2328. name, name_len, -1);
  2329. if (IS_ERR(di)) {
  2330. ret = PTR_ERR(di);
  2331. goto err;
  2332. }
  2333. if (!di) {
  2334. ret = -ENOENT;
  2335. goto err;
  2336. }
  2337. leaf = path->nodes[0];
  2338. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  2339. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  2340. if (ret)
  2341. goto err;
  2342. btrfs_release_path(root, path);
  2343. ret = btrfs_del_inode_ref(trans, root, name, name_len,
  2344. inode->i_ino,
  2345. dir->i_ino, &index);
  2346. if (ret) {
  2347. printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
  2348. "inode %lu parent %lu\n", name_len, name,
  2349. inode->i_ino, dir->i_ino);
  2350. goto err;
  2351. }
  2352. di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
  2353. index, name, name_len, -1);
  2354. if (IS_ERR(di)) {
  2355. ret = PTR_ERR(di);
  2356. goto err;
  2357. }
  2358. if (!di) {
  2359. ret = -ENOENT;
  2360. goto err;
  2361. }
  2362. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  2363. btrfs_release_path(root, path);
  2364. ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
  2365. inode, dir->i_ino);
  2366. BUG_ON(ret != 0 && ret != -ENOENT);
  2367. ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
  2368. dir, index);
  2369. if (ret == -ENOENT)
  2370. ret = 0;
  2371. err:
  2372. btrfs_free_path(path);
  2373. if (ret)
  2374. goto out;
  2375. btrfs_i_size_write(dir, dir->i_size - name_len * 2);
  2376. inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  2377. btrfs_update_inode(trans, root, dir);
  2378. btrfs_drop_nlink(inode);
  2379. ret = btrfs_update_inode(trans, root, inode);
  2380. out:
  2381. return ret;
  2382. }
  2383. /* helper to check if there is any shared block in the path */
  2384. static int check_path_shared(struct btrfs_root *root,
  2385. struct btrfs_path *path)
  2386. {
  2387. struct extent_buffer *eb;
  2388. int level;
  2389. u64 refs = 1;
  2390. int uninitialized_var(ret);
  2391. for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
  2392. if (!path->nodes[level])
  2393. break;
  2394. eb = path->nodes[level];
  2395. if (!btrfs_block_can_be_shared(root, eb))
  2396. continue;
  2397. ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
  2398. &refs, NULL);
  2399. if (refs > 1)
  2400. return 1;
  2401. }
  2402. return ret; /* XXX callers? */
  2403. }
  2404. /*
  2405. * helper to start transaction for unlink and rmdir.
  2406. *
  2407. * unlink and rmdir are special in btrfs, they do not always free space.
  2408. * so in enospc case, we should make sure they will free space before
  2409. * allowing them to use the global metadata reservation.
  2410. */
  2411. static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
  2412. struct dentry *dentry)
  2413. {
  2414. struct btrfs_trans_handle *trans;
  2415. struct btrfs_root *root = BTRFS_I(dir)->root;
  2416. struct btrfs_path *path;
  2417. struct btrfs_inode_ref *ref;
  2418. struct btrfs_dir_item *di;
  2419. struct inode *inode = dentry->d_inode;
  2420. u64 index;
  2421. int check_link = 1;
  2422. int err = -ENOSPC;
  2423. int ret;
  2424. trans = btrfs_start_transaction(root, 10);
  2425. if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
  2426. return trans;
  2427. if (inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
  2428. return ERR_PTR(-ENOSPC);
  2429. /* check if there is someone else holds reference */
  2430. if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
  2431. return ERR_PTR(-ENOSPC);
  2432. if (atomic_read(&inode->i_count) > 2)
  2433. return ERR_PTR(-ENOSPC);
  2434. if (xchg(&root->fs_info->enospc_unlink, 1))
  2435. return ERR_PTR(-ENOSPC);
  2436. path = btrfs_alloc_path();
  2437. if (!path) {
  2438. root->fs_info->enospc_unlink = 0;
  2439. return ERR_PTR(-ENOMEM);
  2440. }
  2441. trans = btrfs_start_transaction(root, 0);
  2442. if (IS_ERR(trans)) {
  2443. btrfs_free_path(path);
  2444. root->fs_info->enospc_unlink = 0;
  2445. return trans;
  2446. }
  2447. path->skip_locking = 1;
  2448. path->search_commit_root = 1;
  2449. ret = btrfs_lookup_inode(trans, root, path,
  2450. &BTRFS_I(dir)->location, 0);
  2451. if (ret < 0) {
  2452. err = ret;
  2453. goto out;
  2454. }
  2455. if (ret == 0) {
  2456. if (check_path_shared(root, path))
  2457. goto out;
  2458. } else {
  2459. check_link = 0;
  2460. }
  2461. btrfs_release_path(root, path);
  2462. ret = btrfs_lookup_inode(trans, root, path,
  2463. &BTRFS_I(inode)->location, 0);
  2464. if (ret < 0) {
  2465. err = ret;
  2466. goto out;
  2467. }
  2468. if (ret == 0) {
  2469. if (check_path_shared(root, path))
  2470. goto out;
  2471. } else {
  2472. check_link = 0;
  2473. }
  2474. btrfs_release_path(root, path);
  2475. if (ret == 0 && S_ISREG(inode->i_mode)) {
  2476. ret = btrfs_lookup_file_extent(trans, root, path,
  2477. inode->i_ino, (u64)-1, 0);
  2478. if (ret < 0) {
  2479. err = ret;
  2480. goto out;
  2481. }
  2482. BUG_ON(ret == 0);
  2483. if (check_path_shared(root, path))
  2484. goto out;
  2485. btrfs_release_path(root, path);
  2486. }
  2487. if (!check_link) {
  2488. err = 0;
  2489. goto out;
  2490. }
  2491. di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
  2492. dentry->d_name.name, dentry->d_name.len, 0);
  2493. if (IS_ERR(di)) {
  2494. err = PTR_ERR(di);
  2495. goto out;
  2496. }
  2497. if (di) {
  2498. if (check_path_shared(root, path))
  2499. goto out;
  2500. } else {
  2501. err = 0;
  2502. goto out;
  2503. }
  2504. btrfs_release_path(root, path);
  2505. ref = btrfs_lookup_inode_ref(trans, root, path,
  2506. dentry->d_name.name, dentry->d_name.len,
  2507. inode->i_ino, dir->i_ino, 0);
  2508. if (IS_ERR(ref)) {
  2509. err = PTR_ERR(ref);
  2510. goto out;
  2511. }
  2512. BUG_ON(!ref);
  2513. if (check_path_shared(root, path))
  2514. goto out;
  2515. index = btrfs_inode_ref_index(path->nodes[0], ref);
  2516. btrfs_release_path(root, path);
  2517. di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, index,
  2518. dentry->d_name.name, dentry->d_name.len, 0);
  2519. if (IS_ERR(di)) {
  2520. err = PTR_ERR(di);
  2521. goto out;
  2522. }
  2523. BUG_ON(ret == -ENOENT);
  2524. if (check_path_shared(root, path))
  2525. goto out;
  2526. err = 0;
  2527. out:
  2528. btrfs_free_path(path);
  2529. if (err) {
  2530. btrfs_end_transaction(trans, root);
  2531. root->fs_info->enospc_unlink = 0;
  2532. return ERR_PTR(err);
  2533. }
  2534. trans->block_rsv = &root->fs_info->global_block_rsv;
  2535. return trans;
  2536. }
  2537. static void __unlink_end_trans(struct btrfs_trans_handle *trans,
  2538. struct btrfs_root *root)
  2539. {
  2540. if (trans->block_rsv == &root->fs_info->global_block_rsv) {
  2541. BUG_ON(!root->fs_info->enospc_unlink);
  2542. root->fs_info->enospc_unlink = 0;
  2543. }
  2544. btrfs_end_transaction_throttle(trans, root);
  2545. }
  2546. static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
  2547. {
  2548. struct btrfs_root *root = BTRFS_I(dir)->root;
  2549. struct btrfs_trans_handle *trans;
  2550. struct inode *inode = dentry->d_inode;
  2551. int ret;
  2552. unsigned long nr = 0;
  2553. trans = __unlink_start_trans(dir, dentry);
  2554. if (IS_ERR(trans))
  2555. return PTR_ERR(trans);
  2556. btrfs_set_trans_block_group(trans, dir);
  2557. btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
  2558. ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
  2559. dentry->d_name.name, dentry->d_name.len);
  2560. BUG_ON(ret);
  2561. if (inode->i_nlink == 0) {
  2562. ret = btrfs_orphan_add(trans, inode);
  2563. BUG_ON(ret);
  2564. }
  2565. nr = trans->blocks_used;
  2566. __unlink_end_trans(trans, root);
  2567. btrfs_btree_balance_dirty(root, nr);
  2568. return ret;
  2569. }
  2570. int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
  2571. struct btrfs_root *root,
  2572. struct inode *dir, u64 objectid,
  2573. const char *name, int name_len)
  2574. {
  2575. struct btrfs_path *path;
  2576. struct extent_buffer *leaf;
  2577. struct btrfs_dir_item *di;
  2578. struct btrfs_key key;
  2579. u64 index;
  2580. int ret;
  2581. path = btrfs_alloc_path();
  2582. if (!path)
  2583. return -ENOMEM;
  2584. di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
  2585. name, name_len, -1);
  2586. BUG_ON(!di || IS_ERR(di));
  2587. leaf = path->nodes[0];
  2588. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  2589. WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
  2590. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  2591. BUG_ON(ret);
  2592. btrfs_release_path(root, path);
  2593. ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
  2594. objectid, root->root_key.objectid,
  2595. dir->i_ino, &index, name, name_len);
  2596. if (ret < 0) {
  2597. BUG_ON(ret != -ENOENT);
  2598. di = btrfs_search_dir_index_item(root, path, dir->i_ino,
  2599. name, name_len);
  2600. BUG_ON(!di || IS_ERR(di));
  2601. leaf = path->nodes[0];
  2602. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  2603. btrfs_release_path(root, path);
  2604. index = key.offset;
  2605. }
  2606. di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
  2607. index, name, name_len, -1);
  2608. BUG_ON(!di || IS_ERR(di));
  2609. leaf = path->nodes[0];
  2610. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  2611. WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
  2612. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  2613. BUG_ON(ret);
  2614. btrfs_release_path(root, path);
  2615. btrfs_i_size_write(dir, dir->i_size - name_len * 2);
  2616. dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  2617. ret = btrfs_update_inode(trans, root, dir);
  2618. BUG_ON(ret);
  2619. btrfs_free_path(path);
  2620. return 0;
  2621. }
  2622. static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
  2623. {
  2624. struct inode *inode = dentry->d_inode;
  2625. int err = 0;
  2626. struct btrfs_root *root = BTRFS_I(dir)->root;
  2627. struct btrfs_trans_handle *trans;
  2628. unsigned long nr = 0;
  2629. if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
  2630. inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
  2631. return -ENOTEMPTY;
  2632. trans = __unlink_start_trans(dir, dentry);
  2633. if (IS_ERR(trans))
  2634. return PTR_ERR(trans);
  2635. btrfs_set_trans_block_group(trans, dir);
  2636. if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
  2637. err = btrfs_unlink_subvol(trans, root, dir,
  2638. BTRFS_I(inode)->location.objectid,
  2639. dentry->d_name.name,
  2640. dentry->d_name.len);
  2641. goto out;
  2642. }
  2643. err = btrfs_orphan_add(trans, inode);
  2644. if (err)
  2645. goto out;
  2646. /* now the directory is empty */
  2647. err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
  2648. dentry->d_name.name, dentry->d_name.len);
  2649. if (!err)
  2650. btrfs_i_size_write(inode, 0);
  2651. out:
  2652. nr = trans->blocks_used;
  2653. __unlink_end_trans(trans, root);
  2654. btrfs_btree_balance_dirty(root, nr);
  2655. return err;
  2656. }
  2657. #if 0
  2658. /*
  2659. * when truncating bytes in a file, it is possible to avoid reading
  2660. * the leaves that contain only checksum items. This can be the
  2661. * majority of the IO required to delete a large file, but it must
  2662. * be done carefully.
  2663. *
  2664. * The keys in the level just above the leaves are checked to make sure
  2665. * the lowest key in a given leaf is a csum key, and starts at an offset
  2666. * after the new size.
  2667. *
  2668. * Then the key for the next leaf is checked to make sure it also has
  2669. * a checksum item for the same file. If it does, we know our target leaf
  2670. * contains only checksum items, and it can be safely freed without reading
  2671. * it.
  2672. *
  2673. * This is just an optimization targeted at large files. It may do
  2674. * nothing. It will return 0 unless things went badly.
  2675. */
  2676. static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
  2677. struct btrfs_root *root,
  2678. struct btrfs_path *path,
  2679. struct inode *inode, u64 new_size)
  2680. {
  2681. struct btrfs_key key;
  2682. int ret;
  2683. int nritems;
  2684. struct btrfs_key found_key;
  2685. struct btrfs_key other_key;
  2686. struct btrfs_leaf_ref *ref;
  2687. u64 leaf_gen;
  2688. u64 leaf_start;
  2689. path->lowest_level = 1;
  2690. key.objectid = inode->i_ino;
  2691. key.type = BTRFS_CSUM_ITEM_KEY;
  2692. key.offset = new_size;
  2693. again:
  2694. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  2695. if (ret < 0)
  2696. goto out;
  2697. if (path->nodes[1] == NULL) {
  2698. ret = 0;
  2699. goto out;
  2700. }
  2701. ret = 0;
  2702. btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
  2703. nritems = btrfs_header_nritems(path->nodes[1]);
  2704. if (!nritems)
  2705. goto out;
  2706. if (path->slots[1] >= nritems)
  2707. goto next_node;
  2708. /* did we find a key greater than anything we want to delete? */
  2709. if (found_key.objectid > inode->i_ino ||
  2710. (found_key.objectid == inode->i_ino && found_key.type > key.type))
  2711. goto out;
  2712. /* we check the next key in the node to make sure the leave contains
  2713. * only checksum items. This comparison doesn't work if our
  2714. * leaf is the last one in the node
  2715. */
  2716. if (path->slots[1] + 1 >= nritems) {
  2717. next_node:
  2718. /* search forward from the last key in the node, this
  2719. * will bring us into the next node in the tree
  2720. */
  2721. btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
  2722. /* unlikely, but we inc below, so check to be safe */
  2723. if (found_key.offset == (u64)-1)
  2724. goto out;
  2725. /* search_forward needs a path with locks held, do the
  2726. * search again for the original key. It is possible
  2727. * this will race with a balance and return a path that
  2728. * we could modify, but this drop is just an optimization
  2729. * and is allowed to miss some leaves.
  2730. */
  2731. btrfs_release_path(root, path);
  2732. found_key.offset++;
  2733. /* setup a max key for search_forward */
  2734. other_key.offset = (u64)-1;
  2735. other_key.type = key.type;
  2736. other_key.objectid = key.objectid;
  2737. path->keep_locks = 1;
  2738. ret = btrfs_search_forward(root, &found_key, &other_key,
  2739. path, 0, 0);
  2740. path->keep_locks = 0;
  2741. if (ret || found_key.objectid != key.objectid ||
  2742. found_key.type != key.type) {
  2743. ret = 0;
  2744. goto out;
  2745. }
  2746. key.offset = found_key.offset;
  2747. btrfs_release_path(root, path);
  2748. cond_resched();
  2749. goto again;
  2750. }
  2751. /* we know there's one more slot after us in the tree,
  2752. * read that key so we can verify it is also a checksum item
  2753. */
  2754. btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
  2755. if (found_key.objectid < inode->i_ino)
  2756. goto next_key;
  2757. if (found_key.type != key.type || found_key.offset < new_size)
  2758. goto next_key;
  2759. /*
  2760. * if the key for the next leaf isn't a csum key from this objectid,
  2761. * we can't be sure there aren't good items inside this leaf.
  2762. * Bail out
  2763. */
  2764. if (other_key.objectid != inode->i_ino || other_key.type != key.type)
  2765. goto out;
  2766. leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
  2767. leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
  2768. /*
  2769. * it is safe to delete this leaf, it contains only
  2770. * csum items from this inode at an offset >= new_size
  2771. */
  2772. ret = btrfs_del_leaf(trans, root, path, leaf_start);
  2773. BUG_ON(ret);
  2774. if (root->ref_cows && leaf_gen < trans->transid) {
  2775. ref = btrfs_alloc_leaf_ref(root, 0);
  2776. if (ref) {
  2777. ref->root_gen = root->root_key.offset;
  2778. ref->bytenr = leaf_start;
  2779. ref->owner = 0;
  2780. ref->generation = leaf_gen;
  2781. ref->nritems = 0;
  2782. btrfs_sort_leaf_ref(ref);
  2783. ret = btrfs_add_leaf_ref(root, ref, 0);
  2784. WARN_ON(ret);
  2785. btrfs_free_leaf_ref(root, ref);
  2786. } else {
  2787. WARN_ON(1);
  2788. }
  2789. }
  2790. next_key:
  2791. btrfs_release_path(root, path);
  2792. if (other_key.objectid == inode->i_ino &&
  2793. other_key.type == key.type && other_key.offset > key.offset) {
  2794. key.offset = other_key.offset;
  2795. cond_resched();
  2796. goto again;
  2797. }
  2798. ret = 0;
  2799. out:
  2800. /* fixup any changes we've made to the path */
  2801. path->lowest_level = 0;
  2802. path->keep_locks = 0;
  2803. btrfs_release_path(root, path);
  2804. return ret;
  2805. }
  2806. #endif
  2807. /*
  2808. * this can truncate away extent items, csum items and directory items.
  2809. * It starts at a high offset and removes keys until it can't find
  2810. * any higher than new_size
  2811. *
  2812. * csum items that cross the new i_size are truncated to the new size
  2813. * as well.
  2814. *
  2815. * min_type is the minimum key type to truncate down to. If set to 0, this
  2816. * will kill all the items on this inode, including the INODE_ITEM_KEY.
  2817. */
  2818. int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
  2819. struct btrfs_root *root,
  2820. struct inode *inode,
  2821. u64 new_size, u32 min_type)
  2822. {
  2823. struct btrfs_path *path;
  2824. struct extent_buffer *leaf;
  2825. struct btrfs_file_extent_item *fi;
  2826. struct btrfs_key key;
  2827. struct btrfs_key found_key;
  2828. u64 extent_start = 0;
  2829. u64 extent_num_bytes = 0;
  2830. u64 extent_offset = 0;
  2831. u64 item_end = 0;
  2832. u64 mask = root->sectorsize - 1;
  2833. u32 found_type = (u8)-1;
  2834. int found_extent;
  2835. int del_item;
  2836. int pending_del_nr = 0;
  2837. int pending_del_slot = 0;
  2838. int extent_type = -1;
  2839. int encoding;
  2840. int ret;
  2841. int err = 0;
  2842. BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
  2843. if (root->ref_cows || root == root->fs_info->tree_root)
  2844. btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
  2845. path = btrfs_alloc_path();
  2846. BUG_ON(!path);
  2847. path->reada = -1;
  2848. key.objectid = inode->i_ino;
  2849. key.offset = (u64)-1;
  2850. key.type = (u8)-1;
  2851. search_again:
  2852. path->leave_spinning = 1;
  2853. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  2854. if (ret < 0) {
  2855. err = ret;
  2856. goto out;
  2857. }
  2858. if (ret > 0) {
  2859. /* there are no items in the tree for us to truncate, we're
  2860. * done
  2861. */
  2862. if (path->slots[0] == 0)
  2863. goto out;
  2864. path->slots[0]--;
  2865. }
  2866. while (1) {
  2867. fi = NULL;
  2868. leaf = path->nodes[0];
  2869. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2870. found_type = btrfs_key_type(&found_key);
  2871. encoding = 0;
  2872. if (found_key.objectid != inode->i_ino)
  2873. break;
  2874. if (found_type < min_type)
  2875. break;
  2876. item_end = found_key.offset;
  2877. if (found_type == BTRFS_EXTENT_DATA_KEY) {
  2878. fi = btrfs_item_ptr(leaf, path->slots[0],
  2879. struct btrfs_file_extent_item);
  2880. extent_type = btrfs_file_extent_type(leaf, fi);
  2881. encoding = btrfs_file_extent_compression(leaf, fi);
  2882. encoding |= btrfs_file_extent_encryption(leaf, fi);
  2883. encoding |= btrfs_file_extent_other_encoding(leaf, fi);
  2884. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  2885. item_end +=
  2886. btrfs_file_extent_num_bytes(leaf, fi);
  2887. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  2888. item_end += btrfs_file_extent_inline_len(leaf,
  2889. fi);
  2890. }
  2891. item_end--;
  2892. }
  2893. if (found_type > min_type) {
  2894. del_item = 1;
  2895. } else {
  2896. if (item_end < new_size)
  2897. break;
  2898. if (found_key.offset >= new_size)
  2899. del_item = 1;
  2900. else
  2901. del_item = 0;
  2902. }
  2903. found_extent = 0;
  2904. /* FIXME, shrink the extent if the ref count is only 1 */
  2905. if (found_type != BTRFS_EXTENT_DATA_KEY)
  2906. goto delete;
  2907. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  2908. u64 num_dec;
  2909. extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
  2910. if (!del_item && !encoding) {
  2911. u64 orig_num_bytes =
  2912. btrfs_file_extent_num_bytes(leaf, fi);
  2913. extent_num_bytes = new_size -
  2914. found_key.offset + root->sectorsize - 1;
  2915. extent_num_bytes = extent_num_bytes &
  2916. ~((u64)root->sectorsize - 1);
  2917. btrfs_set_file_extent_num_bytes(leaf, fi,
  2918. extent_num_bytes);
  2919. num_dec = (orig_num_bytes -
  2920. extent_num_bytes);
  2921. if (root->ref_cows && extent_start != 0)
  2922. inode_sub_bytes(inode, num_dec);
  2923. btrfs_mark_buffer_dirty(leaf);
  2924. } else {
  2925. extent_num_bytes =
  2926. btrfs_file_extent_disk_num_bytes(leaf,
  2927. fi);
  2928. extent_offset = found_key.offset -
  2929. btrfs_file_extent_offset(leaf, fi);
  2930. /* FIXME blocksize != 4096 */
  2931. num_dec = btrfs_file_extent_num_bytes(leaf, fi);
  2932. if (extent_start != 0) {
  2933. found_extent = 1;
  2934. if (root->ref_cows)
  2935. inode_sub_bytes(inode, num_dec);
  2936. }
  2937. }
  2938. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  2939. /*
  2940. * we can't truncate inline items that have had
  2941. * special encodings
  2942. */
  2943. if (!del_item &&
  2944. btrfs_file_extent_compression(leaf, fi) == 0 &&
  2945. btrfs_file_extent_encryption(leaf, fi) == 0 &&
  2946. btrfs_file_extent_other_encoding(leaf, fi) == 0) {
  2947. u32 size = new_size - found_key.offset;
  2948. if (root->ref_cows) {
  2949. inode_sub_bytes(inode, item_end + 1 -
  2950. new_size);
  2951. }
  2952. size =
  2953. btrfs_file_extent_calc_inline_size(size);
  2954. ret = btrfs_truncate_item(trans, root, path,
  2955. size, 1);
  2956. BUG_ON(ret);
  2957. } else if (root->ref_cows) {
  2958. inode_sub_bytes(inode, item_end + 1 -
  2959. found_key.offset);
  2960. }
  2961. }
  2962. delete:
  2963. if (del_item) {
  2964. if (!pending_del_nr) {
  2965. /* no pending yet, add ourselves */
  2966. pending_del_slot = path->slots[0];
  2967. pending_del_nr = 1;
  2968. } else if (pending_del_nr &&
  2969. path->slots[0] + 1 == pending_del_slot) {
  2970. /* hop on the pending chunk */
  2971. pending_del_nr++;
  2972. pending_del_slot = path->slots[0];
  2973. } else {
  2974. BUG();
  2975. }
  2976. } else {
  2977. break;
  2978. }
  2979. if (found_extent && (root->ref_cows ||
  2980. root == root->fs_info->tree_root)) {
  2981. btrfs_set_path_blocking(path);
  2982. ret = btrfs_free_extent(trans, root, extent_start,
  2983. extent_num_bytes, 0,
  2984. btrfs_header_owner(leaf),
  2985. inode->i_ino, extent_offset);
  2986. BUG_ON(ret);
  2987. }
  2988. if (found_type == BTRFS_INODE_ITEM_KEY)
  2989. break;
  2990. if (path->slots[0] == 0 ||
  2991. path->slots[0] != pending_del_slot) {
  2992. if (root->ref_cows) {
  2993. err = -EAGAIN;
  2994. goto out;
  2995. }
  2996. if (pending_del_nr) {
  2997. ret = btrfs_del_items(trans, root, path,
  2998. pending_del_slot,
  2999. pending_del_nr);
  3000. BUG_ON(ret);
  3001. pending_del_nr = 0;
  3002. }
  3003. btrfs_release_path(root, path);
  3004. goto search_again;
  3005. } else {
  3006. path->slots[0]--;
  3007. }
  3008. }
  3009. out:
  3010. if (pending_del_nr) {
  3011. ret = btrfs_del_items(trans, root, path, pending_del_slot,
  3012. pending_del_nr);
  3013. BUG_ON(ret);
  3014. }
  3015. btrfs_free_path(path);
  3016. return err;
  3017. }
  3018. /*
  3019. * taken from block_truncate_page, but does cow as it zeros out
  3020. * any bytes left in the last page in the file.
  3021. */
  3022. static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
  3023. {
  3024. struct inode *inode = mapping->host;
  3025. struct btrfs_root *root = BTRFS_I(inode)->root;
  3026. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  3027. struct btrfs_ordered_extent *ordered;
  3028. struct extent_state *cached_state = NULL;
  3029. char *kaddr;
  3030. u32 blocksize = root->sectorsize;
  3031. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  3032. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  3033. struct page *page;
  3034. int ret = 0;
  3035. u64 page_start;
  3036. u64 page_end;
  3037. if ((offset & (blocksize - 1)) == 0)
  3038. goto out;
  3039. ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
  3040. if (ret)
  3041. goto out;
  3042. ret = -ENOMEM;
  3043. again:
  3044. page = grab_cache_page(mapping, index);
  3045. if (!page) {
  3046. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  3047. goto out;
  3048. }
  3049. page_start = page_offset(page);
  3050. page_end = page_start + PAGE_CACHE_SIZE - 1;
  3051. if (!PageUptodate(page)) {
  3052. ret = btrfs_readpage(NULL, page);
  3053. lock_page(page);
  3054. if (page->mapping != mapping) {
  3055. unlock_page(page);
  3056. page_cache_release(page);
  3057. goto again;
  3058. }
  3059. if (!PageUptodate(page)) {
  3060. ret = -EIO;
  3061. goto out_unlock;
  3062. }
  3063. }
  3064. wait_on_page_writeback(page);
  3065. lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
  3066. GFP_NOFS);
  3067. set_page_extent_mapped(page);
  3068. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  3069. if (ordered) {
  3070. unlock_extent_cached(io_tree, page_start, page_end,
  3071. &cached_state, GFP_NOFS);
  3072. unlock_page(page);
  3073. page_cache_release(page);
  3074. btrfs_start_ordered_extent(inode, ordered, 1);
  3075. btrfs_put_ordered_extent(ordered);
  3076. goto again;
  3077. }
  3078. clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
  3079. EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
  3080. 0, 0, &cached_state, GFP_NOFS);
  3081. ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
  3082. &cached_state);
  3083. if (ret) {
  3084. unlock_extent_cached(io_tree, page_start, page_end,
  3085. &cached_state, GFP_NOFS);
  3086. goto out_unlock;
  3087. }
  3088. ret = 0;
  3089. if (offset != PAGE_CACHE_SIZE) {
  3090. kaddr = kmap(page);
  3091. memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
  3092. flush_dcache_page(page);
  3093. kunmap(page);
  3094. }
  3095. ClearPageChecked(page);
  3096. set_page_dirty(page);
  3097. unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
  3098. GFP_NOFS);
  3099. out_unlock:
  3100. if (ret)
  3101. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  3102. unlock_page(page);
  3103. page_cache_release(page);
  3104. out:
  3105. return ret;
  3106. }
  3107. int btrfs_cont_expand(struct inode *inode, loff_t size)
  3108. {
  3109. struct btrfs_trans_handle *trans;
  3110. struct btrfs_root *root = BTRFS_I(inode)->root;
  3111. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  3112. struct extent_map *em = NULL;
  3113. struct extent_state *cached_state = NULL;
  3114. u64 mask = root->sectorsize - 1;
  3115. u64 hole_start = (inode->i_size + mask) & ~mask;
  3116. u64 block_end = (size + mask) & ~mask;
  3117. u64 last_byte;
  3118. u64 cur_offset;
  3119. u64 hole_size;
  3120. int err = 0;
  3121. if (size <= hole_start)
  3122. return 0;
  3123. while (1) {
  3124. struct btrfs_ordered_extent *ordered;
  3125. btrfs_wait_ordered_range(inode, hole_start,
  3126. block_end - hole_start);
  3127. lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
  3128. &cached_state, GFP_NOFS);
  3129. ordered = btrfs_lookup_ordered_extent(inode, hole_start);
  3130. if (!ordered)
  3131. break;
  3132. unlock_extent_cached(io_tree, hole_start, block_end - 1,
  3133. &cached_state, GFP_NOFS);
  3134. btrfs_put_ordered_extent(ordered);
  3135. }
  3136. cur_offset = hole_start;
  3137. while (1) {
  3138. em = btrfs_get_extent(inode, NULL, 0, cur_offset,
  3139. block_end - cur_offset, 0);
  3140. BUG_ON(IS_ERR(em) || !em);
  3141. last_byte = min(extent_map_end(em), block_end);
  3142. last_byte = (last_byte + mask) & ~mask;
  3143. if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
  3144. u64 hint_byte = 0;
  3145. hole_size = last_byte - cur_offset;
  3146. trans = btrfs_start_transaction(root, 2);
  3147. if (IS_ERR(trans)) {
  3148. err = PTR_ERR(trans);
  3149. break;
  3150. }
  3151. btrfs_set_trans_block_group(trans, inode);
  3152. err = btrfs_drop_extents(trans, inode, cur_offset,
  3153. cur_offset + hole_size,
  3154. &hint_byte, 1);
  3155. BUG_ON(err);
  3156. err = btrfs_insert_file_extent(trans, root,
  3157. inode->i_ino, cur_offset, 0,
  3158. 0, hole_size, 0, hole_size,
  3159. 0, 0, 0);
  3160. BUG_ON(err);
  3161. btrfs_drop_extent_cache(inode, hole_start,
  3162. last_byte - 1, 0);
  3163. btrfs_end_transaction(trans, root);
  3164. }
  3165. free_extent_map(em);
  3166. em = NULL;
  3167. cur_offset = last_byte;
  3168. if (cur_offset >= block_end)
  3169. break;
  3170. }
  3171. free_extent_map(em);
  3172. unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
  3173. GFP_NOFS);
  3174. return err;
  3175. }
  3176. static int btrfs_setattr_size(struct inode *inode, struct iattr *attr)
  3177. {
  3178. struct btrfs_root *root = BTRFS_I(inode)->root;
  3179. struct btrfs_trans_handle *trans;
  3180. unsigned long nr;
  3181. int ret;
  3182. if (attr->ia_size == inode->i_size)
  3183. return 0;
  3184. if (attr->ia_size > inode->i_size) {
  3185. unsigned long limit;
  3186. limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
  3187. if (attr->ia_size > inode->i_sb->s_maxbytes)
  3188. return -EFBIG;
  3189. if (limit != RLIM_INFINITY && attr->ia_size > limit) {
  3190. send_sig(SIGXFSZ, current, 0);
  3191. return -EFBIG;
  3192. }
  3193. }
  3194. trans = btrfs_start_transaction(root, 5);
  3195. if (IS_ERR(trans))
  3196. return PTR_ERR(trans);
  3197. btrfs_set_trans_block_group(trans, inode);
  3198. ret = btrfs_orphan_add(trans, inode);
  3199. BUG_ON(ret);
  3200. nr = trans->blocks_used;
  3201. btrfs_end_transaction(trans, root);
  3202. btrfs_btree_balance_dirty(root, nr);
  3203. if (attr->ia_size > inode->i_size) {
  3204. ret = btrfs_cont_expand(inode, attr->ia_size);
  3205. if (ret) {
  3206. btrfs_truncate(inode);
  3207. return ret;
  3208. }
  3209. i_size_write(inode, attr->ia_size);
  3210. btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
  3211. trans = btrfs_start_transaction(root, 0);
  3212. BUG_ON(IS_ERR(trans));
  3213. btrfs_set_trans_block_group(trans, inode);
  3214. trans->block_rsv = root->orphan_block_rsv;
  3215. BUG_ON(!trans->block_rsv);
  3216. ret = btrfs_update_inode(trans, root, inode);
  3217. BUG_ON(ret);
  3218. if (inode->i_nlink > 0) {
  3219. ret = btrfs_orphan_del(trans, inode);
  3220. BUG_ON(ret);
  3221. }
  3222. nr = trans->blocks_used;
  3223. btrfs_end_transaction(trans, root);
  3224. btrfs_btree_balance_dirty(root, nr);
  3225. return 0;
  3226. }
  3227. /*
  3228. * We're truncating a file that used to have good data down to
  3229. * zero. Make sure it gets into the ordered flush list so that
  3230. * any new writes get down to disk quickly.
  3231. */
  3232. if (attr->ia_size == 0)
  3233. BTRFS_I(inode)->ordered_data_close = 1;
  3234. /* we don't support swapfiles, so vmtruncate shouldn't fail */
  3235. ret = vmtruncate(inode, attr->ia_size);
  3236. BUG_ON(ret);
  3237. return 0;
  3238. }
  3239. static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
  3240. {
  3241. struct inode *inode = dentry->d_inode;
  3242. int err;
  3243. err = inode_change_ok(inode, attr);
  3244. if (err)
  3245. return err;
  3246. if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
  3247. err = btrfs_setattr_size(inode, attr);
  3248. if (err)
  3249. return err;
  3250. }
  3251. if (attr->ia_valid) {
  3252. setattr_copy(inode, attr);
  3253. mark_inode_dirty(inode);
  3254. if (attr->ia_valid & ATTR_MODE)
  3255. err = btrfs_acl_chmod(inode);
  3256. }
  3257. return err;
  3258. }
  3259. void btrfs_evict_inode(struct inode *inode)
  3260. {
  3261. struct btrfs_trans_handle *trans;
  3262. struct btrfs_root *root = BTRFS_I(inode)->root;
  3263. unsigned long nr;
  3264. int ret;
  3265. truncate_inode_pages(&inode->i_data, 0);
  3266. if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
  3267. root == root->fs_info->tree_root))
  3268. goto no_delete;
  3269. if (is_bad_inode(inode)) {
  3270. btrfs_orphan_del(NULL, inode);
  3271. goto no_delete;
  3272. }
  3273. /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
  3274. btrfs_wait_ordered_range(inode, 0, (u64)-1);
  3275. if (root->fs_info->log_root_recovering) {
  3276. BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan));
  3277. goto no_delete;
  3278. }
  3279. if (inode->i_nlink > 0) {
  3280. BUG_ON(btrfs_root_refs(&root->root_item) != 0);
  3281. goto no_delete;
  3282. }
  3283. btrfs_i_size_write(inode, 0);
  3284. while (1) {
  3285. trans = btrfs_start_transaction(root, 0);
  3286. BUG_ON(IS_ERR(trans));
  3287. btrfs_set_trans_block_group(trans, inode);
  3288. trans->block_rsv = root->orphan_block_rsv;
  3289. ret = btrfs_block_rsv_check(trans, root,
  3290. root->orphan_block_rsv, 0, 5);
  3291. if (ret) {
  3292. BUG_ON(ret != -EAGAIN);
  3293. ret = btrfs_commit_transaction(trans, root);
  3294. BUG_ON(ret);
  3295. continue;
  3296. }
  3297. ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
  3298. if (ret != -EAGAIN)
  3299. break;
  3300. nr = trans->blocks_used;
  3301. btrfs_end_transaction(trans, root);
  3302. trans = NULL;
  3303. btrfs_btree_balance_dirty(root, nr);
  3304. }
  3305. if (ret == 0) {
  3306. ret = btrfs_orphan_del(trans, inode);
  3307. BUG_ON(ret);
  3308. }
  3309. nr = trans->blocks_used;
  3310. btrfs_end_transaction(trans, root);
  3311. btrfs_btree_balance_dirty(root, nr);
  3312. no_delete:
  3313. end_writeback(inode);
  3314. return;
  3315. }
  3316. /*
  3317. * this returns the key found in the dir entry in the location pointer.
  3318. * If no dir entries were found, location->objectid is 0.
  3319. */
  3320. static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
  3321. struct btrfs_key *location)
  3322. {
  3323. const char *name = dentry->d_name.name;
  3324. int namelen = dentry->d_name.len;
  3325. struct btrfs_dir_item *di;
  3326. struct btrfs_path *path;
  3327. struct btrfs_root *root = BTRFS_I(dir)->root;
  3328. int ret = 0;
  3329. path = btrfs_alloc_path();
  3330. BUG_ON(!path);
  3331. di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
  3332. namelen, 0);
  3333. if (IS_ERR(di))
  3334. ret = PTR_ERR(di);
  3335. if (!di || IS_ERR(di))
  3336. goto out_err;
  3337. btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
  3338. out:
  3339. btrfs_free_path(path);
  3340. return ret;
  3341. out_err:
  3342. location->objectid = 0;
  3343. goto out;
  3344. }
  3345. /*
  3346. * when we hit a tree root in a directory, the btrfs part of the inode
  3347. * needs to be changed to reflect the root directory of the tree root. This
  3348. * is kind of like crossing a mount point.
  3349. */
  3350. static int fixup_tree_root_location(struct btrfs_root *root,
  3351. struct inode *dir,
  3352. struct dentry *dentry,
  3353. struct btrfs_key *location,
  3354. struct btrfs_root **sub_root)
  3355. {
  3356. struct btrfs_path *path;
  3357. struct btrfs_root *new_root;
  3358. struct btrfs_root_ref *ref;
  3359. struct extent_buffer *leaf;
  3360. int ret;
  3361. int err = 0;
  3362. path = btrfs_alloc_path();
  3363. if (!path) {
  3364. err = -ENOMEM;
  3365. goto out;
  3366. }
  3367. err = -ENOENT;
  3368. ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
  3369. BTRFS_I(dir)->root->root_key.objectid,
  3370. location->objectid);
  3371. if (ret) {
  3372. if (ret < 0)
  3373. err = ret;
  3374. goto out;
  3375. }
  3376. leaf = path->nodes[0];
  3377. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
  3378. if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino ||
  3379. btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
  3380. goto out;
  3381. ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
  3382. (unsigned long)(ref + 1),
  3383. dentry->d_name.len);
  3384. if (ret)
  3385. goto out;
  3386. btrfs_release_path(root->fs_info->tree_root, path);
  3387. new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
  3388. if (IS_ERR(new_root)) {
  3389. err = PTR_ERR(new_root);
  3390. goto out;
  3391. }
  3392. if (btrfs_root_refs(&new_root->root_item) == 0) {
  3393. err = -ENOENT;
  3394. goto out;
  3395. }
  3396. *sub_root = new_root;
  3397. location->objectid = btrfs_root_dirid(&new_root->root_item);
  3398. location->type = BTRFS_INODE_ITEM_KEY;
  3399. location->offset = 0;
  3400. err = 0;
  3401. out:
  3402. btrfs_free_path(path);
  3403. return err;
  3404. }
  3405. static void inode_tree_add(struct inode *inode)
  3406. {
  3407. struct btrfs_root *root = BTRFS_I(inode)->root;
  3408. struct btrfs_inode *entry;
  3409. struct rb_node **p;
  3410. struct rb_node *parent;
  3411. again:
  3412. p = &root->inode_tree.rb_node;
  3413. parent = NULL;
  3414. if (inode_unhashed(inode))
  3415. return;
  3416. spin_lock(&root->inode_lock);
  3417. while (*p) {
  3418. parent = *p;
  3419. entry = rb_entry(parent, struct btrfs_inode, rb_node);
  3420. if (inode->i_ino < entry->vfs_inode.i_ino)
  3421. p = &parent->rb_left;
  3422. else if (inode->i_ino > entry->vfs_inode.i_ino)
  3423. p = &parent->rb_right;
  3424. else {
  3425. WARN_ON(!(entry->vfs_inode.i_state &
  3426. (I_WILL_FREE | I_FREEING)));
  3427. rb_erase(parent, &root->inode_tree);
  3428. RB_CLEAR_NODE(parent);
  3429. spin_unlock(&root->inode_lock);
  3430. goto again;
  3431. }
  3432. }
  3433. rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
  3434. rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
  3435. spin_unlock(&root->inode_lock);
  3436. }
  3437. static void inode_tree_del(struct inode *inode)
  3438. {
  3439. struct btrfs_root *root = BTRFS_I(inode)->root;
  3440. int empty = 0;
  3441. spin_lock(&root->inode_lock);
  3442. if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
  3443. rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
  3444. RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
  3445. empty = RB_EMPTY_ROOT(&root->inode_tree);
  3446. }
  3447. spin_unlock(&root->inode_lock);
  3448. /*
  3449. * Free space cache has inodes in the tree root, but the tree root has a
  3450. * root_refs of 0, so this could end up dropping the tree root as a
  3451. * snapshot, so we need the extra !root->fs_info->tree_root check to
  3452. * make sure we don't drop it.
  3453. */
  3454. if (empty && btrfs_root_refs(&root->root_item) == 0 &&
  3455. root != root->fs_info->tree_root) {
  3456. synchronize_srcu(&root->fs_info->subvol_srcu);
  3457. spin_lock(&root->inode_lock);
  3458. empty = RB_EMPTY_ROOT(&root->inode_tree);
  3459. spin_unlock(&root->inode_lock);
  3460. if (empty)
  3461. btrfs_add_dead_root(root);
  3462. }
  3463. }
  3464. int btrfs_invalidate_inodes(struct btrfs_root *root)
  3465. {
  3466. struct rb_node *node;
  3467. struct rb_node *prev;
  3468. struct btrfs_inode *entry;
  3469. struct inode *inode;
  3470. u64 objectid = 0;
  3471. WARN_ON(btrfs_root_refs(&root->root_item) != 0);
  3472. spin_lock(&root->inode_lock);
  3473. again:
  3474. node = root->inode_tree.rb_node;
  3475. prev = NULL;
  3476. while (node) {
  3477. prev = node;
  3478. entry = rb_entry(node, struct btrfs_inode, rb_node);
  3479. if (objectid < entry->vfs_inode.i_ino)
  3480. node = node->rb_left;
  3481. else if (objectid > entry->vfs_inode.i_ino)
  3482. node = node->rb_right;
  3483. else
  3484. break;
  3485. }
  3486. if (!node) {
  3487. while (prev) {
  3488. entry = rb_entry(prev, struct btrfs_inode, rb_node);
  3489. if (objectid <= entry->vfs_inode.i_ino) {
  3490. node = prev;
  3491. break;
  3492. }
  3493. prev = rb_next(prev);
  3494. }
  3495. }
  3496. while (node) {
  3497. entry = rb_entry(node, struct btrfs_inode, rb_node);
  3498. objectid = entry->vfs_inode.i_ino + 1;
  3499. inode = igrab(&entry->vfs_inode);
  3500. if (inode) {
  3501. spin_unlock(&root->inode_lock);
  3502. if (atomic_read(&inode->i_count) > 1)
  3503. d_prune_aliases(inode);
  3504. /*
  3505. * btrfs_drop_inode will have it removed from
  3506. * the inode cache when its usage count
  3507. * hits zero.
  3508. */
  3509. iput(inode);
  3510. cond_resched();
  3511. spin_lock(&root->inode_lock);
  3512. goto again;
  3513. }
  3514. if (cond_resched_lock(&root->inode_lock))
  3515. goto again;
  3516. node = rb_next(node);
  3517. }
  3518. spin_unlock(&root->inode_lock);
  3519. return 0;
  3520. }
  3521. static int btrfs_init_locked_inode(struct inode *inode, void *p)
  3522. {
  3523. struct btrfs_iget_args *args = p;
  3524. inode->i_ino = args->ino;
  3525. BTRFS_I(inode)->root = args->root;
  3526. btrfs_set_inode_space_info(args->root, inode);
  3527. return 0;
  3528. }
  3529. static int btrfs_find_actor(struct inode *inode, void *opaque)
  3530. {
  3531. struct btrfs_iget_args *args = opaque;
  3532. return args->ino == inode->i_ino &&
  3533. args->root == BTRFS_I(inode)->root;
  3534. }
  3535. static struct inode *btrfs_iget_locked(struct super_block *s,
  3536. u64 objectid,
  3537. struct btrfs_root *root)
  3538. {
  3539. struct inode *inode;
  3540. struct btrfs_iget_args args;
  3541. args.ino = objectid;
  3542. args.root = root;
  3543. inode = iget5_locked(s, objectid, btrfs_find_actor,
  3544. btrfs_init_locked_inode,
  3545. (void *)&args);
  3546. return inode;
  3547. }
  3548. /* Get an inode object given its location and corresponding root.
  3549. * Returns in *is_new if the inode was read from disk
  3550. */
  3551. struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
  3552. struct btrfs_root *root, int *new)
  3553. {
  3554. struct inode *inode;
  3555. inode = btrfs_iget_locked(s, location->objectid, root);
  3556. if (!inode)
  3557. return ERR_PTR(-ENOMEM);
  3558. if (inode->i_state & I_NEW) {
  3559. BTRFS_I(inode)->root = root;
  3560. memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
  3561. btrfs_read_locked_inode(inode);
  3562. inode_tree_add(inode);
  3563. unlock_new_inode(inode);
  3564. if (new)
  3565. *new = 1;
  3566. }
  3567. return inode;
  3568. }
  3569. static struct inode *new_simple_dir(struct super_block *s,
  3570. struct btrfs_key *key,
  3571. struct btrfs_root *root)
  3572. {
  3573. struct inode *inode = new_inode(s);
  3574. if (!inode)
  3575. return ERR_PTR(-ENOMEM);
  3576. BTRFS_I(inode)->root = root;
  3577. memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
  3578. BTRFS_I(inode)->dummy_inode = 1;
  3579. inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
  3580. inode->i_op = &simple_dir_inode_operations;
  3581. inode->i_fop = &simple_dir_operations;
  3582. inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
  3583. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  3584. return inode;
  3585. }
  3586. struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
  3587. {
  3588. struct inode *inode;
  3589. struct btrfs_root *root = BTRFS_I(dir)->root;
  3590. struct btrfs_root *sub_root = root;
  3591. struct btrfs_key location;
  3592. int index;
  3593. int ret;
  3594. d_set_d_op(dentry, &btrfs_dentry_operations);
  3595. if (dentry->d_name.len > BTRFS_NAME_LEN)
  3596. return ERR_PTR(-ENAMETOOLONG);
  3597. ret = btrfs_inode_by_name(dir, dentry, &location);
  3598. if (ret < 0)
  3599. return ERR_PTR(ret);
  3600. if (location.objectid == 0)
  3601. return NULL;
  3602. if (location.type == BTRFS_INODE_ITEM_KEY) {
  3603. inode = btrfs_iget(dir->i_sb, &location, root, NULL);
  3604. return inode;
  3605. }
  3606. BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
  3607. index = srcu_read_lock(&root->fs_info->subvol_srcu);
  3608. ret = fixup_tree_root_location(root, dir, dentry,
  3609. &location, &sub_root);
  3610. if (ret < 0) {
  3611. if (ret != -ENOENT)
  3612. inode = ERR_PTR(ret);
  3613. else
  3614. inode = new_simple_dir(dir->i_sb, &location, sub_root);
  3615. } else {
  3616. inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
  3617. }
  3618. srcu_read_unlock(&root->fs_info->subvol_srcu, index);
  3619. if (root != sub_root) {
  3620. down_read(&root->fs_info->cleanup_work_sem);
  3621. if (!(inode->i_sb->s_flags & MS_RDONLY))
  3622. btrfs_orphan_cleanup(sub_root);
  3623. up_read(&root->fs_info->cleanup_work_sem);
  3624. }
  3625. return inode;
  3626. }
  3627. static int btrfs_dentry_delete(const struct dentry *dentry)
  3628. {
  3629. struct btrfs_root *root;
  3630. if (!dentry->d_inode && !IS_ROOT(dentry))
  3631. dentry = dentry->d_parent;
  3632. if (dentry->d_inode) {
  3633. root = BTRFS_I(dentry->d_inode)->root;
  3634. if (btrfs_root_refs(&root->root_item) == 0)
  3635. return 1;
  3636. }
  3637. return 0;
  3638. }
  3639. static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
  3640. struct nameidata *nd)
  3641. {
  3642. struct inode *inode;
  3643. inode = btrfs_lookup_dentry(dir, dentry);
  3644. if (IS_ERR(inode))
  3645. return ERR_CAST(inode);
  3646. return d_splice_alias(inode, dentry);
  3647. }
  3648. static unsigned char btrfs_filetype_table[] = {
  3649. DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
  3650. };
  3651. static int btrfs_real_readdir(struct file *filp, void *dirent,
  3652. filldir_t filldir)
  3653. {
  3654. struct inode *inode = filp->f_dentry->d_inode;
  3655. struct btrfs_root *root = BTRFS_I(inode)->root;
  3656. struct btrfs_item *item;
  3657. struct btrfs_dir_item *di;
  3658. struct btrfs_key key;
  3659. struct btrfs_key found_key;
  3660. struct btrfs_path *path;
  3661. int ret;
  3662. u32 nritems;
  3663. struct extent_buffer *leaf;
  3664. int slot;
  3665. int advance;
  3666. unsigned char d_type;
  3667. int over = 0;
  3668. u32 di_cur;
  3669. u32 di_total;
  3670. u32 di_len;
  3671. int key_type = BTRFS_DIR_INDEX_KEY;
  3672. char tmp_name[32];
  3673. char *name_ptr;
  3674. int name_len;
  3675. /* FIXME, use a real flag for deciding about the key type */
  3676. if (root->fs_info->tree_root == root)
  3677. key_type = BTRFS_DIR_ITEM_KEY;
  3678. /* special case for "." */
  3679. if (filp->f_pos == 0) {
  3680. over = filldir(dirent, ".", 1,
  3681. 1, inode->i_ino,
  3682. DT_DIR);
  3683. if (over)
  3684. return 0;
  3685. filp->f_pos = 1;
  3686. }
  3687. /* special case for .., just use the back ref */
  3688. if (filp->f_pos == 1) {
  3689. u64 pino = parent_ino(filp->f_path.dentry);
  3690. over = filldir(dirent, "..", 2,
  3691. 2, pino, DT_DIR);
  3692. if (over)
  3693. return 0;
  3694. filp->f_pos = 2;
  3695. }
  3696. path = btrfs_alloc_path();
  3697. path->reada = 2;
  3698. btrfs_set_key_type(&key, key_type);
  3699. key.offset = filp->f_pos;
  3700. key.objectid = inode->i_ino;
  3701. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3702. if (ret < 0)
  3703. goto err;
  3704. advance = 0;
  3705. while (1) {
  3706. leaf = path->nodes[0];
  3707. nritems = btrfs_header_nritems(leaf);
  3708. slot = path->slots[0];
  3709. if (advance || slot >= nritems) {
  3710. if (slot >= nritems - 1) {
  3711. ret = btrfs_next_leaf(root, path);
  3712. if (ret)
  3713. break;
  3714. leaf = path->nodes[0];
  3715. nritems = btrfs_header_nritems(leaf);
  3716. slot = path->slots[0];
  3717. } else {
  3718. slot++;
  3719. path->slots[0]++;
  3720. }
  3721. }
  3722. advance = 1;
  3723. item = btrfs_item_nr(leaf, slot);
  3724. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  3725. if (found_key.objectid != key.objectid)
  3726. break;
  3727. if (btrfs_key_type(&found_key) != key_type)
  3728. break;
  3729. if (found_key.offset < filp->f_pos)
  3730. continue;
  3731. filp->f_pos = found_key.offset;
  3732. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  3733. di_cur = 0;
  3734. di_total = btrfs_item_size(leaf, item);
  3735. while (di_cur < di_total) {
  3736. struct btrfs_key location;
  3737. name_len = btrfs_dir_name_len(leaf, di);
  3738. if (name_len <= sizeof(tmp_name)) {
  3739. name_ptr = tmp_name;
  3740. } else {
  3741. name_ptr = kmalloc(name_len, GFP_NOFS);
  3742. if (!name_ptr) {
  3743. ret = -ENOMEM;
  3744. goto err;
  3745. }
  3746. }
  3747. read_extent_buffer(leaf, name_ptr,
  3748. (unsigned long)(di + 1), name_len);
  3749. d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
  3750. btrfs_dir_item_key_to_cpu(leaf, di, &location);
  3751. /* is this a reference to our own snapshot? If so
  3752. * skip it
  3753. */
  3754. if (location.type == BTRFS_ROOT_ITEM_KEY &&
  3755. location.objectid == root->root_key.objectid) {
  3756. over = 0;
  3757. goto skip;
  3758. }
  3759. over = filldir(dirent, name_ptr, name_len,
  3760. found_key.offset, location.objectid,
  3761. d_type);
  3762. skip:
  3763. if (name_ptr != tmp_name)
  3764. kfree(name_ptr);
  3765. if (over)
  3766. goto nopos;
  3767. di_len = btrfs_dir_name_len(leaf, di) +
  3768. btrfs_dir_data_len(leaf, di) + sizeof(*di);
  3769. di_cur += di_len;
  3770. di = (struct btrfs_dir_item *)((char *)di + di_len);
  3771. }
  3772. }
  3773. /* Reached end of directory/root. Bump pos past the last item. */
  3774. if (key_type == BTRFS_DIR_INDEX_KEY)
  3775. /*
  3776. * 32-bit glibc will use getdents64, but then strtol -
  3777. * so the last number we can serve is this.
  3778. */
  3779. filp->f_pos = 0x7fffffff;
  3780. else
  3781. filp->f_pos++;
  3782. nopos:
  3783. ret = 0;
  3784. err:
  3785. btrfs_free_path(path);
  3786. return ret;
  3787. }
  3788. int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
  3789. {
  3790. struct btrfs_root *root = BTRFS_I(inode)->root;
  3791. struct btrfs_trans_handle *trans;
  3792. int ret = 0;
  3793. bool nolock = false;
  3794. if (BTRFS_I(inode)->dummy_inode)
  3795. return 0;
  3796. smp_mb();
  3797. nolock = (root->fs_info->closing && root == root->fs_info->tree_root);
  3798. if (wbc->sync_mode == WB_SYNC_ALL) {
  3799. if (nolock)
  3800. trans = btrfs_join_transaction_nolock(root, 1);
  3801. else
  3802. trans = btrfs_join_transaction(root, 1);
  3803. btrfs_set_trans_block_group(trans, inode);
  3804. if (nolock)
  3805. ret = btrfs_end_transaction_nolock(trans, root);
  3806. else
  3807. ret = btrfs_commit_transaction(trans, root);
  3808. }
  3809. return ret;
  3810. }
  3811. /*
  3812. * This is somewhat expensive, updating the tree every time the
  3813. * inode changes. But, it is most likely to find the inode in cache.
  3814. * FIXME, needs more benchmarking...there are no reasons other than performance
  3815. * to keep or drop this code.
  3816. */
  3817. void btrfs_dirty_inode(struct inode *inode)
  3818. {
  3819. struct btrfs_root *root = BTRFS_I(inode)->root;
  3820. struct btrfs_trans_handle *trans;
  3821. int ret;
  3822. if (BTRFS_I(inode)->dummy_inode)
  3823. return;
  3824. trans = btrfs_join_transaction(root, 1);
  3825. btrfs_set_trans_block_group(trans, inode);
  3826. ret = btrfs_update_inode(trans, root, inode);
  3827. if (ret && ret == -ENOSPC) {
  3828. /* whoops, lets try again with the full transaction */
  3829. btrfs_end_transaction(trans, root);
  3830. trans = btrfs_start_transaction(root, 1);
  3831. if (IS_ERR(trans)) {
  3832. if (printk_ratelimit()) {
  3833. printk(KERN_ERR "btrfs: fail to "
  3834. "dirty inode %lu error %ld\n",
  3835. inode->i_ino, PTR_ERR(trans));
  3836. }
  3837. return;
  3838. }
  3839. btrfs_set_trans_block_group(trans, inode);
  3840. ret = btrfs_update_inode(trans, root, inode);
  3841. if (ret) {
  3842. if (printk_ratelimit()) {
  3843. printk(KERN_ERR "btrfs: fail to "
  3844. "dirty inode %lu error %d\n",
  3845. inode->i_ino, ret);
  3846. }
  3847. }
  3848. }
  3849. btrfs_end_transaction(trans, root);
  3850. }
  3851. /*
  3852. * find the highest existing sequence number in a directory
  3853. * and then set the in-memory index_cnt variable to reflect
  3854. * free sequence numbers
  3855. */
  3856. static int btrfs_set_inode_index_count(struct inode *inode)
  3857. {
  3858. struct btrfs_root *root = BTRFS_I(inode)->root;
  3859. struct btrfs_key key, found_key;
  3860. struct btrfs_path *path;
  3861. struct extent_buffer *leaf;
  3862. int ret;
  3863. key.objectid = inode->i_ino;
  3864. btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
  3865. key.offset = (u64)-1;
  3866. path = btrfs_alloc_path();
  3867. if (!path)
  3868. return -ENOMEM;
  3869. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3870. if (ret < 0)
  3871. goto out;
  3872. /* FIXME: we should be able to handle this */
  3873. if (ret == 0)
  3874. goto out;
  3875. ret = 0;
  3876. /*
  3877. * MAGIC NUMBER EXPLANATION:
  3878. * since we search a directory based on f_pos we have to start at 2
  3879. * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
  3880. * else has to start at 2
  3881. */
  3882. if (path->slots[0] == 0) {
  3883. BTRFS_I(inode)->index_cnt = 2;
  3884. goto out;
  3885. }
  3886. path->slots[0]--;
  3887. leaf = path->nodes[0];
  3888. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3889. if (found_key.objectid != inode->i_ino ||
  3890. btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
  3891. BTRFS_I(inode)->index_cnt = 2;
  3892. goto out;
  3893. }
  3894. BTRFS_I(inode)->index_cnt = found_key.offset + 1;
  3895. out:
  3896. btrfs_free_path(path);
  3897. return ret;
  3898. }
  3899. /*
  3900. * helper to find a free sequence number in a given directory. This current
  3901. * code is very simple, later versions will do smarter things in the btree
  3902. */
  3903. int btrfs_set_inode_index(struct inode *dir, u64 *index)
  3904. {
  3905. int ret = 0;
  3906. if (BTRFS_I(dir)->index_cnt == (u64)-1) {
  3907. ret = btrfs_set_inode_index_count(dir);
  3908. if (ret)
  3909. return ret;
  3910. }
  3911. *index = BTRFS_I(dir)->index_cnt;
  3912. BTRFS_I(dir)->index_cnt++;
  3913. return ret;
  3914. }
  3915. static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
  3916. struct btrfs_root *root,
  3917. struct inode *dir,
  3918. const char *name, int name_len,
  3919. u64 ref_objectid, u64 objectid,
  3920. u64 alloc_hint, int mode, u64 *index)
  3921. {
  3922. struct inode *inode;
  3923. struct btrfs_inode_item *inode_item;
  3924. struct btrfs_key *location;
  3925. struct btrfs_path *path;
  3926. struct btrfs_inode_ref *ref;
  3927. struct btrfs_key key[2];
  3928. u32 sizes[2];
  3929. unsigned long ptr;
  3930. int ret;
  3931. int owner;
  3932. path = btrfs_alloc_path();
  3933. BUG_ON(!path);
  3934. inode = new_inode(root->fs_info->sb);
  3935. if (!inode)
  3936. return ERR_PTR(-ENOMEM);
  3937. if (dir) {
  3938. ret = btrfs_set_inode_index(dir, index);
  3939. if (ret) {
  3940. iput(inode);
  3941. return ERR_PTR(ret);
  3942. }
  3943. }
  3944. /*
  3945. * index_cnt is ignored for everything but a dir,
  3946. * btrfs_get_inode_index_count has an explanation for the magic
  3947. * number
  3948. */
  3949. BTRFS_I(inode)->index_cnt = 2;
  3950. BTRFS_I(inode)->root = root;
  3951. BTRFS_I(inode)->generation = trans->transid;
  3952. inode->i_generation = BTRFS_I(inode)->generation;
  3953. btrfs_set_inode_space_info(root, inode);
  3954. if (mode & S_IFDIR)
  3955. owner = 0;
  3956. else
  3957. owner = 1;
  3958. BTRFS_I(inode)->block_group =
  3959. btrfs_find_block_group(root, 0, alloc_hint, owner);
  3960. key[0].objectid = objectid;
  3961. btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
  3962. key[0].offset = 0;
  3963. key[1].objectid = objectid;
  3964. btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
  3965. key[1].offset = ref_objectid;
  3966. sizes[0] = sizeof(struct btrfs_inode_item);
  3967. sizes[1] = name_len + sizeof(*ref);
  3968. path->leave_spinning = 1;
  3969. ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
  3970. if (ret != 0)
  3971. goto fail;
  3972. inode_init_owner(inode, dir, mode);
  3973. inode->i_ino = objectid;
  3974. inode_set_bytes(inode, 0);
  3975. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  3976. inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3977. struct btrfs_inode_item);
  3978. fill_inode_item(trans, path->nodes[0], inode_item, inode);
  3979. ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
  3980. struct btrfs_inode_ref);
  3981. btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
  3982. btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
  3983. ptr = (unsigned long)(ref + 1);
  3984. write_extent_buffer(path->nodes[0], name, ptr, name_len);
  3985. btrfs_mark_buffer_dirty(path->nodes[0]);
  3986. btrfs_free_path(path);
  3987. location = &BTRFS_I(inode)->location;
  3988. location->objectid = objectid;
  3989. location->offset = 0;
  3990. btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
  3991. btrfs_inherit_iflags(inode, dir);
  3992. if ((mode & S_IFREG)) {
  3993. if (btrfs_test_opt(root, NODATASUM))
  3994. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
  3995. if (btrfs_test_opt(root, NODATACOW))
  3996. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
  3997. }
  3998. insert_inode_hash(inode);
  3999. inode_tree_add(inode);
  4000. return inode;
  4001. fail:
  4002. if (dir)
  4003. BTRFS_I(dir)->index_cnt--;
  4004. btrfs_free_path(path);
  4005. iput(inode);
  4006. return ERR_PTR(ret);
  4007. }
  4008. static inline u8 btrfs_inode_type(struct inode *inode)
  4009. {
  4010. return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
  4011. }
  4012. /*
  4013. * utility function to add 'inode' into 'parent_inode' with
  4014. * a give name and a given sequence number.
  4015. * if 'add_backref' is true, also insert a backref from the
  4016. * inode to the parent directory.
  4017. */
  4018. int btrfs_add_link(struct btrfs_trans_handle *trans,
  4019. struct inode *parent_inode, struct inode *inode,
  4020. const char *name, int name_len, int add_backref, u64 index)
  4021. {
  4022. int ret = 0;
  4023. struct btrfs_key key;
  4024. struct btrfs_root *root = BTRFS_I(parent_inode)->root;
  4025. if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  4026. memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
  4027. } else {
  4028. key.objectid = inode->i_ino;
  4029. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  4030. key.offset = 0;
  4031. }
  4032. if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  4033. ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
  4034. key.objectid, root->root_key.objectid,
  4035. parent_inode->i_ino,
  4036. index, name, name_len);
  4037. } else if (add_backref) {
  4038. ret = btrfs_insert_inode_ref(trans, root,
  4039. name, name_len, inode->i_ino,
  4040. parent_inode->i_ino, index);
  4041. }
  4042. if (ret == 0) {
  4043. ret = btrfs_insert_dir_item(trans, root, name, name_len,
  4044. parent_inode->i_ino, &key,
  4045. btrfs_inode_type(inode), index);
  4046. BUG_ON(ret);
  4047. btrfs_i_size_write(parent_inode, parent_inode->i_size +
  4048. name_len * 2);
  4049. parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
  4050. ret = btrfs_update_inode(trans, root, parent_inode);
  4051. }
  4052. return ret;
  4053. }
  4054. static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
  4055. struct inode *dir, struct dentry *dentry,
  4056. struct inode *inode, int backref, u64 index)
  4057. {
  4058. int err = btrfs_add_link(trans, dir, inode,
  4059. dentry->d_name.name, dentry->d_name.len,
  4060. backref, index);
  4061. if (!err) {
  4062. d_instantiate(dentry, inode);
  4063. return 0;
  4064. }
  4065. if (err > 0)
  4066. err = -EEXIST;
  4067. return err;
  4068. }
  4069. static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
  4070. int mode, dev_t rdev)
  4071. {
  4072. struct btrfs_trans_handle *trans;
  4073. struct btrfs_root *root = BTRFS_I(dir)->root;
  4074. struct inode *inode = NULL;
  4075. int err;
  4076. int drop_inode = 0;
  4077. u64 objectid;
  4078. unsigned long nr = 0;
  4079. u64 index = 0;
  4080. if (!new_valid_dev(rdev))
  4081. return -EINVAL;
  4082. err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
  4083. if (err)
  4084. return err;
  4085. /*
  4086. * 2 for inode item and ref
  4087. * 2 for dir items
  4088. * 1 for xattr if selinux is on
  4089. */
  4090. trans = btrfs_start_transaction(root, 5);
  4091. if (IS_ERR(trans))
  4092. return PTR_ERR(trans);
  4093. btrfs_set_trans_block_group(trans, dir);
  4094. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  4095. dentry->d_name.len, dir->i_ino, objectid,
  4096. BTRFS_I(dir)->block_group, mode, &index);
  4097. err = PTR_ERR(inode);
  4098. if (IS_ERR(inode))
  4099. goto out_unlock;
  4100. err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
  4101. if (err) {
  4102. drop_inode = 1;
  4103. goto out_unlock;
  4104. }
  4105. btrfs_set_trans_block_group(trans, inode);
  4106. err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
  4107. if (err)
  4108. drop_inode = 1;
  4109. else {
  4110. inode->i_op = &btrfs_special_inode_operations;
  4111. init_special_inode(inode, inode->i_mode, rdev);
  4112. btrfs_update_inode(trans, root, inode);
  4113. }
  4114. btrfs_update_inode_block_group(trans, inode);
  4115. btrfs_update_inode_block_group(trans, dir);
  4116. out_unlock:
  4117. nr = trans->blocks_used;
  4118. btrfs_end_transaction_throttle(trans, root);
  4119. btrfs_btree_balance_dirty(root, nr);
  4120. if (drop_inode) {
  4121. inode_dec_link_count(inode);
  4122. iput(inode);
  4123. }
  4124. return err;
  4125. }
  4126. static int btrfs_create(struct inode *dir, struct dentry *dentry,
  4127. int mode, struct nameidata *nd)
  4128. {
  4129. struct btrfs_trans_handle *trans;
  4130. struct btrfs_root *root = BTRFS_I(dir)->root;
  4131. struct inode *inode = NULL;
  4132. int drop_inode = 0;
  4133. int err;
  4134. unsigned long nr = 0;
  4135. u64 objectid;
  4136. u64 index = 0;
  4137. err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
  4138. if (err)
  4139. return err;
  4140. /*
  4141. * 2 for inode item and ref
  4142. * 2 for dir items
  4143. * 1 for xattr if selinux is on
  4144. */
  4145. trans = btrfs_start_transaction(root, 5);
  4146. if (IS_ERR(trans))
  4147. return PTR_ERR(trans);
  4148. btrfs_set_trans_block_group(trans, dir);
  4149. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  4150. dentry->d_name.len, dir->i_ino, objectid,
  4151. BTRFS_I(dir)->block_group, mode, &index);
  4152. err = PTR_ERR(inode);
  4153. if (IS_ERR(inode))
  4154. goto out_unlock;
  4155. err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
  4156. if (err) {
  4157. drop_inode = 1;
  4158. goto out_unlock;
  4159. }
  4160. btrfs_set_trans_block_group(trans, inode);
  4161. err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
  4162. if (err)
  4163. drop_inode = 1;
  4164. else {
  4165. inode->i_mapping->a_ops = &btrfs_aops;
  4166. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  4167. inode->i_fop = &btrfs_file_operations;
  4168. inode->i_op = &btrfs_file_inode_operations;
  4169. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  4170. }
  4171. btrfs_update_inode_block_group(trans, inode);
  4172. btrfs_update_inode_block_group(trans, dir);
  4173. out_unlock:
  4174. nr = trans->blocks_used;
  4175. btrfs_end_transaction_throttle(trans, root);
  4176. if (drop_inode) {
  4177. inode_dec_link_count(inode);
  4178. iput(inode);
  4179. }
  4180. btrfs_btree_balance_dirty(root, nr);
  4181. return err;
  4182. }
  4183. static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
  4184. struct dentry *dentry)
  4185. {
  4186. struct btrfs_trans_handle *trans;
  4187. struct btrfs_root *root = BTRFS_I(dir)->root;
  4188. struct inode *inode = old_dentry->d_inode;
  4189. u64 index;
  4190. unsigned long nr = 0;
  4191. int err;
  4192. int drop_inode = 0;
  4193. if (inode->i_nlink == 0)
  4194. return -ENOENT;
  4195. /* do not allow sys_link's with other subvols of the same device */
  4196. if (root->objectid != BTRFS_I(inode)->root->objectid)
  4197. return -EPERM;
  4198. btrfs_inc_nlink(inode);
  4199. inode->i_ctime = CURRENT_TIME;
  4200. err = btrfs_set_inode_index(dir, &index);
  4201. if (err)
  4202. goto fail;
  4203. /*
  4204. * 1 item for inode ref
  4205. * 2 items for dir items
  4206. */
  4207. trans = btrfs_start_transaction(root, 3);
  4208. if (IS_ERR(trans)) {
  4209. err = PTR_ERR(trans);
  4210. goto fail;
  4211. }
  4212. btrfs_set_trans_block_group(trans, dir);
  4213. ihold(inode);
  4214. err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
  4215. if (err) {
  4216. drop_inode = 1;
  4217. } else {
  4218. struct dentry *parent = dget_parent(dentry);
  4219. btrfs_update_inode_block_group(trans, dir);
  4220. err = btrfs_update_inode(trans, root, inode);
  4221. BUG_ON(err);
  4222. btrfs_log_new_name(trans, inode, NULL, parent);
  4223. dput(parent);
  4224. }
  4225. nr = trans->blocks_used;
  4226. btrfs_end_transaction_throttle(trans, root);
  4227. fail:
  4228. if (drop_inode) {
  4229. inode_dec_link_count(inode);
  4230. iput(inode);
  4231. }
  4232. btrfs_btree_balance_dirty(root, nr);
  4233. return err;
  4234. }
  4235. static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  4236. {
  4237. struct inode *inode = NULL;
  4238. struct btrfs_trans_handle *trans;
  4239. struct btrfs_root *root = BTRFS_I(dir)->root;
  4240. int err = 0;
  4241. int drop_on_err = 0;
  4242. u64 objectid = 0;
  4243. u64 index = 0;
  4244. unsigned long nr = 1;
  4245. err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
  4246. if (err)
  4247. return err;
  4248. /*
  4249. * 2 items for inode and ref
  4250. * 2 items for dir items
  4251. * 1 for xattr if selinux is on
  4252. */
  4253. trans = btrfs_start_transaction(root, 5);
  4254. if (IS_ERR(trans))
  4255. return PTR_ERR(trans);
  4256. btrfs_set_trans_block_group(trans, dir);
  4257. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  4258. dentry->d_name.len, dir->i_ino, objectid,
  4259. BTRFS_I(dir)->block_group, S_IFDIR | mode,
  4260. &index);
  4261. if (IS_ERR(inode)) {
  4262. err = PTR_ERR(inode);
  4263. goto out_fail;
  4264. }
  4265. drop_on_err = 1;
  4266. err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
  4267. if (err)
  4268. goto out_fail;
  4269. inode->i_op = &btrfs_dir_inode_operations;
  4270. inode->i_fop = &btrfs_dir_file_operations;
  4271. btrfs_set_trans_block_group(trans, inode);
  4272. btrfs_i_size_write(inode, 0);
  4273. err = btrfs_update_inode(trans, root, inode);
  4274. if (err)
  4275. goto out_fail;
  4276. err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
  4277. dentry->d_name.len, 0, index);
  4278. if (err)
  4279. goto out_fail;
  4280. d_instantiate(dentry, inode);
  4281. drop_on_err = 0;
  4282. btrfs_update_inode_block_group(trans, inode);
  4283. btrfs_update_inode_block_group(trans, dir);
  4284. out_fail:
  4285. nr = trans->blocks_used;
  4286. btrfs_end_transaction_throttle(trans, root);
  4287. if (drop_on_err)
  4288. iput(inode);
  4289. btrfs_btree_balance_dirty(root, nr);
  4290. return err;
  4291. }
  4292. /* helper for btfs_get_extent. Given an existing extent in the tree,
  4293. * and an extent that you want to insert, deal with overlap and insert
  4294. * the new extent into the tree.
  4295. */
  4296. static int merge_extent_mapping(struct extent_map_tree *em_tree,
  4297. struct extent_map *existing,
  4298. struct extent_map *em,
  4299. u64 map_start, u64 map_len)
  4300. {
  4301. u64 start_diff;
  4302. BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
  4303. start_diff = map_start - em->start;
  4304. em->start = map_start;
  4305. em->len = map_len;
  4306. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  4307. !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  4308. em->block_start += start_diff;
  4309. em->block_len -= start_diff;
  4310. }
  4311. return add_extent_mapping(em_tree, em);
  4312. }
  4313. static noinline int uncompress_inline(struct btrfs_path *path,
  4314. struct inode *inode, struct page *page,
  4315. size_t pg_offset, u64 extent_offset,
  4316. struct btrfs_file_extent_item *item)
  4317. {
  4318. int ret;
  4319. struct extent_buffer *leaf = path->nodes[0];
  4320. char *tmp;
  4321. size_t max_size;
  4322. unsigned long inline_size;
  4323. unsigned long ptr;
  4324. WARN_ON(pg_offset != 0);
  4325. max_size = btrfs_file_extent_ram_bytes(leaf, item);
  4326. inline_size = btrfs_file_extent_inline_item_len(leaf,
  4327. btrfs_item_nr(leaf, path->slots[0]));
  4328. tmp = kmalloc(inline_size, GFP_NOFS);
  4329. ptr = btrfs_file_extent_inline_start(item);
  4330. read_extent_buffer(leaf, tmp, ptr, inline_size);
  4331. max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
  4332. ret = btrfs_zlib_decompress(tmp, page, extent_offset,
  4333. inline_size, max_size);
  4334. if (ret) {
  4335. char *kaddr = kmap_atomic(page, KM_USER0);
  4336. unsigned long copy_size = min_t(u64,
  4337. PAGE_CACHE_SIZE - pg_offset,
  4338. max_size - extent_offset);
  4339. memset(kaddr + pg_offset, 0, copy_size);
  4340. kunmap_atomic(kaddr, KM_USER0);
  4341. }
  4342. kfree(tmp);
  4343. return 0;
  4344. }
  4345. /*
  4346. * a bit scary, this does extent mapping from logical file offset to the disk.
  4347. * the ugly parts come from merging extents from the disk with the in-ram
  4348. * representation. This gets more complex because of the data=ordered code,
  4349. * where the in-ram extents might be locked pending data=ordered completion.
  4350. *
  4351. * This also copies inline extents directly into the page.
  4352. */
  4353. struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
  4354. size_t pg_offset, u64 start, u64 len,
  4355. int create)
  4356. {
  4357. int ret;
  4358. int err = 0;
  4359. u64 bytenr;
  4360. u64 extent_start = 0;
  4361. u64 extent_end = 0;
  4362. u64 objectid = inode->i_ino;
  4363. u32 found_type;
  4364. struct btrfs_path *path = NULL;
  4365. struct btrfs_root *root = BTRFS_I(inode)->root;
  4366. struct btrfs_file_extent_item *item;
  4367. struct extent_buffer *leaf;
  4368. struct btrfs_key found_key;
  4369. struct extent_map *em = NULL;
  4370. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  4371. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  4372. struct btrfs_trans_handle *trans = NULL;
  4373. int compressed;
  4374. again:
  4375. read_lock(&em_tree->lock);
  4376. em = lookup_extent_mapping(em_tree, start, len);
  4377. if (em)
  4378. em->bdev = root->fs_info->fs_devices->latest_bdev;
  4379. read_unlock(&em_tree->lock);
  4380. if (em) {
  4381. if (em->start > start || em->start + em->len <= start)
  4382. free_extent_map(em);
  4383. else if (em->block_start == EXTENT_MAP_INLINE && page)
  4384. free_extent_map(em);
  4385. else
  4386. goto out;
  4387. }
  4388. em = alloc_extent_map(GFP_NOFS);
  4389. if (!em) {
  4390. err = -ENOMEM;
  4391. goto out;
  4392. }
  4393. em->bdev = root->fs_info->fs_devices->latest_bdev;
  4394. em->start = EXTENT_MAP_HOLE;
  4395. em->orig_start = EXTENT_MAP_HOLE;
  4396. em->len = (u64)-1;
  4397. em->block_len = (u64)-1;
  4398. if (!path) {
  4399. path = btrfs_alloc_path();
  4400. BUG_ON(!path);
  4401. }
  4402. ret = btrfs_lookup_file_extent(trans, root, path,
  4403. objectid, start, trans != NULL);
  4404. if (ret < 0) {
  4405. err = ret;
  4406. goto out;
  4407. }
  4408. if (ret != 0) {
  4409. if (path->slots[0] == 0)
  4410. goto not_found;
  4411. path->slots[0]--;
  4412. }
  4413. leaf = path->nodes[0];
  4414. item = btrfs_item_ptr(leaf, path->slots[0],
  4415. struct btrfs_file_extent_item);
  4416. /* are we inside the extent that was found? */
  4417. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  4418. found_type = btrfs_key_type(&found_key);
  4419. if (found_key.objectid != objectid ||
  4420. found_type != BTRFS_EXTENT_DATA_KEY) {
  4421. goto not_found;
  4422. }
  4423. found_type = btrfs_file_extent_type(leaf, item);
  4424. extent_start = found_key.offset;
  4425. compressed = btrfs_file_extent_compression(leaf, item);
  4426. if (found_type == BTRFS_FILE_EXTENT_REG ||
  4427. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  4428. extent_end = extent_start +
  4429. btrfs_file_extent_num_bytes(leaf, item);
  4430. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  4431. size_t size;
  4432. size = btrfs_file_extent_inline_len(leaf, item);
  4433. extent_end = (extent_start + size + root->sectorsize - 1) &
  4434. ~((u64)root->sectorsize - 1);
  4435. }
  4436. if (start >= extent_end) {
  4437. path->slots[0]++;
  4438. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  4439. ret = btrfs_next_leaf(root, path);
  4440. if (ret < 0) {
  4441. err = ret;
  4442. goto out;
  4443. }
  4444. if (ret > 0)
  4445. goto not_found;
  4446. leaf = path->nodes[0];
  4447. }
  4448. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  4449. if (found_key.objectid != objectid ||
  4450. found_key.type != BTRFS_EXTENT_DATA_KEY)
  4451. goto not_found;
  4452. if (start + len <= found_key.offset)
  4453. goto not_found;
  4454. em->start = start;
  4455. em->len = found_key.offset - start;
  4456. goto not_found_em;
  4457. }
  4458. if (found_type == BTRFS_FILE_EXTENT_REG ||
  4459. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  4460. em->start = extent_start;
  4461. em->len = extent_end - extent_start;
  4462. em->orig_start = extent_start -
  4463. btrfs_file_extent_offset(leaf, item);
  4464. bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
  4465. if (bytenr == 0) {
  4466. em->block_start = EXTENT_MAP_HOLE;
  4467. goto insert;
  4468. }
  4469. if (compressed) {
  4470. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  4471. em->block_start = bytenr;
  4472. em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
  4473. item);
  4474. } else {
  4475. bytenr += btrfs_file_extent_offset(leaf, item);
  4476. em->block_start = bytenr;
  4477. em->block_len = em->len;
  4478. if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
  4479. set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
  4480. }
  4481. goto insert;
  4482. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  4483. unsigned long ptr;
  4484. char *map;
  4485. size_t size;
  4486. size_t extent_offset;
  4487. size_t copy_size;
  4488. em->block_start = EXTENT_MAP_INLINE;
  4489. if (!page || create) {
  4490. em->start = extent_start;
  4491. em->len = extent_end - extent_start;
  4492. goto out;
  4493. }
  4494. size = btrfs_file_extent_inline_len(leaf, item);
  4495. extent_offset = page_offset(page) + pg_offset - extent_start;
  4496. copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
  4497. size - extent_offset);
  4498. em->start = extent_start + extent_offset;
  4499. em->len = (copy_size + root->sectorsize - 1) &
  4500. ~((u64)root->sectorsize - 1);
  4501. em->orig_start = EXTENT_MAP_INLINE;
  4502. if (compressed)
  4503. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  4504. ptr = btrfs_file_extent_inline_start(item) + extent_offset;
  4505. if (create == 0 && !PageUptodate(page)) {
  4506. if (btrfs_file_extent_compression(leaf, item) ==
  4507. BTRFS_COMPRESS_ZLIB) {
  4508. ret = uncompress_inline(path, inode, page,
  4509. pg_offset,
  4510. extent_offset, item);
  4511. BUG_ON(ret);
  4512. } else {
  4513. map = kmap(page);
  4514. read_extent_buffer(leaf, map + pg_offset, ptr,
  4515. copy_size);
  4516. if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
  4517. memset(map + pg_offset + copy_size, 0,
  4518. PAGE_CACHE_SIZE - pg_offset -
  4519. copy_size);
  4520. }
  4521. kunmap(page);
  4522. }
  4523. flush_dcache_page(page);
  4524. } else if (create && PageUptodate(page)) {
  4525. WARN_ON(1);
  4526. if (!trans) {
  4527. kunmap(page);
  4528. free_extent_map(em);
  4529. em = NULL;
  4530. btrfs_release_path(root, path);
  4531. trans = btrfs_join_transaction(root, 1);
  4532. goto again;
  4533. }
  4534. map = kmap(page);
  4535. write_extent_buffer(leaf, map + pg_offset, ptr,
  4536. copy_size);
  4537. kunmap(page);
  4538. btrfs_mark_buffer_dirty(leaf);
  4539. }
  4540. set_extent_uptodate(io_tree, em->start,
  4541. extent_map_end(em) - 1, GFP_NOFS);
  4542. goto insert;
  4543. } else {
  4544. printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
  4545. WARN_ON(1);
  4546. }
  4547. not_found:
  4548. em->start = start;
  4549. em->len = len;
  4550. not_found_em:
  4551. em->block_start = EXTENT_MAP_HOLE;
  4552. set_bit(EXTENT_FLAG_VACANCY, &em->flags);
  4553. insert:
  4554. btrfs_release_path(root, path);
  4555. if (em->start > start || extent_map_end(em) <= start) {
  4556. printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
  4557. "[%llu %llu]\n", (unsigned long long)em->start,
  4558. (unsigned long long)em->len,
  4559. (unsigned long long)start,
  4560. (unsigned long long)len);
  4561. err = -EIO;
  4562. goto out;
  4563. }
  4564. err = 0;
  4565. write_lock(&em_tree->lock);
  4566. ret = add_extent_mapping(em_tree, em);
  4567. /* it is possible that someone inserted the extent into the tree
  4568. * while we had the lock dropped. It is also possible that
  4569. * an overlapping map exists in the tree
  4570. */
  4571. if (ret == -EEXIST) {
  4572. struct extent_map *existing;
  4573. ret = 0;
  4574. existing = lookup_extent_mapping(em_tree, start, len);
  4575. if (existing && (existing->start > start ||
  4576. existing->start + existing->len <= start)) {
  4577. free_extent_map(existing);
  4578. existing = NULL;
  4579. }
  4580. if (!existing) {
  4581. existing = lookup_extent_mapping(em_tree, em->start,
  4582. em->len);
  4583. if (existing) {
  4584. err = merge_extent_mapping(em_tree, existing,
  4585. em, start,
  4586. root->sectorsize);
  4587. free_extent_map(existing);
  4588. if (err) {
  4589. free_extent_map(em);
  4590. em = NULL;
  4591. }
  4592. } else {
  4593. err = -EIO;
  4594. free_extent_map(em);
  4595. em = NULL;
  4596. }
  4597. } else {
  4598. free_extent_map(em);
  4599. em = existing;
  4600. err = 0;
  4601. }
  4602. }
  4603. write_unlock(&em_tree->lock);
  4604. out:
  4605. if (path)
  4606. btrfs_free_path(path);
  4607. if (trans) {
  4608. ret = btrfs_end_transaction(trans, root);
  4609. if (!err)
  4610. err = ret;
  4611. }
  4612. if (err) {
  4613. free_extent_map(em);
  4614. return ERR_PTR(err);
  4615. }
  4616. return em;
  4617. }
  4618. static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
  4619. u64 start, u64 len)
  4620. {
  4621. struct btrfs_root *root = BTRFS_I(inode)->root;
  4622. struct btrfs_trans_handle *trans;
  4623. struct extent_map *em;
  4624. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  4625. struct btrfs_key ins;
  4626. u64 alloc_hint;
  4627. int ret;
  4628. btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
  4629. trans = btrfs_join_transaction(root, 0);
  4630. if (!trans)
  4631. return ERR_PTR(-ENOMEM);
  4632. trans->block_rsv = &root->fs_info->delalloc_block_rsv;
  4633. alloc_hint = get_extent_allocation_hint(inode, start, len);
  4634. ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
  4635. alloc_hint, (u64)-1, &ins, 1);
  4636. if (ret) {
  4637. em = ERR_PTR(ret);
  4638. goto out;
  4639. }
  4640. em = alloc_extent_map(GFP_NOFS);
  4641. if (!em) {
  4642. em = ERR_PTR(-ENOMEM);
  4643. goto out;
  4644. }
  4645. em->start = start;
  4646. em->orig_start = em->start;
  4647. em->len = ins.offset;
  4648. em->block_start = ins.objectid;
  4649. em->block_len = ins.offset;
  4650. em->bdev = root->fs_info->fs_devices->latest_bdev;
  4651. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  4652. while (1) {
  4653. write_lock(&em_tree->lock);
  4654. ret = add_extent_mapping(em_tree, em);
  4655. write_unlock(&em_tree->lock);
  4656. if (ret != -EEXIST)
  4657. break;
  4658. btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0);
  4659. }
  4660. ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
  4661. ins.offset, ins.offset, 0);
  4662. if (ret) {
  4663. btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
  4664. em = ERR_PTR(ret);
  4665. }
  4666. out:
  4667. btrfs_end_transaction(trans, root);
  4668. return em;
  4669. }
  4670. /*
  4671. * returns 1 when the nocow is safe, < 1 on error, 0 if the
  4672. * block must be cow'd
  4673. */
  4674. static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
  4675. struct inode *inode, u64 offset, u64 len)
  4676. {
  4677. struct btrfs_path *path;
  4678. int ret;
  4679. struct extent_buffer *leaf;
  4680. struct btrfs_root *root = BTRFS_I(inode)->root;
  4681. struct btrfs_file_extent_item *fi;
  4682. struct btrfs_key key;
  4683. u64 disk_bytenr;
  4684. u64 backref_offset;
  4685. u64 extent_end;
  4686. u64 num_bytes;
  4687. int slot;
  4688. int found_type;
  4689. path = btrfs_alloc_path();
  4690. if (!path)
  4691. return -ENOMEM;
  4692. ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
  4693. offset, 0);
  4694. if (ret < 0)
  4695. goto out;
  4696. slot = path->slots[0];
  4697. if (ret == 1) {
  4698. if (slot == 0) {
  4699. /* can't find the item, must cow */
  4700. ret = 0;
  4701. goto out;
  4702. }
  4703. slot--;
  4704. }
  4705. ret = 0;
  4706. leaf = path->nodes[0];
  4707. btrfs_item_key_to_cpu(leaf, &key, slot);
  4708. if (key.objectid != inode->i_ino ||
  4709. key.type != BTRFS_EXTENT_DATA_KEY) {
  4710. /* not our file or wrong item type, must cow */
  4711. goto out;
  4712. }
  4713. if (key.offset > offset) {
  4714. /* Wrong offset, must cow */
  4715. goto out;
  4716. }
  4717. fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  4718. found_type = btrfs_file_extent_type(leaf, fi);
  4719. if (found_type != BTRFS_FILE_EXTENT_REG &&
  4720. found_type != BTRFS_FILE_EXTENT_PREALLOC) {
  4721. /* not a regular extent, must cow */
  4722. goto out;
  4723. }
  4724. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  4725. backref_offset = btrfs_file_extent_offset(leaf, fi);
  4726. extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
  4727. if (extent_end < offset + len) {
  4728. /* extent doesn't include our full range, must cow */
  4729. goto out;
  4730. }
  4731. if (btrfs_extent_readonly(root, disk_bytenr))
  4732. goto out;
  4733. /*
  4734. * look for other files referencing this extent, if we
  4735. * find any we must cow
  4736. */
  4737. if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
  4738. key.offset - backref_offset, disk_bytenr))
  4739. goto out;
  4740. /*
  4741. * adjust disk_bytenr and num_bytes to cover just the bytes
  4742. * in this extent we are about to write. If there
  4743. * are any csums in that range we have to cow in order
  4744. * to keep the csums correct
  4745. */
  4746. disk_bytenr += backref_offset;
  4747. disk_bytenr += offset - key.offset;
  4748. num_bytes = min(offset + len, extent_end) - offset;
  4749. if (csum_exist_in_range(root, disk_bytenr, num_bytes))
  4750. goto out;
  4751. /*
  4752. * all of the above have passed, it is safe to overwrite this extent
  4753. * without cow
  4754. */
  4755. ret = 1;
  4756. out:
  4757. btrfs_free_path(path);
  4758. return ret;
  4759. }
  4760. static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
  4761. struct buffer_head *bh_result, int create)
  4762. {
  4763. struct extent_map *em;
  4764. struct btrfs_root *root = BTRFS_I(inode)->root;
  4765. u64 start = iblock << inode->i_blkbits;
  4766. u64 len = bh_result->b_size;
  4767. struct btrfs_trans_handle *trans;
  4768. em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
  4769. if (IS_ERR(em))
  4770. return PTR_ERR(em);
  4771. /*
  4772. * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
  4773. * io. INLINE is special, and we could probably kludge it in here, but
  4774. * it's still buffered so for safety lets just fall back to the generic
  4775. * buffered path.
  4776. *
  4777. * For COMPRESSED we _have_ to read the entire extent in so we can
  4778. * decompress it, so there will be buffering required no matter what we
  4779. * do, so go ahead and fallback to buffered.
  4780. *
  4781. * We return -ENOTBLK because thats what makes DIO go ahead and go back
  4782. * to buffered IO. Don't blame me, this is the price we pay for using
  4783. * the generic code.
  4784. */
  4785. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
  4786. em->block_start == EXTENT_MAP_INLINE) {
  4787. free_extent_map(em);
  4788. return -ENOTBLK;
  4789. }
  4790. /* Just a good old fashioned hole, return */
  4791. if (!create && (em->block_start == EXTENT_MAP_HOLE ||
  4792. test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
  4793. free_extent_map(em);
  4794. /* DIO will do one hole at a time, so just unlock a sector */
  4795. unlock_extent(&BTRFS_I(inode)->io_tree, start,
  4796. start + root->sectorsize - 1, GFP_NOFS);
  4797. return 0;
  4798. }
  4799. /*
  4800. * We don't allocate a new extent in the following cases
  4801. *
  4802. * 1) The inode is marked as NODATACOW. In this case we'll just use the
  4803. * existing extent.
  4804. * 2) The extent is marked as PREALLOC. We're good to go here and can
  4805. * just use the extent.
  4806. *
  4807. */
  4808. if (!create) {
  4809. len = em->len - (start - em->start);
  4810. goto map;
  4811. }
  4812. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
  4813. ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
  4814. em->block_start != EXTENT_MAP_HOLE)) {
  4815. int type;
  4816. int ret;
  4817. u64 block_start;
  4818. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  4819. type = BTRFS_ORDERED_PREALLOC;
  4820. else
  4821. type = BTRFS_ORDERED_NOCOW;
  4822. len = min(len, em->len - (start - em->start));
  4823. block_start = em->block_start + (start - em->start);
  4824. /*
  4825. * we're not going to log anything, but we do need
  4826. * to make sure the current transaction stays open
  4827. * while we look for nocow cross refs
  4828. */
  4829. trans = btrfs_join_transaction(root, 0);
  4830. if (!trans)
  4831. goto must_cow;
  4832. if (can_nocow_odirect(trans, inode, start, len) == 1) {
  4833. ret = btrfs_add_ordered_extent_dio(inode, start,
  4834. block_start, len, len, type);
  4835. btrfs_end_transaction(trans, root);
  4836. if (ret) {
  4837. free_extent_map(em);
  4838. return ret;
  4839. }
  4840. goto unlock;
  4841. }
  4842. btrfs_end_transaction(trans, root);
  4843. }
  4844. must_cow:
  4845. /*
  4846. * this will cow the extent, reset the len in case we changed
  4847. * it above
  4848. */
  4849. len = bh_result->b_size;
  4850. free_extent_map(em);
  4851. em = btrfs_new_extent_direct(inode, start, len);
  4852. if (IS_ERR(em))
  4853. return PTR_ERR(em);
  4854. len = min(len, em->len - (start - em->start));
  4855. unlock:
  4856. clear_extent_bit(&BTRFS_I(inode)->io_tree, start, start + len - 1,
  4857. EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DIRTY, 1,
  4858. 0, NULL, GFP_NOFS);
  4859. map:
  4860. bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
  4861. inode->i_blkbits;
  4862. bh_result->b_size = len;
  4863. bh_result->b_bdev = em->bdev;
  4864. set_buffer_mapped(bh_result);
  4865. if (create && !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  4866. set_buffer_new(bh_result);
  4867. free_extent_map(em);
  4868. return 0;
  4869. }
  4870. struct btrfs_dio_private {
  4871. struct inode *inode;
  4872. u64 logical_offset;
  4873. u64 disk_bytenr;
  4874. u64 bytes;
  4875. u32 *csums;
  4876. void *private;
  4877. /* number of bios pending for this dio */
  4878. atomic_t pending_bios;
  4879. /* IO errors */
  4880. int errors;
  4881. struct bio *orig_bio;
  4882. };
  4883. static void btrfs_endio_direct_read(struct bio *bio, int err)
  4884. {
  4885. struct btrfs_dio_private *dip = bio->bi_private;
  4886. struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
  4887. struct bio_vec *bvec = bio->bi_io_vec;
  4888. struct inode *inode = dip->inode;
  4889. struct btrfs_root *root = BTRFS_I(inode)->root;
  4890. u64 start;
  4891. u32 *private = dip->csums;
  4892. start = dip->logical_offset;
  4893. do {
  4894. if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
  4895. struct page *page = bvec->bv_page;
  4896. char *kaddr;
  4897. u32 csum = ~(u32)0;
  4898. unsigned long flags;
  4899. local_irq_save(flags);
  4900. kaddr = kmap_atomic(page, KM_IRQ0);
  4901. csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
  4902. csum, bvec->bv_len);
  4903. btrfs_csum_final(csum, (char *)&csum);
  4904. kunmap_atomic(kaddr, KM_IRQ0);
  4905. local_irq_restore(flags);
  4906. flush_dcache_page(bvec->bv_page);
  4907. if (csum != *private) {
  4908. printk(KERN_ERR "btrfs csum failed ino %lu off"
  4909. " %llu csum %u private %u\n",
  4910. inode->i_ino, (unsigned long long)start,
  4911. csum, *private);
  4912. err = -EIO;
  4913. }
  4914. }
  4915. start += bvec->bv_len;
  4916. private++;
  4917. bvec++;
  4918. } while (bvec <= bvec_end);
  4919. unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
  4920. dip->logical_offset + dip->bytes - 1, GFP_NOFS);
  4921. bio->bi_private = dip->private;
  4922. kfree(dip->csums);
  4923. kfree(dip);
  4924. dio_end_io(bio, err);
  4925. }
  4926. static void btrfs_endio_direct_write(struct bio *bio, int err)
  4927. {
  4928. struct btrfs_dio_private *dip = bio->bi_private;
  4929. struct inode *inode = dip->inode;
  4930. struct btrfs_root *root = BTRFS_I(inode)->root;
  4931. struct btrfs_trans_handle *trans;
  4932. struct btrfs_ordered_extent *ordered = NULL;
  4933. struct extent_state *cached_state = NULL;
  4934. u64 ordered_offset = dip->logical_offset;
  4935. u64 ordered_bytes = dip->bytes;
  4936. int ret;
  4937. if (err)
  4938. goto out_done;
  4939. again:
  4940. ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
  4941. &ordered_offset,
  4942. ordered_bytes);
  4943. if (!ret)
  4944. goto out_test;
  4945. BUG_ON(!ordered);
  4946. trans = btrfs_join_transaction(root, 1);
  4947. if (!trans) {
  4948. err = -ENOMEM;
  4949. goto out;
  4950. }
  4951. trans->block_rsv = &root->fs_info->delalloc_block_rsv;
  4952. if (test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) {
  4953. ret = btrfs_ordered_update_i_size(inode, 0, ordered);
  4954. if (!ret)
  4955. ret = btrfs_update_inode(trans, root, inode);
  4956. err = ret;
  4957. goto out;
  4958. }
  4959. lock_extent_bits(&BTRFS_I(inode)->io_tree, ordered->file_offset,
  4960. ordered->file_offset + ordered->len - 1, 0,
  4961. &cached_state, GFP_NOFS);
  4962. if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) {
  4963. ret = btrfs_mark_extent_written(trans, inode,
  4964. ordered->file_offset,
  4965. ordered->file_offset +
  4966. ordered->len);
  4967. if (ret) {
  4968. err = ret;
  4969. goto out_unlock;
  4970. }
  4971. } else {
  4972. ret = insert_reserved_file_extent(trans, inode,
  4973. ordered->file_offset,
  4974. ordered->start,
  4975. ordered->disk_len,
  4976. ordered->len,
  4977. ordered->len,
  4978. 0, 0, 0,
  4979. BTRFS_FILE_EXTENT_REG);
  4980. unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
  4981. ordered->file_offset, ordered->len);
  4982. if (ret) {
  4983. err = ret;
  4984. WARN_ON(1);
  4985. goto out_unlock;
  4986. }
  4987. }
  4988. add_pending_csums(trans, inode, ordered->file_offset, &ordered->list);
  4989. btrfs_ordered_update_i_size(inode, 0, ordered);
  4990. btrfs_update_inode(trans, root, inode);
  4991. out_unlock:
  4992. unlock_extent_cached(&BTRFS_I(inode)->io_tree, ordered->file_offset,
  4993. ordered->file_offset + ordered->len - 1,
  4994. &cached_state, GFP_NOFS);
  4995. out:
  4996. btrfs_delalloc_release_metadata(inode, ordered->len);
  4997. btrfs_end_transaction(trans, root);
  4998. ordered_offset = ordered->file_offset + ordered->len;
  4999. btrfs_put_ordered_extent(ordered);
  5000. btrfs_put_ordered_extent(ordered);
  5001. out_test:
  5002. /*
  5003. * our bio might span multiple ordered extents. If we haven't
  5004. * completed the accounting for the whole dio, go back and try again
  5005. */
  5006. if (ordered_offset < dip->logical_offset + dip->bytes) {
  5007. ordered_bytes = dip->logical_offset + dip->bytes -
  5008. ordered_offset;
  5009. goto again;
  5010. }
  5011. out_done:
  5012. bio->bi_private = dip->private;
  5013. kfree(dip->csums);
  5014. kfree(dip);
  5015. dio_end_io(bio, err);
  5016. }
  5017. static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
  5018. struct bio *bio, int mirror_num,
  5019. unsigned long bio_flags, u64 offset)
  5020. {
  5021. int ret;
  5022. struct btrfs_root *root = BTRFS_I(inode)->root;
  5023. ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
  5024. BUG_ON(ret);
  5025. return 0;
  5026. }
  5027. static void btrfs_end_dio_bio(struct bio *bio, int err)
  5028. {
  5029. struct btrfs_dio_private *dip = bio->bi_private;
  5030. if (err) {
  5031. printk(KERN_ERR "btrfs direct IO failed ino %lu rw %lu "
  5032. "sector %#Lx len %u err no %d\n",
  5033. dip->inode->i_ino, bio->bi_rw,
  5034. (unsigned long long)bio->bi_sector, bio->bi_size, err);
  5035. dip->errors = 1;
  5036. /*
  5037. * before atomic variable goto zero, we must make sure
  5038. * dip->errors is perceived to be set.
  5039. */
  5040. smp_mb__before_atomic_dec();
  5041. }
  5042. /* if there are more bios still pending for this dio, just exit */
  5043. if (!atomic_dec_and_test(&dip->pending_bios))
  5044. goto out;
  5045. if (dip->errors)
  5046. bio_io_error(dip->orig_bio);
  5047. else {
  5048. set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
  5049. bio_endio(dip->orig_bio, 0);
  5050. }
  5051. out:
  5052. bio_put(bio);
  5053. }
  5054. static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
  5055. u64 first_sector, gfp_t gfp_flags)
  5056. {
  5057. int nr_vecs = bio_get_nr_vecs(bdev);
  5058. return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
  5059. }
  5060. static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
  5061. int rw, u64 file_offset, int skip_sum,
  5062. u32 *csums)
  5063. {
  5064. int write = rw & REQ_WRITE;
  5065. struct btrfs_root *root = BTRFS_I(inode)->root;
  5066. int ret;
  5067. bio_get(bio);
  5068. ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
  5069. if (ret)
  5070. goto err;
  5071. if (write && !skip_sum) {
  5072. ret = btrfs_wq_submit_bio(root->fs_info,
  5073. inode, rw, bio, 0, 0,
  5074. file_offset,
  5075. __btrfs_submit_bio_start_direct_io,
  5076. __btrfs_submit_bio_done);
  5077. goto err;
  5078. } else if (!skip_sum)
  5079. btrfs_lookup_bio_sums_dio(root, inode, bio,
  5080. file_offset, csums);
  5081. ret = btrfs_map_bio(root, rw, bio, 0, 1);
  5082. err:
  5083. bio_put(bio);
  5084. return ret;
  5085. }
  5086. static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
  5087. int skip_sum)
  5088. {
  5089. struct inode *inode = dip->inode;
  5090. struct btrfs_root *root = BTRFS_I(inode)->root;
  5091. struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
  5092. struct bio *bio;
  5093. struct bio *orig_bio = dip->orig_bio;
  5094. struct bio_vec *bvec = orig_bio->bi_io_vec;
  5095. u64 start_sector = orig_bio->bi_sector;
  5096. u64 file_offset = dip->logical_offset;
  5097. u64 submit_len = 0;
  5098. u64 map_length;
  5099. int nr_pages = 0;
  5100. u32 *csums = dip->csums;
  5101. int ret = 0;
  5102. bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
  5103. if (!bio)
  5104. return -ENOMEM;
  5105. bio->bi_private = dip;
  5106. bio->bi_end_io = btrfs_end_dio_bio;
  5107. atomic_inc(&dip->pending_bios);
  5108. map_length = orig_bio->bi_size;
  5109. ret = btrfs_map_block(map_tree, READ, start_sector << 9,
  5110. &map_length, NULL, 0);
  5111. if (ret) {
  5112. bio_put(bio);
  5113. return -EIO;
  5114. }
  5115. while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
  5116. if (unlikely(map_length < submit_len + bvec->bv_len ||
  5117. bio_add_page(bio, bvec->bv_page, bvec->bv_len,
  5118. bvec->bv_offset) < bvec->bv_len)) {
  5119. /*
  5120. * inc the count before we submit the bio so
  5121. * we know the end IO handler won't happen before
  5122. * we inc the count. Otherwise, the dip might get freed
  5123. * before we're done setting it up
  5124. */
  5125. atomic_inc(&dip->pending_bios);
  5126. ret = __btrfs_submit_dio_bio(bio, inode, rw,
  5127. file_offset, skip_sum,
  5128. csums);
  5129. if (ret) {
  5130. bio_put(bio);
  5131. atomic_dec(&dip->pending_bios);
  5132. goto out_err;
  5133. }
  5134. if (!skip_sum)
  5135. csums = csums + nr_pages;
  5136. start_sector += submit_len >> 9;
  5137. file_offset += submit_len;
  5138. submit_len = 0;
  5139. nr_pages = 0;
  5140. bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
  5141. start_sector, GFP_NOFS);
  5142. if (!bio)
  5143. goto out_err;
  5144. bio->bi_private = dip;
  5145. bio->bi_end_io = btrfs_end_dio_bio;
  5146. map_length = orig_bio->bi_size;
  5147. ret = btrfs_map_block(map_tree, READ, start_sector << 9,
  5148. &map_length, NULL, 0);
  5149. if (ret) {
  5150. bio_put(bio);
  5151. goto out_err;
  5152. }
  5153. } else {
  5154. submit_len += bvec->bv_len;
  5155. nr_pages ++;
  5156. bvec++;
  5157. }
  5158. }
  5159. ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
  5160. csums);
  5161. if (!ret)
  5162. return 0;
  5163. bio_put(bio);
  5164. out_err:
  5165. dip->errors = 1;
  5166. /*
  5167. * before atomic variable goto zero, we must
  5168. * make sure dip->errors is perceived to be set.
  5169. */
  5170. smp_mb__before_atomic_dec();
  5171. if (atomic_dec_and_test(&dip->pending_bios))
  5172. bio_io_error(dip->orig_bio);
  5173. /* bio_end_io() will handle error, so we needn't return it */
  5174. return 0;
  5175. }
  5176. static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
  5177. loff_t file_offset)
  5178. {
  5179. struct btrfs_root *root = BTRFS_I(inode)->root;
  5180. struct btrfs_dio_private *dip;
  5181. struct bio_vec *bvec = bio->bi_io_vec;
  5182. int skip_sum;
  5183. int write = rw & REQ_WRITE;
  5184. int ret = 0;
  5185. skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  5186. dip = kmalloc(sizeof(*dip), GFP_NOFS);
  5187. if (!dip) {
  5188. ret = -ENOMEM;
  5189. goto free_ordered;
  5190. }
  5191. dip->csums = NULL;
  5192. if (!skip_sum) {
  5193. dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS);
  5194. if (!dip->csums) {
  5195. ret = -ENOMEM;
  5196. goto free_ordered;
  5197. }
  5198. }
  5199. dip->private = bio->bi_private;
  5200. dip->inode = inode;
  5201. dip->logical_offset = file_offset;
  5202. dip->bytes = 0;
  5203. do {
  5204. dip->bytes += bvec->bv_len;
  5205. bvec++;
  5206. } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
  5207. dip->disk_bytenr = (u64)bio->bi_sector << 9;
  5208. bio->bi_private = dip;
  5209. dip->errors = 0;
  5210. dip->orig_bio = bio;
  5211. atomic_set(&dip->pending_bios, 0);
  5212. if (write)
  5213. bio->bi_end_io = btrfs_endio_direct_write;
  5214. else
  5215. bio->bi_end_io = btrfs_endio_direct_read;
  5216. ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
  5217. if (!ret)
  5218. return;
  5219. free_ordered:
  5220. /*
  5221. * If this is a write, we need to clean up the reserved space and kill
  5222. * the ordered extent.
  5223. */
  5224. if (write) {
  5225. struct btrfs_ordered_extent *ordered;
  5226. ordered = btrfs_lookup_ordered_extent(inode, file_offset);
  5227. if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
  5228. !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
  5229. btrfs_free_reserved_extent(root, ordered->start,
  5230. ordered->disk_len);
  5231. btrfs_put_ordered_extent(ordered);
  5232. btrfs_put_ordered_extent(ordered);
  5233. }
  5234. bio_endio(bio, ret);
  5235. }
  5236. static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
  5237. const struct iovec *iov, loff_t offset,
  5238. unsigned long nr_segs)
  5239. {
  5240. int seg;
  5241. size_t size;
  5242. unsigned long addr;
  5243. unsigned blocksize_mask = root->sectorsize - 1;
  5244. ssize_t retval = -EINVAL;
  5245. loff_t end = offset;
  5246. if (offset & blocksize_mask)
  5247. goto out;
  5248. /* Check the memory alignment. Blocks cannot straddle pages */
  5249. for (seg = 0; seg < nr_segs; seg++) {
  5250. addr = (unsigned long)iov[seg].iov_base;
  5251. size = iov[seg].iov_len;
  5252. end += size;
  5253. if ((addr & blocksize_mask) || (size & blocksize_mask))
  5254. goto out;
  5255. }
  5256. retval = 0;
  5257. out:
  5258. return retval;
  5259. }
  5260. static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
  5261. const struct iovec *iov, loff_t offset,
  5262. unsigned long nr_segs)
  5263. {
  5264. struct file *file = iocb->ki_filp;
  5265. struct inode *inode = file->f_mapping->host;
  5266. struct btrfs_ordered_extent *ordered;
  5267. struct extent_state *cached_state = NULL;
  5268. u64 lockstart, lockend;
  5269. ssize_t ret;
  5270. int writing = rw & WRITE;
  5271. int write_bits = 0;
  5272. size_t count = iov_length(iov, nr_segs);
  5273. if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
  5274. offset, nr_segs)) {
  5275. return 0;
  5276. }
  5277. lockstart = offset;
  5278. lockend = offset + count - 1;
  5279. if (writing) {
  5280. ret = btrfs_delalloc_reserve_space(inode, count);
  5281. if (ret)
  5282. goto out;
  5283. }
  5284. while (1) {
  5285. lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  5286. 0, &cached_state, GFP_NOFS);
  5287. /*
  5288. * We're concerned with the entire range that we're going to be
  5289. * doing DIO to, so we need to make sure theres no ordered
  5290. * extents in this range.
  5291. */
  5292. ordered = btrfs_lookup_ordered_range(inode, lockstart,
  5293. lockend - lockstart + 1);
  5294. if (!ordered)
  5295. break;
  5296. unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  5297. &cached_state, GFP_NOFS);
  5298. btrfs_start_ordered_extent(inode, ordered, 1);
  5299. btrfs_put_ordered_extent(ordered);
  5300. cond_resched();
  5301. }
  5302. /*
  5303. * we don't use btrfs_set_extent_delalloc because we don't want
  5304. * the dirty or uptodate bits
  5305. */
  5306. if (writing) {
  5307. write_bits = EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING;
  5308. ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  5309. EXTENT_DELALLOC, 0, NULL, &cached_state,
  5310. GFP_NOFS);
  5311. if (ret) {
  5312. clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
  5313. lockend, EXTENT_LOCKED | write_bits,
  5314. 1, 0, &cached_state, GFP_NOFS);
  5315. goto out;
  5316. }
  5317. }
  5318. free_extent_state(cached_state);
  5319. cached_state = NULL;
  5320. ret = __blockdev_direct_IO(rw, iocb, inode,
  5321. BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
  5322. iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
  5323. btrfs_submit_direct, 0);
  5324. if (ret < 0 && ret != -EIOCBQUEUED) {
  5325. clear_extent_bit(&BTRFS_I(inode)->io_tree, offset,
  5326. offset + iov_length(iov, nr_segs) - 1,
  5327. EXTENT_LOCKED | write_bits, 1, 0,
  5328. &cached_state, GFP_NOFS);
  5329. } else if (ret >= 0 && ret < iov_length(iov, nr_segs)) {
  5330. /*
  5331. * We're falling back to buffered, unlock the section we didn't
  5332. * do IO on.
  5333. */
  5334. clear_extent_bit(&BTRFS_I(inode)->io_tree, offset + ret,
  5335. offset + iov_length(iov, nr_segs) - 1,
  5336. EXTENT_LOCKED | write_bits, 1, 0,
  5337. &cached_state, GFP_NOFS);
  5338. }
  5339. out:
  5340. free_extent_state(cached_state);
  5341. return ret;
  5342. }
  5343. static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  5344. __u64 start, __u64 len)
  5345. {
  5346. return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent);
  5347. }
  5348. int btrfs_readpage(struct file *file, struct page *page)
  5349. {
  5350. struct extent_io_tree *tree;
  5351. tree = &BTRFS_I(page->mapping->host)->io_tree;
  5352. return extent_read_full_page(tree, page, btrfs_get_extent);
  5353. }
  5354. static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
  5355. {
  5356. struct extent_io_tree *tree;
  5357. if (current->flags & PF_MEMALLOC) {
  5358. redirty_page_for_writepage(wbc, page);
  5359. unlock_page(page);
  5360. return 0;
  5361. }
  5362. tree = &BTRFS_I(page->mapping->host)->io_tree;
  5363. return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
  5364. }
  5365. int btrfs_writepages(struct address_space *mapping,
  5366. struct writeback_control *wbc)
  5367. {
  5368. struct extent_io_tree *tree;
  5369. tree = &BTRFS_I(mapping->host)->io_tree;
  5370. return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
  5371. }
  5372. static int
  5373. btrfs_readpages(struct file *file, struct address_space *mapping,
  5374. struct list_head *pages, unsigned nr_pages)
  5375. {
  5376. struct extent_io_tree *tree;
  5377. tree = &BTRFS_I(mapping->host)->io_tree;
  5378. return extent_readpages(tree, mapping, pages, nr_pages,
  5379. btrfs_get_extent);
  5380. }
  5381. static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  5382. {
  5383. struct extent_io_tree *tree;
  5384. struct extent_map_tree *map;
  5385. int ret;
  5386. tree = &BTRFS_I(page->mapping->host)->io_tree;
  5387. map = &BTRFS_I(page->mapping->host)->extent_tree;
  5388. ret = try_release_extent_mapping(map, tree, page, gfp_flags);
  5389. if (ret == 1) {
  5390. ClearPagePrivate(page);
  5391. set_page_private(page, 0);
  5392. page_cache_release(page);
  5393. }
  5394. return ret;
  5395. }
  5396. static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  5397. {
  5398. if (PageWriteback(page) || PageDirty(page))
  5399. return 0;
  5400. return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
  5401. }
  5402. static void btrfs_invalidatepage(struct page *page, unsigned long offset)
  5403. {
  5404. struct extent_io_tree *tree;
  5405. struct btrfs_ordered_extent *ordered;
  5406. struct extent_state *cached_state = NULL;
  5407. u64 page_start = page_offset(page);
  5408. u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
  5409. /*
  5410. * we have the page locked, so new writeback can't start,
  5411. * and the dirty bit won't be cleared while we are here.
  5412. *
  5413. * Wait for IO on this page so that we can safely clear
  5414. * the PagePrivate2 bit and do ordered accounting
  5415. */
  5416. wait_on_page_writeback(page);
  5417. tree = &BTRFS_I(page->mapping->host)->io_tree;
  5418. if (offset) {
  5419. btrfs_releasepage(page, GFP_NOFS);
  5420. return;
  5421. }
  5422. lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
  5423. GFP_NOFS);
  5424. ordered = btrfs_lookup_ordered_extent(page->mapping->host,
  5425. page_offset(page));
  5426. if (ordered) {
  5427. /*
  5428. * IO on this page will never be started, so we need
  5429. * to account for any ordered extents now
  5430. */
  5431. clear_extent_bit(tree, page_start, page_end,
  5432. EXTENT_DIRTY | EXTENT_DELALLOC |
  5433. EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
  5434. &cached_state, GFP_NOFS);
  5435. /*
  5436. * whoever cleared the private bit is responsible
  5437. * for the finish_ordered_io
  5438. */
  5439. if (TestClearPagePrivate2(page)) {
  5440. btrfs_finish_ordered_io(page->mapping->host,
  5441. page_start, page_end);
  5442. }
  5443. btrfs_put_ordered_extent(ordered);
  5444. cached_state = NULL;
  5445. lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
  5446. GFP_NOFS);
  5447. }
  5448. clear_extent_bit(tree, page_start, page_end,
  5449. EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
  5450. EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS);
  5451. __btrfs_releasepage(page, GFP_NOFS);
  5452. ClearPageChecked(page);
  5453. if (PagePrivate(page)) {
  5454. ClearPagePrivate(page);
  5455. set_page_private(page, 0);
  5456. page_cache_release(page);
  5457. }
  5458. }
  5459. /*
  5460. * btrfs_page_mkwrite() is not allowed to change the file size as it gets
  5461. * called from a page fault handler when a page is first dirtied. Hence we must
  5462. * be careful to check for EOF conditions here. We set the page up correctly
  5463. * for a written page which means we get ENOSPC checking when writing into
  5464. * holes and correct delalloc and unwritten extent mapping on filesystems that
  5465. * support these features.
  5466. *
  5467. * We are not allowed to take the i_mutex here so we have to play games to
  5468. * protect against truncate races as the page could now be beyond EOF. Because
  5469. * vmtruncate() writes the inode size before removing pages, once we have the
  5470. * page lock we can determine safely if the page is beyond EOF. If it is not
  5471. * beyond EOF, then the page is guaranteed safe against truncation until we
  5472. * unlock the page.
  5473. */
  5474. int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  5475. {
  5476. struct page *page = vmf->page;
  5477. struct inode *inode = fdentry(vma->vm_file)->d_inode;
  5478. struct btrfs_root *root = BTRFS_I(inode)->root;
  5479. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  5480. struct btrfs_ordered_extent *ordered;
  5481. struct extent_state *cached_state = NULL;
  5482. char *kaddr;
  5483. unsigned long zero_start;
  5484. loff_t size;
  5485. int ret;
  5486. u64 page_start;
  5487. u64 page_end;
  5488. ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
  5489. if (ret) {
  5490. if (ret == -ENOMEM)
  5491. ret = VM_FAULT_OOM;
  5492. else /* -ENOSPC, -EIO, etc */
  5493. ret = VM_FAULT_SIGBUS;
  5494. goto out;
  5495. }
  5496. ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
  5497. again:
  5498. lock_page(page);
  5499. size = i_size_read(inode);
  5500. page_start = page_offset(page);
  5501. page_end = page_start + PAGE_CACHE_SIZE - 1;
  5502. if ((page->mapping != inode->i_mapping) ||
  5503. (page_start >= size)) {
  5504. /* page got truncated out from underneath us */
  5505. goto out_unlock;
  5506. }
  5507. wait_on_page_writeback(page);
  5508. lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
  5509. GFP_NOFS);
  5510. set_page_extent_mapped(page);
  5511. /*
  5512. * we can't set the delalloc bits if there are pending ordered
  5513. * extents. Drop our locks and wait for them to finish
  5514. */
  5515. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  5516. if (ordered) {
  5517. unlock_extent_cached(io_tree, page_start, page_end,
  5518. &cached_state, GFP_NOFS);
  5519. unlock_page(page);
  5520. btrfs_start_ordered_extent(inode, ordered, 1);
  5521. btrfs_put_ordered_extent(ordered);
  5522. goto again;
  5523. }
  5524. /*
  5525. * XXX - page_mkwrite gets called every time the page is dirtied, even
  5526. * if it was already dirty, so for space accounting reasons we need to
  5527. * clear any delalloc bits for the range we are fixing to save. There
  5528. * is probably a better way to do this, but for now keep consistent with
  5529. * prepare_pages in the normal write path.
  5530. */
  5531. clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
  5532. EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
  5533. 0, 0, &cached_state, GFP_NOFS);
  5534. ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
  5535. &cached_state);
  5536. if (ret) {
  5537. unlock_extent_cached(io_tree, page_start, page_end,
  5538. &cached_state, GFP_NOFS);
  5539. ret = VM_FAULT_SIGBUS;
  5540. goto out_unlock;
  5541. }
  5542. ret = 0;
  5543. /* page is wholly or partially inside EOF */
  5544. if (page_start + PAGE_CACHE_SIZE > size)
  5545. zero_start = size & ~PAGE_CACHE_MASK;
  5546. else
  5547. zero_start = PAGE_CACHE_SIZE;
  5548. if (zero_start != PAGE_CACHE_SIZE) {
  5549. kaddr = kmap(page);
  5550. memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
  5551. flush_dcache_page(page);
  5552. kunmap(page);
  5553. }
  5554. ClearPageChecked(page);
  5555. set_page_dirty(page);
  5556. SetPageUptodate(page);
  5557. BTRFS_I(inode)->last_trans = root->fs_info->generation;
  5558. BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
  5559. unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
  5560. out_unlock:
  5561. if (!ret)
  5562. return VM_FAULT_LOCKED;
  5563. unlock_page(page);
  5564. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  5565. out:
  5566. return ret;
  5567. }
  5568. static void btrfs_truncate(struct inode *inode)
  5569. {
  5570. struct btrfs_root *root = BTRFS_I(inode)->root;
  5571. int ret;
  5572. struct btrfs_trans_handle *trans;
  5573. unsigned long nr;
  5574. u64 mask = root->sectorsize - 1;
  5575. if (!S_ISREG(inode->i_mode)) {
  5576. WARN_ON(1);
  5577. return;
  5578. }
  5579. ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
  5580. if (ret)
  5581. return;
  5582. btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
  5583. btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
  5584. trans = btrfs_start_transaction(root, 0);
  5585. BUG_ON(IS_ERR(trans));
  5586. btrfs_set_trans_block_group(trans, inode);
  5587. trans->block_rsv = root->orphan_block_rsv;
  5588. /*
  5589. * setattr is responsible for setting the ordered_data_close flag,
  5590. * but that is only tested during the last file release. That
  5591. * could happen well after the next commit, leaving a great big
  5592. * window where new writes may get lost if someone chooses to write
  5593. * to this file after truncating to zero
  5594. *
  5595. * The inode doesn't have any dirty data here, and so if we commit
  5596. * this is a noop. If someone immediately starts writing to the inode
  5597. * it is very likely we'll catch some of their writes in this
  5598. * transaction, and the commit will find this file on the ordered
  5599. * data list with good things to send down.
  5600. *
  5601. * This is a best effort solution, there is still a window where
  5602. * using truncate to replace the contents of the file will
  5603. * end up with a zero length file after a crash.
  5604. */
  5605. if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
  5606. btrfs_add_ordered_operation(trans, root, inode);
  5607. while (1) {
  5608. if (!trans) {
  5609. trans = btrfs_start_transaction(root, 0);
  5610. BUG_ON(IS_ERR(trans));
  5611. btrfs_set_trans_block_group(trans, inode);
  5612. trans->block_rsv = root->orphan_block_rsv;
  5613. }
  5614. ret = btrfs_block_rsv_check(trans, root,
  5615. root->orphan_block_rsv, 0, 5);
  5616. if (ret) {
  5617. BUG_ON(ret != -EAGAIN);
  5618. ret = btrfs_commit_transaction(trans, root);
  5619. BUG_ON(ret);
  5620. trans = NULL;
  5621. continue;
  5622. }
  5623. ret = btrfs_truncate_inode_items(trans, root, inode,
  5624. inode->i_size,
  5625. BTRFS_EXTENT_DATA_KEY);
  5626. if (ret != -EAGAIN)
  5627. break;
  5628. ret = btrfs_update_inode(trans, root, inode);
  5629. BUG_ON(ret);
  5630. nr = trans->blocks_used;
  5631. btrfs_end_transaction(trans, root);
  5632. trans = NULL;
  5633. btrfs_btree_balance_dirty(root, nr);
  5634. }
  5635. if (ret == 0 && inode->i_nlink > 0) {
  5636. ret = btrfs_orphan_del(trans, inode);
  5637. BUG_ON(ret);
  5638. }
  5639. ret = btrfs_update_inode(trans, root, inode);
  5640. BUG_ON(ret);
  5641. nr = trans->blocks_used;
  5642. ret = btrfs_end_transaction_throttle(trans, root);
  5643. BUG_ON(ret);
  5644. btrfs_btree_balance_dirty(root, nr);
  5645. }
  5646. /*
  5647. * create a new subvolume directory/inode (helper for the ioctl).
  5648. */
  5649. int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
  5650. struct btrfs_root *new_root,
  5651. u64 new_dirid, u64 alloc_hint)
  5652. {
  5653. struct inode *inode;
  5654. int err;
  5655. u64 index = 0;
  5656. inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
  5657. new_dirid, alloc_hint, S_IFDIR | 0700, &index);
  5658. if (IS_ERR(inode))
  5659. return PTR_ERR(inode);
  5660. inode->i_op = &btrfs_dir_inode_operations;
  5661. inode->i_fop = &btrfs_dir_file_operations;
  5662. inode->i_nlink = 1;
  5663. btrfs_i_size_write(inode, 0);
  5664. err = btrfs_update_inode(trans, new_root, inode);
  5665. BUG_ON(err);
  5666. iput(inode);
  5667. return 0;
  5668. }
  5669. /* helper function for file defrag and space balancing. This
  5670. * forces readahead on a given range of bytes in an inode
  5671. */
  5672. unsigned long btrfs_force_ra(struct address_space *mapping,
  5673. struct file_ra_state *ra, struct file *file,
  5674. pgoff_t offset, pgoff_t last_index)
  5675. {
  5676. pgoff_t req_size = last_index - offset + 1;
  5677. page_cache_sync_readahead(mapping, ra, file, offset, req_size);
  5678. return offset + req_size;
  5679. }
  5680. struct inode *btrfs_alloc_inode(struct super_block *sb)
  5681. {
  5682. struct btrfs_inode *ei;
  5683. struct inode *inode;
  5684. ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
  5685. if (!ei)
  5686. return NULL;
  5687. ei->root = NULL;
  5688. ei->space_info = NULL;
  5689. ei->generation = 0;
  5690. ei->sequence = 0;
  5691. ei->last_trans = 0;
  5692. ei->last_sub_trans = 0;
  5693. ei->logged_trans = 0;
  5694. ei->delalloc_bytes = 0;
  5695. ei->reserved_bytes = 0;
  5696. ei->disk_i_size = 0;
  5697. ei->flags = 0;
  5698. ei->index_cnt = (u64)-1;
  5699. ei->last_unlink_trans = 0;
  5700. spin_lock_init(&ei->accounting_lock);
  5701. atomic_set(&ei->outstanding_extents, 0);
  5702. ei->reserved_extents = 0;
  5703. ei->ordered_data_close = 0;
  5704. ei->orphan_meta_reserved = 0;
  5705. ei->dummy_inode = 0;
  5706. ei->force_compress = 0;
  5707. inode = &ei->vfs_inode;
  5708. extent_map_tree_init(&ei->extent_tree, GFP_NOFS);
  5709. extent_io_tree_init(&ei->io_tree, &inode->i_data, GFP_NOFS);
  5710. extent_io_tree_init(&ei->io_failure_tree, &inode->i_data, GFP_NOFS);
  5711. mutex_init(&ei->log_mutex);
  5712. btrfs_ordered_inode_tree_init(&ei->ordered_tree);
  5713. INIT_LIST_HEAD(&ei->i_orphan);
  5714. INIT_LIST_HEAD(&ei->delalloc_inodes);
  5715. INIT_LIST_HEAD(&ei->ordered_operations);
  5716. RB_CLEAR_NODE(&ei->rb_node);
  5717. return inode;
  5718. }
  5719. static void btrfs_i_callback(struct rcu_head *head)
  5720. {
  5721. struct inode *inode = container_of(head, struct inode, i_rcu);
  5722. INIT_LIST_HEAD(&inode->i_dentry);
  5723. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  5724. }
  5725. void btrfs_destroy_inode(struct inode *inode)
  5726. {
  5727. struct btrfs_ordered_extent *ordered;
  5728. struct btrfs_root *root = BTRFS_I(inode)->root;
  5729. WARN_ON(!list_empty(&inode->i_dentry));
  5730. WARN_ON(inode->i_data.nrpages);
  5731. WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents));
  5732. WARN_ON(BTRFS_I(inode)->reserved_extents);
  5733. /*
  5734. * This can happen where we create an inode, but somebody else also
  5735. * created the same inode and we need to destroy the one we already
  5736. * created.
  5737. */
  5738. if (!root)
  5739. goto free;
  5740. /*
  5741. * Make sure we're properly removed from the ordered operation
  5742. * lists.
  5743. */
  5744. smp_mb();
  5745. if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
  5746. spin_lock(&root->fs_info->ordered_extent_lock);
  5747. list_del_init(&BTRFS_I(inode)->ordered_operations);
  5748. spin_unlock(&root->fs_info->ordered_extent_lock);
  5749. }
  5750. if (root == root->fs_info->tree_root) {
  5751. struct btrfs_block_group_cache *block_group;
  5752. block_group = btrfs_lookup_block_group(root->fs_info,
  5753. BTRFS_I(inode)->block_group);
  5754. if (block_group && block_group->inode == inode) {
  5755. spin_lock(&block_group->lock);
  5756. block_group->inode = NULL;
  5757. spin_unlock(&block_group->lock);
  5758. btrfs_put_block_group(block_group);
  5759. } else if (block_group) {
  5760. btrfs_put_block_group(block_group);
  5761. }
  5762. }
  5763. spin_lock(&root->orphan_lock);
  5764. if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
  5765. printk(KERN_INFO "BTRFS: inode %lu still on the orphan list\n",
  5766. inode->i_ino);
  5767. list_del_init(&BTRFS_I(inode)->i_orphan);
  5768. }
  5769. spin_unlock(&root->orphan_lock);
  5770. while (1) {
  5771. ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
  5772. if (!ordered)
  5773. break;
  5774. else {
  5775. printk(KERN_ERR "btrfs found ordered "
  5776. "extent %llu %llu on inode cleanup\n",
  5777. (unsigned long long)ordered->file_offset,
  5778. (unsigned long long)ordered->len);
  5779. btrfs_remove_ordered_extent(inode, ordered);
  5780. btrfs_put_ordered_extent(ordered);
  5781. btrfs_put_ordered_extent(ordered);
  5782. }
  5783. }
  5784. inode_tree_del(inode);
  5785. btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
  5786. free:
  5787. call_rcu(&inode->i_rcu, btrfs_i_callback);
  5788. }
  5789. int btrfs_drop_inode(struct inode *inode)
  5790. {
  5791. struct btrfs_root *root = BTRFS_I(inode)->root;
  5792. if (btrfs_root_refs(&root->root_item) == 0 &&
  5793. root != root->fs_info->tree_root)
  5794. return 1;
  5795. else
  5796. return generic_drop_inode(inode);
  5797. }
  5798. static void init_once(void *foo)
  5799. {
  5800. struct btrfs_inode *ei = (struct btrfs_inode *) foo;
  5801. inode_init_once(&ei->vfs_inode);
  5802. }
  5803. void btrfs_destroy_cachep(void)
  5804. {
  5805. if (btrfs_inode_cachep)
  5806. kmem_cache_destroy(btrfs_inode_cachep);
  5807. if (btrfs_trans_handle_cachep)
  5808. kmem_cache_destroy(btrfs_trans_handle_cachep);
  5809. if (btrfs_transaction_cachep)
  5810. kmem_cache_destroy(btrfs_transaction_cachep);
  5811. if (btrfs_path_cachep)
  5812. kmem_cache_destroy(btrfs_path_cachep);
  5813. }
  5814. int btrfs_init_cachep(void)
  5815. {
  5816. btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
  5817. sizeof(struct btrfs_inode), 0,
  5818. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
  5819. if (!btrfs_inode_cachep)
  5820. goto fail;
  5821. btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
  5822. sizeof(struct btrfs_trans_handle), 0,
  5823. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  5824. if (!btrfs_trans_handle_cachep)
  5825. goto fail;
  5826. btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
  5827. sizeof(struct btrfs_transaction), 0,
  5828. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  5829. if (!btrfs_transaction_cachep)
  5830. goto fail;
  5831. btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
  5832. sizeof(struct btrfs_path), 0,
  5833. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  5834. if (!btrfs_path_cachep)
  5835. goto fail;
  5836. return 0;
  5837. fail:
  5838. btrfs_destroy_cachep();
  5839. return -ENOMEM;
  5840. }
  5841. static int btrfs_getattr(struct vfsmount *mnt,
  5842. struct dentry *dentry, struct kstat *stat)
  5843. {
  5844. struct inode *inode = dentry->d_inode;
  5845. generic_fillattr(inode, stat);
  5846. stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
  5847. stat->blksize = PAGE_CACHE_SIZE;
  5848. stat->blocks = (inode_get_bytes(inode) +
  5849. BTRFS_I(inode)->delalloc_bytes) >> 9;
  5850. return 0;
  5851. }
  5852. static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  5853. struct inode *new_dir, struct dentry *new_dentry)
  5854. {
  5855. struct btrfs_trans_handle *trans;
  5856. struct btrfs_root *root = BTRFS_I(old_dir)->root;
  5857. struct btrfs_root *dest = BTRFS_I(new_dir)->root;
  5858. struct inode *new_inode = new_dentry->d_inode;
  5859. struct inode *old_inode = old_dentry->d_inode;
  5860. struct timespec ctime = CURRENT_TIME;
  5861. u64 index = 0;
  5862. u64 root_objectid;
  5863. int ret;
  5864. if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
  5865. return -EPERM;
  5866. /* we only allow rename subvolume link between subvolumes */
  5867. if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
  5868. return -EXDEV;
  5869. if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
  5870. (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID))
  5871. return -ENOTEMPTY;
  5872. if (S_ISDIR(old_inode->i_mode) && new_inode &&
  5873. new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
  5874. return -ENOTEMPTY;
  5875. /*
  5876. * we're using rename to replace one file with another.
  5877. * and the replacement file is large. Start IO on it now so
  5878. * we don't add too much work to the end of the transaction
  5879. */
  5880. if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
  5881. old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
  5882. filemap_flush(old_inode->i_mapping);
  5883. /* close the racy window with snapshot create/destroy ioctl */
  5884. if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
  5885. down_read(&root->fs_info->subvol_sem);
  5886. /*
  5887. * We want to reserve the absolute worst case amount of items. So if
  5888. * both inodes are subvols and we need to unlink them then that would
  5889. * require 4 item modifications, but if they are both normal inodes it
  5890. * would require 5 item modifications, so we'll assume their normal
  5891. * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
  5892. * should cover the worst case number of items we'll modify.
  5893. */
  5894. trans = btrfs_start_transaction(root, 20);
  5895. if (IS_ERR(trans))
  5896. return PTR_ERR(trans);
  5897. btrfs_set_trans_block_group(trans, new_dir);
  5898. if (dest != root)
  5899. btrfs_record_root_in_trans(trans, dest);
  5900. ret = btrfs_set_inode_index(new_dir, &index);
  5901. if (ret)
  5902. goto out_fail;
  5903. if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  5904. /* force full log commit if subvolume involved. */
  5905. root->fs_info->last_trans_log_full_commit = trans->transid;
  5906. } else {
  5907. ret = btrfs_insert_inode_ref(trans, dest,
  5908. new_dentry->d_name.name,
  5909. new_dentry->d_name.len,
  5910. old_inode->i_ino,
  5911. new_dir->i_ino, index);
  5912. if (ret)
  5913. goto out_fail;
  5914. /*
  5915. * this is an ugly little race, but the rename is required
  5916. * to make sure that if we crash, the inode is either at the
  5917. * old name or the new one. pinning the log transaction lets
  5918. * us make sure we don't allow a log commit to come in after
  5919. * we unlink the name but before we add the new name back in.
  5920. */
  5921. btrfs_pin_log_trans(root);
  5922. }
  5923. /*
  5924. * make sure the inode gets flushed if it is replacing
  5925. * something.
  5926. */
  5927. if (new_inode && new_inode->i_size &&
  5928. old_inode && S_ISREG(old_inode->i_mode)) {
  5929. btrfs_add_ordered_operation(trans, root, old_inode);
  5930. }
  5931. old_dir->i_ctime = old_dir->i_mtime = ctime;
  5932. new_dir->i_ctime = new_dir->i_mtime = ctime;
  5933. old_inode->i_ctime = ctime;
  5934. if (old_dentry->d_parent != new_dentry->d_parent)
  5935. btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
  5936. if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  5937. root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
  5938. ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
  5939. old_dentry->d_name.name,
  5940. old_dentry->d_name.len);
  5941. } else {
  5942. btrfs_inc_nlink(old_dentry->d_inode);
  5943. ret = btrfs_unlink_inode(trans, root, old_dir,
  5944. old_dentry->d_inode,
  5945. old_dentry->d_name.name,
  5946. old_dentry->d_name.len);
  5947. }
  5948. BUG_ON(ret);
  5949. if (new_inode) {
  5950. new_inode->i_ctime = CURRENT_TIME;
  5951. if (unlikely(new_inode->i_ino ==
  5952. BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
  5953. root_objectid = BTRFS_I(new_inode)->location.objectid;
  5954. ret = btrfs_unlink_subvol(trans, dest, new_dir,
  5955. root_objectid,
  5956. new_dentry->d_name.name,
  5957. new_dentry->d_name.len);
  5958. BUG_ON(new_inode->i_nlink == 0);
  5959. } else {
  5960. ret = btrfs_unlink_inode(trans, dest, new_dir,
  5961. new_dentry->d_inode,
  5962. new_dentry->d_name.name,
  5963. new_dentry->d_name.len);
  5964. }
  5965. BUG_ON(ret);
  5966. if (new_inode->i_nlink == 0) {
  5967. ret = btrfs_orphan_add(trans, new_dentry->d_inode);
  5968. BUG_ON(ret);
  5969. }
  5970. }
  5971. ret = btrfs_add_link(trans, new_dir, old_inode,
  5972. new_dentry->d_name.name,
  5973. new_dentry->d_name.len, 0, index);
  5974. BUG_ON(ret);
  5975. if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
  5976. struct dentry *parent = dget_parent(new_dentry);
  5977. btrfs_log_new_name(trans, old_inode, old_dir, parent);
  5978. dput(parent);
  5979. btrfs_end_log_trans(root);
  5980. }
  5981. out_fail:
  5982. btrfs_end_transaction_throttle(trans, root);
  5983. if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
  5984. up_read(&root->fs_info->subvol_sem);
  5985. return ret;
  5986. }
  5987. /*
  5988. * some fairly slow code that needs optimization. This walks the list
  5989. * of all the inodes with pending delalloc and forces them to disk.
  5990. */
  5991. int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
  5992. {
  5993. struct list_head *head = &root->fs_info->delalloc_inodes;
  5994. struct btrfs_inode *binode;
  5995. struct inode *inode;
  5996. if (root->fs_info->sb->s_flags & MS_RDONLY)
  5997. return -EROFS;
  5998. spin_lock(&root->fs_info->delalloc_lock);
  5999. while (!list_empty(head)) {
  6000. binode = list_entry(head->next, struct btrfs_inode,
  6001. delalloc_inodes);
  6002. inode = igrab(&binode->vfs_inode);
  6003. if (!inode)
  6004. list_del_init(&binode->delalloc_inodes);
  6005. spin_unlock(&root->fs_info->delalloc_lock);
  6006. if (inode) {
  6007. filemap_flush(inode->i_mapping);
  6008. if (delay_iput)
  6009. btrfs_add_delayed_iput(inode);
  6010. else
  6011. iput(inode);
  6012. }
  6013. cond_resched();
  6014. spin_lock(&root->fs_info->delalloc_lock);
  6015. }
  6016. spin_unlock(&root->fs_info->delalloc_lock);
  6017. /* the filemap_flush will queue IO into the worker threads, but
  6018. * we have to make sure the IO is actually started and that
  6019. * ordered extents get created before we return
  6020. */
  6021. atomic_inc(&root->fs_info->async_submit_draining);
  6022. while (atomic_read(&root->fs_info->nr_async_submits) ||
  6023. atomic_read(&root->fs_info->async_delalloc_pages)) {
  6024. wait_event(root->fs_info->async_submit_wait,
  6025. (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
  6026. atomic_read(&root->fs_info->async_delalloc_pages) == 0));
  6027. }
  6028. atomic_dec(&root->fs_info->async_submit_draining);
  6029. return 0;
  6030. }
  6031. int btrfs_start_one_delalloc_inode(struct btrfs_root *root, int delay_iput,
  6032. int sync)
  6033. {
  6034. struct btrfs_inode *binode;
  6035. struct inode *inode = NULL;
  6036. spin_lock(&root->fs_info->delalloc_lock);
  6037. while (!list_empty(&root->fs_info->delalloc_inodes)) {
  6038. binode = list_entry(root->fs_info->delalloc_inodes.next,
  6039. struct btrfs_inode, delalloc_inodes);
  6040. inode = igrab(&binode->vfs_inode);
  6041. if (inode) {
  6042. list_move_tail(&binode->delalloc_inodes,
  6043. &root->fs_info->delalloc_inodes);
  6044. break;
  6045. }
  6046. list_del_init(&binode->delalloc_inodes);
  6047. cond_resched_lock(&root->fs_info->delalloc_lock);
  6048. }
  6049. spin_unlock(&root->fs_info->delalloc_lock);
  6050. if (inode) {
  6051. if (sync) {
  6052. filemap_write_and_wait(inode->i_mapping);
  6053. /*
  6054. * We have to do this because compression doesn't
  6055. * actually set PG_writeback until it submits the pages
  6056. * for IO, which happens in an async thread, so we could
  6057. * race and not actually wait for any writeback pages
  6058. * because they've not been submitted yet. Technically
  6059. * this could still be the case for the ordered stuff
  6060. * since the async thread may not have started to do its
  6061. * work yet. If this becomes the case then we need to
  6062. * figure out a way to make sure that in writepage we
  6063. * wait for any async pages to be submitted before
  6064. * returning so that fdatawait does what its supposed to
  6065. * do.
  6066. */
  6067. btrfs_wait_ordered_range(inode, 0, (u64)-1);
  6068. } else {
  6069. filemap_flush(inode->i_mapping);
  6070. }
  6071. if (delay_iput)
  6072. btrfs_add_delayed_iput(inode);
  6073. else
  6074. iput(inode);
  6075. return 1;
  6076. }
  6077. return 0;
  6078. }
  6079. static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
  6080. const char *symname)
  6081. {
  6082. struct btrfs_trans_handle *trans;
  6083. struct btrfs_root *root = BTRFS_I(dir)->root;
  6084. struct btrfs_path *path;
  6085. struct btrfs_key key;
  6086. struct inode *inode = NULL;
  6087. int err;
  6088. int drop_inode = 0;
  6089. u64 objectid;
  6090. u64 index = 0 ;
  6091. int name_len;
  6092. int datasize;
  6093. unsigned long ptr;
  6094. struct btrfs_file_extent_item *ei;
  6095. struct extent_buffer *leaf;
  6096. unsigned long nr = 0;
  6097. name_len = strlen(symname) + 1;
  6098. if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
  6099. return -ENAMETOOLONG;
  6100. err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
  6101. if (err)
  6102. return err;
  6103. /*
  6104. * 2 items for inode item and ref
  6105. * 2 items for dir items
  6106. * 1 item for xattr if selinux is on
  6107. */
  6108. trans = btrfs_start_transaction(root, 5);
  6109. if (IS_ERR(trans))
  6110. return PTR_ERR(trans);
  6111. btrfs_set_trans_block_group(trans, dir);
  6112. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  6113. dentry->d_name.len, dir->i_ino, objectid,
  6114. BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
  6115. &index);
  6116. err = PTR_ERR(inode);
  6117. if (IS_ERR(inode))
  6118. goto out_unlock;
  6119. err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
  6120. if (err) {
  6121. drop_inode = 1;
  6122. goto out_unlock;
  6123. }
  6124. btrfs_set_trans_block_group(trans, inode);
  6125. err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
  6126. if (err)
  6127. drop_inode = 1;
  6128. else {
  6129. inode->i_mapping->a_ops = &btrfs_aops;
  6130. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  6131. inode->i_fop = &btrfs_file_operations;
  6132. inode->i_op = &btrfs_file_inode_operations;
  6133. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  6134. }
  6135. btrfs_update_inode_block_group(trans, inode);
  6136. btrfs_update_inode_block_group(trans, dir);
  6137. if (drop_inode)
  6138. goto out_unlock;
  6139. path = btrfs_alloc_path();
  6140. BUG_ON(!path);
  6141. key.objectid = inode->i_ino;
  6142. key.offset = 0;
  6143. btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
  6144. datasize = btrfs_file_extent_calc_inline_size(name_len);
  6145. err = btrfs_insert_empty_item(trans, root, path, &key,
  6146. datasize);
  6147. if (err) {
  6148. drop_inode = 1;
  6149. goto out_unlock;
  6150. }
  6151. leaf = path->nodes[0];
  6152. ei = btrfs_item_ptr(leaf, path->slots[0],
  6153. struct btrfs_file_extent_item);
  6154. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  6155. btrfs_set_file_extent_type(leaf, ei,
  6156. BTRFS_FILE_EXTENT_INLINE);
  6157. btrfs_set_file_extent_encryption(leaf, ei, 0);
  6158. btrfs_set_file_extent_compression(leaf, ei, 0);
  6159. btrfs_set_file_extent_other_encoding(leaf, ei, 0);
  6160. btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
  6161. ptr = btrfs_file_extent_inline_start(ei);
  6162. write_extent_buffer(leaf, symname, ptr, name_len);
  6163. btrfs_mark_buffer_dirty(leaf);
  6164. btrfs_free_path(path);
  6165. inode->i_op = &btrfs_symlink_inode_operations;
  6166. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  6167. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  6168. inode_set_bytes(inode, name_len);
  6169. btrfs_i_size_write(inode, name_len - 1);
  6170. err = btrfs_update_inode(trans, root, inode);
  6171. if (err)
  6172. drop_inode = 1;
  6173. out_unlock:
  6174. nr = trans->blocks_used;
  6175. btrfs_end_transaction_throttle(trans, root);
  6176. if (drop_inode) {
  6177. inode_dec_link_count(inode);
  6178. iput(inode);
  6179. }
  6180. btrfs_btree_balance_dirty(root, nr);
  6181. return err;
  6182. }
  6183. static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
  6184. u64 start, u64 num_bytes, u64 min_size,
  6185. loff_t actual_len, u64 *alloc_hint,
  6186. struct btrfs_trans_handle *trans)
  6187. {
  6188. struct btrfs_root *root = BTRFS_I(inode)->root;
  6189. struct btrfs_key ins;
  6190. u64 cur_offset = start;
  6191. u64 i_size;
  6192. int ret = 0;
  6193. bool own_trans = true;
  6194. if (trans)
  6195. own_trans = false;
  6196. while (num_bytes > 0) {
  6197. if (own_trans) {
  6198. trans = btrfs_start_transaction(root, 3);
  6199. if (IS_ERR(trans)) {
  6200. ret = PTR_ERR(trans);
  6201. break;
  6202. }
  6203. }
  6204. ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
  6205. 0, *alloc_hint, (u64)-1, &ins, 1);
  6206. if (ret) {
  6207. if (own_trans)
  6208. btrfs_end_transaction(trans, root);
  6209. break;
  6210. }
  6211. ret = insert_reserved_file_extent(trans, inode,
  6212. cur_offset, ins.objectid,
  6213. ins.offset, ins.offset,
  6214. ins.offset, 0, 0, 0,
  6215. BTRFS_FILE_EXTENT_PREALLOC);
  6216. BUG_ON(ret);
  6217. btrfs_drop_extent_cache(inode, cur_offset,
  6218. cur_offset + ins.offset -1, 0);
  6219. num_bytes -= ins.offset;
  6220. cur_offset += ins.offset;
  6221. *alloc_hint = ins.objectid + ins.offset;
  6222. inode->i_ctime = CURRENT_TIME;
  6223. BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
  6224. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  6225. (actual_len > inode->i_size) &&
  6226. (cur_offset > inode->i_size)) {
  6227. if (cur_offset > actual_len)
  6228. i_size = actual_len;
  6229. else
  6230. i_size = cur_offset;
  6231. i_size_write(inode, i_size);
  6232. btrfs_ordered_update_i_size(inode, i_size, NULL);
  6233. }
  6234. ret = btrfs_update_inode(trans, root, inode);
  6235. BUG_ON(ret);
  6236. if (own_trans)
  6237. btrfs_end_transaction(trans, root);
  6238. }
  6239. return ret;
  6240. }
  6241. int btrfs_prealloc_file_range(struct inode *inode, int mode,
  6242. u64 start, u64 num_bytes, u64 min_size,
  6243. loff_t actual_len, u64 *alloc_hint)
  6244. {
  6245. return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
  6246. min_size, actual_len, alloc_hint,
  6247. NULL);
  6248. }
  6249. int btrfs_prealloc_file_range_trans(struct inode *inode,
  6250. struct btrfs_trans_handle *trans, int mode,
  6251. u64 start, u64 num_bytes, u64 min_size,
  6252. loff_t actual_len, u64 *alloc_hint)
  6253. {
  6254. return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
  6255. min_size, actual_len, alloc_hint, trans);
  6256. }
  6257. static long btrfs_fallocate(struct inode *inode, int mode,
  6258. loff_t offset, loff_t len)
  6259. {
  6260. struct extent_state *cached_state = NULL;
  6261. u64 cur_offset;
  6262. u64 last_byte;
  6263. u64 alloc_start;
  6264. u64 alloc_end;
  6265. u64 alloc_hint = 0;
  6266. u64 locked_end;
  6267. u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
  6268. struct extent_map *em;
  6269. int ret;
  6270. alloc_start = offset & ~mask;
  6271. alloc_end = (offset + len + mask) & ~mask;
  6272. /*
  6273. * wait for ordered IO before we have any locks. We'll loop again
  6274. * below with the locks held.
  6275. */
  6276. btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
  6277. mutex_lock(&inode->i_mutex);
  6278. ret = inode_newsize_ok(inode, alloc_end);
  6279. if (ret)
  6280. goto out;
  6281. if (alloc_start > inode->i_size) {
  6282. ret = btrfs_cont_expand(inode, alloc_start);
  6283. if (ret)
  6284. goto out;
  6285. }
  6286. ret = btrfs_check_data_free_space(inode, alloc_end - alloc_start);
  6287. if (ret)
  6288. goto out;
  6289. locked_end = alloc_end - 1;
  6290. while (1) {
  6291. struct btrfs_ordered_extent *ordered;
  6292. /* the extent lock is ordered inside the running
  6293. * transaction
  6294. */
  6295. lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
  6296. locked_end, 0, &cached_state, GFP_NOFS);
  6297. ordered = btrfs_lookup_first_ordered_extent(inode,
  6298. alloc_end - 1);
  6299. if (ordered &&
  6300. ordered->file_offset + ordered->len > alloc_start &&
  6301. ordered->file_offset < alloc_end) {
  6302. btrfs_put_ordered_extent(ordered);
  6303. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  6304. alloc_start, locked_end,
  6305. &cached_state, GFP_NOFS);
  6306. /*
  6307. * we can't wait on the range with the transaction
  6308. * running or with the extent lock held
  6309. */
  6310. btrfs_wait_ordered_range(inode, alloc_start,
  6311. alloc_end - alloc_start);
  6312. } else {
  6313. if (ordered)
  6314. btrfs_put_ordered_extent(ordered);
  6315. break;
  6316. }
  6317. }
  6318. cur_offset = alloc_start;
  6319. while (1) {
  6320. em = btrfs_get_extent(inode, NULL, 0, cur_offset,
  6321. alloc_end - cur_offset, 0);
  6322. BUG_ON(IS_ERR(em) || !em);
  6323. last_byte = min(extent_map_end(em), alloc_end);
  6324. last_byte = (last_byte + mask) & ~mask;
  6325. if (em->block_start == EXTENT_MAP_HOLE ||
  6326. (cur_offset >= inode->i_size &&
  6327. !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
  6328. ret = btrfs_prealloc_file_range(inode, mode, cur_offset,
  6329. last_byte - cur_offset,
  6330. 1 << inode->i_blkbits,
  6331. offset + len,
  6332. &alloc_hint);
  6333. if (ret < 0) {
  6334. free_extent_map(em);
  6335. break;
  6336. }
  6337. }
  6338. free_extent_map(em);
  6339. cur_offset = last_byte;
  6340. if (cur_offset >= alloc_end) {
  6341. ret = 0;
  6342. break;
  6343. }
  6344. }
  6345. unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
  6346. &cached_state, GFP_NOFS);
  6347. btrfs_free_reserved_data_space(inode, alloc_end - alloc_start);
  6348. out:
  6349. mutex_unlock(&inode->i_mutex);
  6350. return ret;
  6351. }
  6352. static int btrfs_set_page_dirty(struct page *page)
  6353. {
  6354. return __set_page_dirty_nobuffers(page);
  6355. }
  6356. static int btrfs_permission(struct inode *inode, int mask, unsigned int flags)
  6357. {
  6358. if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
  6359. return -EACCES;
  6360. return generic_permission(inode, mask, flags, btrfs_check_acl);
  6361. }
  6362. static const struct inode_operations btrfs_dir_inode_operations = {
  6363. .getattr = btrfs_getattr,
  6364. .lookup = btrfs_lookup,
  6365. .create = btrfs_create,
  6366. .unlink = btrfs_unlink,
  6367. .link = btrfs_link,
  6368. .mkdir = btrfs_mkdir,
  6369. .rmdir = btrfs_rmdir,
  6370. .rename = btrfs_rename,
  6371. .symlink = btrfs_symlink,
  6372. .setattr = btrfs_setattr,
  6373. .mknod = btrfs_mknod,
  6374. .setxattr = btrfs_setxattr,
  6375. .getxattr = btrfs_getxattr,
  6376. .listxattr = btrfs_listxattr,
  6377. .removexattr = btrfs_removexattr,
  6378. .permission = btrfs_permission,
  6379. };
  6380. static const struct inode_operations btrfs_dir_ro_inode_operations = {
  6381. .lookup = btrfs_lookup,
  6382. .permission = btrfs_permission,
  6383. };
  6384. static const struct file_operations btrfs_dir_file_operations = {
  6385. .llseek = generic_file_llseek,
  6386. .read = generic_read_dir,
  6387. .readdir = btrfs_real_readdir,
  6388. .unlocked_ioctl = btrfs_ioctl,
  6389. #ifdef CONFIG_COMPAT
  6390. .compat_ioctl = btrfs_ioctl,
  6391. #endif
  6392. .release = btrfs_release_file,
  6393. .fsync = btrfs_sync_file,
  6394. };
  6395. static struct extent_io_ops btrfs_extent_io_ops = {
  6396. .fill_delalloc = run_delalloc_range,
  6397. .submit_bio_hook = btrfs_submit_bio_hook,
  6398. .merge_bio_hook = btrfs_merge_bio_hook,
  6399. .readpage_end_io_hook = btrfs_readpage_end_io_hook,
  6400. .writepage_end_io_hook = btrfs_writepage_end_io_hook,
  6401. .writepage_start_hook = btrfs_writepage_start_hook,
  6402. .readpage_io_failed_hook = btrfs_io_failed_hook,
  6403. .set_bit_hook = btrfs_set_bit_hook,
  6404. .clear_bit_hook = btrfs_clear_bit_hook,
  6405. .merge_extent_hook = btrfs_merge_extent_hook,
  6406. .split_extent_hook = btrfs_split_extent_hook,
  6407. };
  6408. /*
  6409. * btrfs doesn't support the bmap operation because swapfiles
  6410. * use bmap to make a mapping of extents in the file. They assume
  6411. * these extents won't change over the life of the file and they
  6412. * use the bmap result to do IO directly to the drive.
  6413. *
  6414. * the btrfs bmap call would return logical addresses that aren't
  6415. * suitable for IO and they also will change frequently as COW
  6416. * operations happen. So, swapfile + btrfs == corruption.
  6417. *
  6418. * For now we're avoiding this by dropping bmap.
  6419. */
  6420. static const struct address_space_operations btrfs_aops = {
  6421. .readpage = btrfs_readpage,
  6422. .writepage = btrfs_writepage,
  6423. .writepages = btrfs_writepages,
  6424. .readpages = btrfs_readpages,
  6425. .sync_page = block_sync_page,
  6426. .direct_IO = btrfs_direct_IO,
  6427. .invalidatepage = btrfs_invalidatepage,
  6428. .releasepage = btrfs_releasepage,
  6429. .set_page_dirty = btrfs_set_page_dirty,
  6430. .error_remove_page = generic_error_remove_page,
  6431. };
  6432. static const struct address_space_operations btrfs_symlink_aops = {
  6433. .readpage = btrfs_readpage,
  6434. .writepage = btrfs_writepage,
  6435. .invalidatepage = btrfs_invalidatepage,
  6436. .releasepage = btrfs_releasepage,
  6437. };
  6438. static const struct inode_operations btrfs_file_inode_operations = {
  6439. .truncate = btrfs_truncate,
  6440. .getattr = btrfs_getattr,
  6441. .setattr = btrfs_setattr,
  6442. .setxattr = btrfs_setxattr,
  6443. .getxattr = btrfs_getxattr,
  6444. .listxattr = btrfs_listxattr,
  6445. .removexattr = btrfs_removexattr,
  6446. .permission = btrfs_permission,
  6447. .fallocate = btrfs_fallocate,
  6448. .fiemap = btrfs_fiemap,
  6449. };
  6450. static const struct inode_operations btrfs_special_inode_operations = {
  6451. .getattr = btrfs_getattr,
  6452. .setattr = btrfs_setattr,
  6453. .permission = btrfs_permission,
  6454. .setxattr = btrfs_setxattr,
  6455. .getxattr = btrfs_getxattr,
  6456. .listxattr = btrfs_listxattr,
  6457. .removexattr = btrfs_removexattr,
  6458. };
  6459. static const struct inode_operations btrfs_symlink_inode_operations = {
  6460. .readlink = generic_readlink,
  6461. .follow_link = page_follow_link_light,
  6462. .put_link = page_put_link,
  6463. .getattr = btrfs_getattr,
  6464. .permission = btrfs_permission,
  6465. .setxattr = btrfs_setxattr,
  6466. .getxattr = btrfs_getxattr,
  6467. .listxattr = btrfs_listxattr,
  6468. .removexattr = btrfs_removexattr,
  6469. };
  6470. const struct dentry_operations btrfs_dentry_operations = {
  6471. .d_delete = btrfs_dentry_delete,
  6472. };