inode.c 194 KB

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