inode.c 187 KB

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