inode.c 196 KB

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