inode.c 187 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041
  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 & (1 << BIO_RW))) {
  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 & (1 << BIO_RW))
  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. dir->i_sb->s_dirt = 1;
  2593. btrfs_free_path(path);
  2594. return 0;
  2595. }
  2596. static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
  2597. {
  2598. struct inode *inode = dentry->d_inode;
  2599. int err = 0;
  2600. struct btrfs_root *root = BTRFS_I(dir)->root;
  2601. struct btrfs_trans_handle *trans;
  2602. unsigned long nr = 0;
  2603. if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
  2604. inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
  2605. return -ENOTEMPTY;
  2606. trans = __unlink_start_trans(dir, dentry);
  2607. if (IS_ERR(trans))
  2608. return PTR_ERR(trans);
  2609. btrfs_set_trans_block_group(trans, dir);
  2610. if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
  2611. err = btrfs_unlink_subvol(trans, root, dir,
  2612. BTRFS_I(inode)->location.objectid,
  2613. dentry->d_name.name,
  2614. dentry->d_name.len);
  2615. goto out;
  2616. }
  2617. err = btrfs_orphan_add(trans, inode);
  2618. if (err)
  2619. goto out;
  2620. /* now the directory is empty */
  2621. err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
  2622. dentry->d_name.name, dentry->d_name.len);
  2623. if (!err)
  2624. btrfs_i_size_write(inode, 0);
  2625. out:
  2626. nr = trans->blocks_used;
  2627. __unlink_end_trans(trans, root);
  2628. btrfs_btree_balance_dirty(root, nr);
  2629. return err;
  2630. }
  2631. #if 0
  2632. /*
  2633. * when truncating bytes in a file, it is possible to avoid reading
  2634. * the leaves that contain only checksum items. This can be the
  2635. * majority of the IO required to delete a large file, but it must
  2636. * be done carefully.
  2637. *
  2638. * The keys in the level just above the leaves are checked to make sure
  2639. * the lowest key in a given leaf is a csum key, and starts at an offset
  2640. * after the new size.
  2641. *
  2642. * Then the key for the next leaf is checked to make sure it also has
  2643. * a checksum item for the same file. If it does, we know our target leaf
  2644. * contains only checksum items, and it can be safely freed without reading
  2645. * it.
  2646. *
  2647. * This is just an optimization targeted at large files. It may do
  2648. * nothing. It will return 0 unless things went badly.
  2649. */
  2650. static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
  2651. struct btrfs_root *root,
  2652. struct btrfs_path *path,
  2653. struct inode *inode, u64 new_size)
  2654. {
  2655. struct btrfs_key key;
  2656. int ret;
  2657. int nritems;
  2658. struct btrfs_key found_key;
  2659. struct btrfs_key other_key;
  2660. struct btrfs_leaf_ref *ref;
  2661. u64 leaf_gen;
  2662. u64 leaf_start;
  2663. path->lowest_level = 1;
  2664. key.objectid = inode->i_ino;
  2665. key.type = BTRFS_CSUM_ITEM_KEY;
  2666. key.offset = new_size;
  2667. again:
  2668. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  2669. if (ret < 0)
  2670. goto out;
  2671. if (path->nodes[1] == NULL) {
  2672. ret = 0;
  2673. goto out;
  2674. }
  2675. ret = 0;
  2676. btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
  2677. nritems = btrfs_header_nritems(path->nodes[1]);
  2678. if (!nritems)
  2679. goto out;
  2680. if (path->slots[1] >= nritems)
  2681. goto next_node;
  2682. /* did we find a key greater than anything we want to delete? */
  2683. if (found_key.objectid > inode->i_ino ||
  2684. (found_key.objectid == inode->i_ino && found_key.type > key.type))
  2685. goto out;
  2686. /* we check the next key in the node to make sure the leave contains
  2687. * only checksum items. This comparison doesn't work if our
  2688. * leaf is the last one in the node
  2689. */
  2690. if (path->slots[1] + 1 >= nritems) {
  2691. next_node:
  2692. /* search forward from the last key in the node, this
  2693. * will bring us into the next node in the tree
  2694. */
  2695. btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
  2696. /* unlikely, but we inc below, so check to be safe */
  2697. if (found_key.offset == (u64)-1)
  2698. goto out;
  2699. /* search_forward needs a path with locks held, do the
  2700. * search again for the original key. It is possible
  2701. * this will race with a balance and return a path that
  2702. * we could modify, but this drop is just an optimization
  2703. * and is allowed to miss some leaves.
  2704. */
  2705. btrfs_release_path(root, path);
  2706. found_key.offset++;
  2707. /* setup a max key for search_forward */
  2708. other_key.offset = (u64)-1;
  2709. other_key.type = key.type;
  2710. other_key.objectid = key.objectid;
  2711. path->keep_locks = 1;
  2712. ret = btrfs_search_forward(root, &found_key, &other_key,
  2713. path, 0, 0);
  2714. path->keep_locks = 0;
  2715. if (ret || found_key.objectid != key.objectid ||
  2716. found_key.type != key.type) {
  2717. ret = 0;
  2718. goto out;
  2719. }
  2720. key.offset = found_key.offset;
  2721. btrfs_release_path(root, path);
  2722. cond_resched();
  2723. goto again;
  2724. }
  2725. /* we know there's one more slot after us in the tree,
  2726. * read that key so we can verify it is also a checksum item
  2727. */
  2728. btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
  2729. if (found_key.objectid < inode->i_ino)
  2730. goto next_key;
  2731. if (found_key.type != key.type || found_key.offset < new_size)
  2732. goto next_key;
  2733. /*
  2734. * if the key for the next leaf isn't a csum key from this objectid,
  2735. * we can't be sure there aren't good items inside this leaf.
  2736. * Bail out
  2737. */
  2738. if (other_key.objectid != inode->i_ino || other_key.type != key.type)
  2739. goto out;
  2740. leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
  2741. leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
  2742. /*
  2743. * it is safe to delete this leaf, it contains only
  2744. * csum items from this inode at an offset >= new_size
  2745. */
  2746. ret = btrfs_del_leaf(trans, root, path, leaf_start);
  2747. BUG_ON(ret);
  2748. if (root->ref_cows && leaf_gen < trans->transid) {
  2749. ref = btrfs_alloc_leaf_ref(root, 0);
  2750. if (ref) {
  2751. ref->root_gen = root->root_key.offset;
  2752. ref->bytenr = leaf_start;
  2753. ref->owner = 0;
  2754. ref->generation = leaf_gen;
  2755. ref->nritems = 0;
  2756. btrfs_sort_leaf_ref(ref);
  2757. ret = btrfs_add_leaf_ref(root, ref, 0);
  2758. WARN_ON(ret);
  2759. btrfs_free_leaf_ref(root, ref);
  2760. } else {
  2761. WARN_ON(1);
  2762. }
  2763. }
  2764. next_key:
  2765. btrfs_release_path(root, path);
  2766. if (other_key.objectid == inode->i_ino &&
  2767. other_key.type == key.type && other_key.offset > key.offset) {
  2768. key.offset = other_key.offset;
  2769. cond_resched();
  2770. goto again;
  2771. }
  2772. ret = 0;
  2773. out:
  2774. /* fixup any changes we've made to the path */
  2775. path->lowest_level = 0;
  2776. path->keep_locks = 0;
  2777. btrfs_release_path(root, path);
  2778. return ret;
  2779. }
  2780. #endif
  2781. /*
  2782. * this can truncate away extent items, csum items and directory items.
  2783. * It starts at a high offset and removes keys until it can't find
  2784. * any higher than new_size
  2785. *
  2786. * csum items that cross the new i_size are truncated to the new size
  2787. * as well.
  2788. *
  2789. * min_type is the minimum key type to truncate down to. If set to 0, this
  2790. * will kill all the items on this inode, including the INODE_ITEM_KEY.
  2791. */
  2792. int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
  2793. struct btrfs_root *root,
  2794. struct inode *inode,
  2795. u64 new_size, u32 min_type)
  2796. {
  2797. struct btrfs_path *path;
  2798. struct extent_buffer *leaf;
  2799. struct btrfs_file_extent_item *fi;
  2800. struct btrfs_key key;
  2801. struct btrfs_key found_key;
  2802. u64 extent_start = 0;
  2803. u64 extent_num_bytes = 0;
  2804. u64 extent_offset = 0;
  2805. u64 item_end = 0;
  2806. u64 mask = root->sectorsize - 1;
  2807. u32 found_type = (u8)-1;
  2808. int found_extent;
  2809. int del_item;
  2810. int pending_del_nr = 0;
  2811. int pending_del_slot = 0;
  2812. int extent_type = -1;
  2813. int encoding;
  2814. int ret;
  2815. int err = 0;
  2816. BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
  2817. if (root->ref_cows)
  2818. btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
  2819. path = btrfs_alloc_path();
  2820. BUG_ON(!path);
  2821. path->reada = -1;
  2822. key.objectid = inode->i_ino;
  2823. key.offset = (u64)-1;
  2824. key.type = (u8)-1;
  2825. search_again:
  2826. path->leave_spinning = 1;
  2827. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  2828. if (ret < 0) {
  2829. err = ret;
  2830. goto out;
  2831. }
  2832. if (ret > 0) {
  2833. /* there are no items in the tree for us to truncate, we're
  2834. * done
  2835. */
  2836. if (path->slots[0] == 0)
  2837. goto out;
  2838. path->slots[0]--;
  2839. }
  2840. while (1) {
  2841. fi = NULL;
  2842. leaf = path->nodes[0];
  2843. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2844. found_type = btrfs_key_type(&found_key);
  2845. encoding = 0;
  2846. if (found_key.objectid != inode->i_ino)
  2847. break;
  2848. if (found_type < min_type)
  2849. break;
  2850. item_end = found_key.offset;
  2851. if (found_type == BTRFS_EXTENT_DATA_KEY) {
  2852. fi = btrfs_item_ptr(leaf, path->slots[0],
  2853. struct btrfs_file_extent_item);
  2854. extent_type = btrfs_file_extent_type(leaf, fi);
  2855. encoding = btrfs_file_extent_compression(leaf, fi);
  2856. encoding |= btrfs_file_extent_encryption(leaf, fi);
  2857. encoding |= btrfs_file_extent_other_encoding(leaf, fi);
  2858. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  2859. item_end +=
  2860. btrfs_file_extent_num_bytes(leaf, fi);
  2861. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  2862. item_end += btrfs_file_extent_inline_len(leaf,
  2863. fi);
  2864. }
  2865. item_end--;
  2866. }
  2867. if (found_type > min_type) {
  2868. del_item = 1;
  2869. } else {
  2870. if (item_end < new_size)
  2871. break;
  2872. if (found_key.offset >= new_size)
  2873. del_item = 1;
  2874. else
  2875. del_item = 0;
  2876. }
  2877. found_extent = 0;
  2878. /* FIXME, shrink the extent if the ref count is only 1 */
  2879. if (found_type != BTRFS_EXTENT_DATA_KEY)
  2880. goto delete;
  2881. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  2882. u64 num_dec;
  2883. extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
  2884. if (!del_item && !encoding) {
  2885. u64 orig_num_bytes =
  2886. btrfs_file_extent_num_bytes(leaf, fi);
  2887. extent_num_bytes = new_size -
  2888. found_key.offset + root->sectorsize - 1;
  2889. extent_num_bytes = extent_num_bytes &
  2890. ~((u64)root->sectorsize - 1);
  2891. btrfs_set_file_extent_num_bytes(leaf, fi,
  2892. extent_num_bytes);
  2893. num_dec = (orig_num_bytes -
  2894. extent_num_bytes);
  2895. if (root->ref_cows && extent_start != 0)
  2896. inode_sub_bytes(inode, num_dec);
  2897. btrfs_mark_buffer_dirty(leaf);
  2898. } else {
  2899. extent_num_bytes =
  2900. btrfs_file_extent_disk_num_bytes(leaf,
  2901. fi);
  2902. extent_offset = found_key.offset -
  2903. btrfs_file_extent_offset(leaf, fi);
  2904. /* FIXME blocksize != 4096 */
  2905. num_dec = btrfs_file_extent_num_bytes(leaf, fi);
  2906. if (extent_start != 0) {
  2907. found_extent = 1;
  2908. if (root->ref_cows)
  2909. inode_sub_bytes(inode, num_dec);
  2910. }
  2911. }
  2912. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  2913. /*
  2914. * we can't truncate inline items that have had
  2915. * special encodings
  2916. */
  2917. if (!del_item &&
  2918. btrfs_file_extent_compression(leaf, fi) == 0 &&
  2919. btrfs_file_extent_encryption(leaf, fi) == 0 &&
  2920. btrfs_file_extent_other_encoding(leaf, fi) == 0) {
  2921. u32 size = new_size - found_key.offset;
  2922. if (root->ref_cows) {
  2923. inode_sub_bytes(inode, item_end + 1 -
  2924. new_size);
  2925. }
  2926. size =
  2927. btrfs_file_extent_calc_inline_size(size);
  2928. ret = btrfs_truncate_item(trans, root, path,
  2929. size, 1);
  2930. BUG_ON(ret);
  2931. } else if (root->ref_cows) {
  2932. inode_sub_bytes(inode, item_end + 1 -
  2933. found_key.offset);
  2934. }
  2935. }
  2936. delete:
  2937. if (del_item) {
  2938. if (!pending_del_nr) {
  2939. /* no pending yet, add ourselves */
  2940. pending_del_slot = path->slots[0];
  2941. pending_del_nr = 1;
  2942. } else if (pending_del_nr &&
  2943. path->slots[0] + 1 == pending_del_slot) {
  2944. /* hop on the pending chunk */
  2945. pending_del_nr++;
  2946. pending_del_slot = path->slots[0];
  2947. } else {
  2948. BUG();
  2949. }
  2950. } else {
  2951. break;
  2952. }
  2953. if (found_extent && root->ref_cows) {
  2954. btrfs_set_path_blocking(path);
  2955. ret = btrfs_free_extent(trans, root, extent_start,
  2956. extent_num_bytes, 0,
  2957. btrfs_header_owner(leaf),
  2958. inode->i_ino, extent_offset);
  2959. BUG_ON(ret);
  2960. }
  2961. if (found_type == BTRFS_INODE_ITEM_KEY)
  2962. break;
  2963. if (path->slots[0] == 0 ||
  2964. path->slots[0] != pending_del_slot) {
  2965. if (root->ref_cows) {
  2966. err = -EAGAIN;
  2967. goto out;
  2968. }
  2969. if (pending_del_nr) {
  2970. ret = btrfs_del_items(trans, root, path,
  2971. pending_del_slot,
  2972. pending_del_nr);
  2973. BUG_ON(ret);
  2974. pending_del_nr = 0;
  2975. }
  2976. btrfs_release_path(root, path);
  2977. goto search_again;
  2978. } else {
  2979. path->slots[0]--;
  2980. }
  2981. }
  2982. out:
  2983. if (pending_del_nr) {
  2984. ret = btrfs_del_items(trans, root, path, pending_del_slot,
  2985. pending_del_nr);
  2986. BUG_ON(ret);
  2987. }
  2988. btrfs_free_path(path);
  2989. return err;
  2990. }
  2991. /*
  2992. * taken from block_truncate_page, but does cow as it zeros out
  2993. * any bytes left in the last page in the file.
  2994. */
  2995. static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
  2996. {
  2997. struct inode *inode = mapping->host;
  2998. struct btrfs_root *root = BTRFS_I(inode)->root;
  2999. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  3000. struct btrfs_ordered_extent *ordered;
  3001. struct extent_state *cached_state = NULL;
  3002. char *kaddr;
  3003. u32 blocksize = root->sectorsize;
  3004. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  3005. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  3006. struct page *page;
  3007. int ret = 0;
  3008. u64 page_start;
  3009. u64 page_end;
  3010. if ((offset & (blocksize - 1)) == 0)
  3011. goto out;
  3012. ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
  3013. if (ret)
  3014. goto out;
  3015. ret = -ENOMEM;
  3016. again:
  3017. page = grab_cache_page(mapping, index);
  3018. if (!page) {
  3019. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  3020. goto out;
  3021. }
  3022. page_start = page_offset(page);
  3023. page_end = page_start + PAGE_CACHE_SIZE - 1;
  3024. if (!PageUptodate(page)) {
  3025. ret = btrfs_readpage(NULL, page);
  3026. lock_page(page);
  3027. if (page->mapping != mapping) {
  3028. unlock_page(page);
  3029. page_cache_release(page);
  3030. goto again;
  3031. }
  3032. if (!PageUptodate(page)) {
  3033. ret = -EIO;
  3034. goto out_unlock;
  3035. }
  3036. }
  3037. wait_on_page_writeback(page);
  3038. lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
  3039. GFP_NOFS);
  3040. set_page_extent_mapped(page);
  3041. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  3042. if (ordered) {
  3043. unlock_extent_cached(io_tree, page_start, page_end,
  3044. &cached_state, GFP_NOFS);
  3045. unlock_page(page);
  3046. page_cache_release(page);
  3047. btrfs_start_ordered_extent(inode, ordered, 1);
  3048. btrfs_put_ordered_extent(ordered);
  3049. goto again;
  3050. }
  3051. clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
  3052. EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
  3053. 0, 0, &cached_state, GFP_NOFS);
  3054. ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
  3055. &cached_state);
  3056. if (ret) {
  3057. unlock_extent_cached(io_tree, page_start, page_end,
  3058. &cached_state, GFP_NOFS);
  3059. goto out_unlock;
  3060. }
  3061. ret = 0;
  3062. if (offset != PAGE_CACHE_SIZE) {
  3063. kaddr = kmap(page);
  3064. memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
  3065. flush_dcache_page(page);
  3066. kunmap(page);
  3067. }
  3068. ClearPageChecked(page);
  3069. set_page_dirty(page);
  3070. unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
  3071. GFP_NOFS);
  3072. out_unlock:
  3073. if (ret)
  3074. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  3075. unlock_page(page);
  3076. page_cache_release(page);
  3077. out:
  3078. return ret;
  3079. }
  3080. int btrfs_cont_expand(struct inode *inode, loff_t size)
  3081. {
  3082. struct btrfs_trans_handle *trans;
  3083. struct btrfs_root *root = BTRFS_I(inode)->root;
  3084. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  3085. struct extent_map *em = NULL;
  3086. struct extent_state *cached_state = NULL;
  3087. u64 mask = root->sectorsize - 1;
  3088. u64 hole_start = (inode->i_size + mask) & ~mask;
  3089. u64 block_end = (size + mask) & ~mask;
  3090. u64 last_byte;
  3091. u64 cur_offset;
  3092. u64 hole_size;
  3093. int err = 0;
  3094. if (size <= hole_start)
  3095. return 0;
  3096. while (1) {
  3097. struct btrfs_ordered_extent *ordered;
  3098. btrfs_wait_ordered_range(inode, hole_start,
  3099. block_end - hole_start);
  3100. lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
  3101. &cached_state, GFP_NOFS);
  3102. ordered = btrfs_lookup_ordered_extent(inode, hole_start);
  3103. if (!ordered)
  3104. break;
  3105. unlock_extent_cached(io_tree, hole_start, block_end - 1,
  3106. &cached_state, GFP_NOFS);
  3107. btrfs_put_ordered_extent(ordered);
  3108. }
  3109. cur_offset = hole_start;
  3110. while (1) {
  3111. em = btrfs_get_extent(inode, NULL, 0, cur_offset,
  3112. block_end - cur_offset, 0);
  3113. BUG_ON(IS_ERR(em) || !em);
  3114. last_byte = min(extent_map_end(em), block_end);
  3115. last_byte = (last_byte + mask) & ~mask;
  3116. if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
  3117. u64 hint_byte = 0;
  3118. hole_size = last_byte - cur_offset;
  3119. trans = btrfs_start_transaction(root, 2);
  3120. if (IS_ERR(trans)) {
  3121. err = PTR_ERR(trans);
  3122. break;
  3123. }
  3124. btrfs_set_trans_block_group(trans, inode);
  3125. err = btrfs_drop_extents(trans, inode, cur_offset,
  3126. cur_offset + hole_size,
  3127. &hint_byte, 1);
  3128. BUG_ON(err);
  3129. err = btrfs_insert_file_extent(trans, root,
  3130. inode->i_ino, cur_offset, 0,
  3131. 0, hole_size, 0, hole_size,
  3132. 0, 0, 0);
  3133. BUG_ON(err);
  3134. btrfs_drop_extent_cache(inode, hole_start,
  3135. last_byte - 1, 0);
  3136. btrfs_end_transaction(trans, root);
  3137. }
  3138. free_extent_map(em);
  3139. em = NULL;
  3140. cur_offset = last_byte;
  3141. if (cur_offset >= block_end)
  3142. break;
  3143. }
  3144. free_extent_map(em);
  3145. unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
  3146. GFP_NOFS);
  3147. return err;
  3148. }
  3149. static int btrfs_setattr_size(struct inode *inode, struct iattr *attr)
  3150. {
  3151. struct btrfs_root *root = BTRFS_I(inode)->root;
  3152. struct btrfs_trans_handle *trans;
  3153. unsigned long nr;
  3154. int ret;
  3155. if (attr->ia_size == inode->i_size)
  3156. return 0;
  3157. if (attr->ia_size > inode->i_size) {
  3158. unsigned long limit;
  3159. limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
  3160. if (attr->ia_size > inode->i_sb->s_maxbytes)
  3161. return -EFBIG;
  3162. if (limit != RLIM_INFINITY && attr->ia_size > limit) {
  3163. send_sig(SIGXFSZ, current, 0);
  3164. return -EFBIG;
  3165. }
  3166. }
  3167. trans = btrfs_start_transaction(root, 5);
  3168. if (IS_ERR(trans))
  3169. return PTR_ERR(trans);
  3170. btrfs_set_trans_block_group(trans, inode);
  3171. ret = btrfs_orphan_add(trans, inode);
  3172. BUG_ON(ret);
  3173. nr = trans->blocks_used;
  3174. btrfs_end_transaction(trans, root);
  3175. btrfs_btree_balance_dirty(root, nr);
  3176. if (attr->ia_size > inode->i_size) {
  3177. ret = btrfs_cont_expand(inode, attr->ia_size);
  3178. if (ret) {
  3179. btrfs_truncate(inode);
  3180. return ret;
  3181. }
  3182. i_size_write(inode, attr->ia_size);
  3183. btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
  3184. trans = btrfs_start_transaction(root, 0);
  3185. BUG_ON(IS_ERR(trans));
  3186. btrfs_set_trans_block_group(trans, inode);
  3187. trans->block_rsv = root->orphan_block_rsv;
  3188. BUG_ON(!trans->block_rsv);
  3189. ret = btrfs_update_inode(trans, root, inode);
  3190. BUG_ON(ret);
  3191. if (inode->i_nlink > 0) {
  3192. ret = btrfs_orphan_del(trans, inode);
  3193. BUG_ON(ret);
  3194. }
  3195. nr = trans->blocks_used;
  3196. btrfs_end_transaction(trans, root);
  3197. btrfs_btree_balance_dirty(root, nr);
  3198. return 0;
  3199. }
  3200. /*
  3201. * We're truncating a file that used to have good data down to
  3202. * zero. Make sure it gets into the ordered flush list so that
  3203. * any new writes get down to disk quickly.
  3204. */
  3205. if (attr->ia_size == 0)
  3206. BTRFS_I(inode)->ordered_data_close = 1;
  3207. /* we don't support swapfiles, so vmtruncate shouldn't fail */
  3208. ret = vmtruncate(inode, attr->ia_size);
  3209. BUG_ON(ret);
  3210. return 0;
  3211. }
  3212. static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
  3213. {
  3214. struct inode *inode = dentry->d_inode;
  3215. int err;
  3216. err = inode_change_ok(inode, attr);
  3217. if (err)
  3218. return err;
  3219. if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
  3220. err = btrfs_setattr_size(inode, attr);
  3221. if (err)
  3222. return err;
  3223. }
  3224. attr->ia_valid &= ~ATTR_SIZE;
  3225. if (attr->ia_valid)
  3226. err = inode_setattr(inode, attr);
  3227. if (!err && ((attr->ia_valid & ATTR_MODE)))
  3228. err = btrfs_acl_chmod(inode);
  3229. return err;
  3230. }
  3231. void btrfs_delete_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 (is_bad_inode(inode)) {
  3239. btrfs_orphan_del(NULL, inode);
  3240. goto no_delete;
  3241. }
  3242. btrfs_wait_ordered_range(inode, 0, (u64)-1);
  3243. if (root->fs_info->log_root_recovering) {
  3244. BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan));
  3245. goto no_delete;
  3246. }
  3247. if (inode->i_nlink > 0) {
  3248. BUG_ON(btrfs_root_refs(&root->root_item) != 0);
  3249. goto no_delete;
  3250. }
  3251. btrfs_i_size_write(inode, 0);
  3252. while (1) {
  3253. trans = btrfs_start_transaction(root, 0);
  3254. BUG_ON(IS_ERR(trans));
  3255. btrfs_set_trans_block_group(trans, inode);
  3256. trans->block_rsv = root->orphan_block_rsv;
  3257. ret = btrfs_block_rsv_check(trans, root,
  3258. root->orphan_block_rsv, 0, 5);
  3259. if (ret) {
  3260. BUG_ON(ret != -EAGAIN);
  3261. ret = btrfs_commit_transaction(trans, root);
  3262. BUG_ON(ret);
  3263. continue;
  3264. }
  3265. ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
  3266. if (ret != -EAGAIN)
  3267. break;
  3268. nr = trans->blocks_used;
  3269. btrfs_end_transaction(trans, root);
  3270. trans = NULL;
  3271. btrfs_btree_balance_dirty(root, nr);
  3272. }
  3273. if (ret == 0) {
  3274. ret = btrfs_orphan_del(trans, inode);
  3275. BUG_ON(ret);
  3276. }
  3277. nr = trans->blocks_used;
  3278. btrfs_end_transaction(trans, root);
  3279. btrfs_btree_balance_dirty(root, nr);
  3280. no_delete:
  3281. clear_inode(inode);
  3282. return;
  3283. }
  3284. /*
  3285. * this returns the key found in the dir entry in the location pointer.
  3286. * If no dir entries were found, location->objectid is 0.
  3287. */
  3288. static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
  3289. struct btrfs_key *location)
  3290. {
  3291. const char *name = dentry->d_name.name;
  3292. int namelen = dentry->d_name.len;
  3293. struct btrfs_dir_item *di;
  3294. struct btrfs_path *path;
  3295. struct btrfs_root *root = BTRFS_I(dir)->root;
  3296. int ret = 0;
  3297. path = btrfs_alloc_path();
  3298. BUG_ON(!path);
  3299. di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
  3300. namelen, 0);
  3301. if (IS_ERR(di))
  3302. ret = PTR_ERR(di);
  3303. if (!di || IS_ERR(di))
  3304. goto out_err;
  3305. btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
  3306. out:
  3307. btrfs_free_path(path);
  3308. return ret;
  3309. out_err:
  3310. location->objectid = 0;
  3311. goto out;
  3312. }
  3313. /*
  3314. * when we hit a tree root in a directory, the btrfs part of the inode
  3315. * needs to be changed to reflect the root directory of the tree root. This
  3316. * is kind of like crossing a mount point.
  3317. */
  3318. static int fixup_tree_root_location(struct btrfs_root *root,
  3319. struct inode *dir,
  3320. struct dentry *dentry,
  3321. struct btrfs_key *location,
  3322. struct btrfs_root **sub_root)
  3323. {
  3324. struct btrfs_path *path;
  3325. struct btrfs_root *new_root;
  3326. struct btrfs_root_ref *ref;
  3327. struct extent_buffer *leaf;
  3328. int ret;
  3329. int err = 0;
  3330. path = btrfs_alloc_path();
  3331. if (!path) {
  3332. err = -ENOMEM;
  3333. goto out;
  3334. }
  3335. err = -ENOENT;
  3336. ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
  3337. BTRFS_I(dir)->root->root_key.objectid,
  3338. location->objectid);
  3339. if (ret) {
  3340. if (ret < 0)
  3341. err = ret;
  3342. goto out;
  3343. }
  3344. leaf = path->nodes[0];
  3345. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
  3346. if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino ||
  3347. btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
  3348. goto out;
  3349. ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
  3350. (unsigned long)(ref + 1),
  3351. dentry->d_name.len);
  3352. if (ret)
  3353. goto out;
  3354. btrfs_release_path(root->fs_info->tree_root, path);
  3355. new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
  3356. if (IS_ERR(new_root)) {
  3357. err = PTR_ERR(new_root);
  3358. goto out;
  3359. }
  3360. if (btrfs_root_refs(&new_root->root_item) == 0) {
  3361. err = -ENOENT;
  3362. goto out;
  3363. }
  3364. *sub_root = new_root;
  3365. location->objectid = btrfs_root_dirid(&new_root->root_item);
  3366. location->type = BTRFS_INODE_ITEM_KEY;
  3367. location->offset = 0;
  3368. err = 0;
  3369. out:
  3370. btrfs_free_path(path);
  3371. return err;
  3372. }
  3373. static void inode_tree_add(struct inode *inode)
  3374. {
  3375. struct btrfs_root *root = BTRFS_I(inode)->root;
  3376. struct btrfs_inode *entry;
  3377. struct rb_node **p;
  3378. struct rb_node *parent;
  3379. again:
  3380. p = &root->inode_tree.rb_node;
  3381. parent = NULL;
  3382. if (hlist_unhashed(&inode->i_hash))
  3383. return;
  3384. spin_lock(&root->inode_lock);
  3385. while (*p) {
  3386. parent = *p;
  3387. entry = rb_entry(parent, struct btrfs_inode, rb_node);
  3388. if (inode->i_ino < entry->vfs_inode.i_ino)
  3389. p = &parent->rb_left;
  3390. else if (inode->i_ino > entry->vfs_inode.i_ino)
  3391. p = &parent->rb_right;
  3392. else {
  3393. WARN_ON(!(entry->vfs_inode.i_state &
  3394. (I_WILL_FREE | I_FREEING | I_CLEAR)));
  3395. rb_erase(parent, &root->inode_tree);
  3396. RB_CLEAR_NODE(parent);
  3397. spin_unlock(&root->inode_lock);
  3398. goto again;
  3399. }
  3400. }
  3401. rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
  3402. rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
  3403. spin_unlock(&root->inode_lock);
  3404. }
  3405. static void inode_tree_del(struct inode *inode)
  3406. {
  3407. struct btrfs_root *root = BTRFS_I(inode)->root;
  3408. int empty = 0;
  3409. spin_lock(&root->inode_lock);
  3410. if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
  3411. rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
  3412. RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
  3413. empty = RB_EMPTY_ROOT(&root->inode_tree);
  3414. }
  3415. spin_unlock(&root->inode_lock);
  3416. if (empty && btrfs_root_refs(&root->root_item) == 0) {
  3417. synchronize_srcu(&root->fs_info->subvol_srcu);
  3418. spin_lock(&root->inode_lock);
  3419. empty = RB_EMPTY_ROOT(&root->inode_tree);
  3420. spin_unlock(&root->inode_lock);
  3421. if (empty)
  3422. btrfs_add_dead_root(root);
  3423. }
  3424. }
  3425. int btrfs_invalidate_inodes(struct btrfs_root *root)
  3426. {
  3427. struct rb_node *node;
  3428. struct rb_node *prev;
  3429. struct btrfs_inode *entry;
  3430. struct inode *inode;
  3431. u64 objectid = 0;
  3432. WARN_ON(btrfs_root_refs(&root->root_item) != 0);
  3433. spin_lock(&root->inode_lock);
  3434. again:
  3435. node = root->inode_tree.rb_node;
  3436. prev = NULL;
  3437. while (node) {
  3438. prev = node;
  3439. entry = rb_entry(node, struct btrfs_inode, rb_node);
  3440. if (objectid < entry->vfs_inode.i_ino)
  3441. node = node->rb_left;
  3442. else if (objectid > entry->vfs_inode.i_ino)
  3443. node = node->rb_right;
  3444. else
  3445. break;
  3446. }
  3447. if (!node) {
  3448. while (prev) {
  3449. entry = rb_entry(prev, struct btrfs_inode, rb_node);
  3450. if (objectid <= entry->vfs_inode.i_ino) {
  3451. node = prev;
  3452. break;
  3453. }
  3454. prev = rb_next(prev);
  3455. }
  3456. }
  3457. while (node) {
  3458. entry = rb_entry(node, struct btrfs_inode, rb_node);
  3459. objectid = entry->vfs_inode.i_ino + 1;
  3460. inode = igrab(&entry->vfs_inode);
  3461. if (inode) {
  3462. spin_unlock(&root->inode_lock);
  3463. if (atomic_read(&inode->i_count) > 1)
  3464. d_prune_aliases(inode);
  3465. /*
  3466. * btrfs_drop_inode will remove it from
  3467. * the inode cache when its usage count
  3468. * hits zero.
  3469. */
  3470. iput(inode);
  3471. cond_resched();
  3472. spin_lock(&root->inode_lock);
  3473. goto again;
  3474. }
  3475. if (cond_resched_lock(&root->inode_lock))
  3476. goto again;
  3477. node = rb_next(node);
  3478. }
  3479. spin_unlock(&root->inode_lock);
  3480. return 0;
  3481. }
  3482. static int btrfs_init_locked_inode(struct inode *inode, void *p)
  3483. {
  3484. struct btrfs_iget_args *args = p;
  3485. inode->i_ino = args->ino;
  3486. BTRFS_I(inode)->root = args->root;
  3487. btrfs_set_inode_space_info(args->root, inode);
  3488. return 0;
  3489. }
  3490. static int btrfs_find_actor(struct inode *inode, void *opaque)
  3491. {
  3492. struct btrfs_iget_args *args = opaque;
  3493. return args->ino == inode->i_ino &&
  3494. args->root == BTRFS_I(inode)->root;
  3495. }
  3496. static struct inode *btrfs_iget_locked(struct super_block *s,
  3497. u64 objectid,
  3498. struct btrfs_root *root)
  3499. {
  3500. struct inode *inode;
  3501. struct btrfs_iget_args args;
  3502. args.ino = objectid;
  3503. args.root = root;
  3504. inode = iget5_locked(s, objectid, btrfs_find_actor,
  3505. btrfs_init_locked_inode,
  3506. (void *)&args);
  3507. return inode;
  3508. }
  3509. /* Get an inode object given its location and corresponding root.
  3510. * Returns in *is_new if the inode was read from disk
  3511. */
  3512. struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
  3513. struct btrfs_root *root, int *new)
  3514. {
  3515. struct inode *inode;
  3516. inode = btrfs_iget_locked(s, location->objectid, root);
  3517. if (!inode)
  3518. return ERR_PTR(-ENOMEM);
  3519. if (inode->i_state & I_NEW) {
  3520. BTRFS_I(inode)->root = root;
  3521. memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
  3522. btrfs_read_locked_inode(inode);
  3523. inode_tree_add(inode);
  3524. unlock_new_inode(inode);
  3525. if (new)
  3526. *new = 1;
  3527. }
  3528. return inode;
  3529. }
  3530. static struct inode *new_simple_dir(struct super_block *s,
  3531. struct btrfs_key *key,
  3532. struct btrfs_root *root)
  3533. {
  3534. struct inode *inode = new_inode(s);
  3535. if (!inode)
  3536. return ERR_PTR(-ENOMEM);
  3537. BTRFS_I(inode)->root = root;
  3538. memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
  3539. BTRFS_I(inode)->dummy_inode = 1;
  3540. inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
  3541. inode->i_op = &simple_dir_inode_operations;
  3542. inode->i_fop = &simple_dir_operations;
  3543. inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
  3544. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  3545. return inode;
  3546. }
  3547. struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
  3548. {
  3549. struct inode *inode;
  3550. struct btrfs_root *root = BTRFS_I(dir)->root;
  3551. struct btrfs_root *sub_root = root;
  3552. struct btrfs_key location;
  3553. int index;
  3554. int ret;
  3555. dentry->d_op = &btrfs_dentry_operations;
  3556. if (dentry->d_name.len > BTRFS_NAME_LEN)
  3557. return ERR_PTR(-ENAMETOOLONG);
  3558. ret = btrfs_inode_by_name(dir, dentry, &location);
  3559. if (ret < 0)
  3560. return ERR_PTR(ret);
  3561. if (location.objectid == 0)
  3562. return NULL;
  3563. if (location.type == BTRFS_INODE_ITEM_KEY) {
  3564. inode = btrfs_iget(dir->i_sb, &location, root, NULL);
  3565. return inode;
  3566. }
  3567. BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
  3568. index = srcu_read_lock(&root->fs_info->subvol_srcu);
  3569. ret = fixup_tree_root_location(root, dir, dentry,
  3570. &location, &sub_root);
  3571. if (ret < 0) {
  3572. if (ret != -ENOENT)
  3573. inode = ERR_PTR(ret);
  3574. else
  3575. inode = new_simple_dir(dir->i_sb, &location, sub_root);
  3576. } else {
  3577. inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
  3578. }
  3579. srcu_read_unlock(&root->fs_info->subvol_srcu, index);
  3580. if (root != sub_root) {
  3581. down_read(&root->fs_info->cleanup_work_sem);
  3582. if (!(inode->i_sb->s_flags & MS_RDONLY))
  3583. btrfs_orphan_cleanup(sub_root);
  3584. up_read(&root->fs_info->cleanup_work_sem);
  3585. }
  3586. return inode;
  3587. }
  3588. static int btrfs_dentry_delete(struct dentry *dentry)
  3589. {
  3590. struct btrfs_root *root;
  3591. if (!dentry->d_inode && !IS_ROOT(dentry))
  3592. dentry = dentry->d_parent;
  3593. if (dentry->d_inode) {
  3594. root = BTRFS_I(dentry->d_inode)->root;
  3595. if (btrfs_root_refs(&root->root_item) == 0)
  3596. return 1;
  3597. }
  3598. return 0;
  3599. }
  3600. static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
  3601. struct nameidata *nd)
  3602. {
  3603. struct inode *inode;
  3604. inode = btrfs_lookup_dentry(dir, dentry);
  3605. if (IS_ERR(inode))
  3606. return ERR_CAST(inode);
  3607. return d_splice_alias(inode, dentry);
  3608. }
  3609. static unsigned char btrfs_filetype_table[] = {
  3610. DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
  3611. };
  3612. static int btrfs_real_readdir(struct file *filp, void *dirent,
  3613. filldir_t filldir)
  3614. {
  3615. struct inode *inode = filp->f_dentry->d_inode;
  3616. struct btrfs_root *root = BTRFS_I(inode)->root;
  3617. struct btrfs_item *item;
  3618. struct btrfs_dir_item *di;
  3619. struct btrfs_key key;
  3620. struct btrfs_key found_key;
  3621. struct btrfs_path *path;
  3622. int ret;
  3623. u32 nritems;
  3624. struct extent_buffer *leaf;
  3625. int slot;
  3626. int advance;
  3627. unsigned char d_type;
  3628. int over = 0;
  3629. u32 di_cur;
  3630. u32 di_total;
  3631. u32 di_len;
  3632. int key_type = BTRFS_DIR_INDEX_KEY;
  3633. char tmp_name[32];
  3634. char *name_ptr;
  3635. int name_len;
  3636. /* FIXME, use a real flag for deciding about the key type */
  3637. if (root->fs_info->tree_root == root)
  3638. key_type = BTRFS_DIR_ITEM_KEY;
  3639. /* special case for "." */
  3640. if (filp->f_pos == 0) {
  3641. over = filldir(dirent, ".", 1,
  3642. 1, inode->i_ino,
  3643. DT_DIR);
  3644. if (over)
  3645. return 0;
  3646. filp->f_pos = 1;
  3647. }
  3648. /* special case for .., just use the back ref */
  3649. if (filp->f_pos == 1) {
  3650. u64 pino = parent_ino(filp->f_path.dentry);
  3651. over = filldir(dirent, "..", 2,
  3652. 2, pino, DT_DIR);
  3653. if (over)
  3654. return 0;
  3655. filp->f_pos = 2;
  3656. }
  3657. path = btrfs_alloc_path();
  3658. path->reada = 2;
  3659. btrfs_set_key_type(&key, key_type);
  3660. key.offset = filp->f_pos;
  3661. key.objectid = inode->i_ino;
  3662. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3663. if (ret < 0)
  3664. goto err;
  3665. advance = 0;
  3666. while (1) {
  3667. leaf = path->nodes[0];
  3668. nritems = btrfs_header_nritems(leaf);
  3669. slot = path->slots[0];
  3670. if (advance || slot >= nritems) {
  3671. if (slot >= nritems - 1) {
  3672. ret = btrfs_next_leaf(root, path);
  3673. if (ret)
  3674. break;
  3675. leaf = path->nodes[0];
  3676. nritems = btrfs_header_nritems(leaf);
  3677. slot = path->slots[0];
  3678. } else {
  3679. slot++;
  3680. path->slots[0]++;
  3681. }
  3682. }
  3683. advance = 1;
  3684. item = btrfs_item_nr(leaf, slot);
  3685. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  3686. if (found_key.objectid != key.objectid)
  3687. break;
  3688. if (btrfs_key_type(&found_key) != key_type)
  3689. break;
  3690. if (found_key.offset < filp->f_pos)
  3691. continue;
  3692. filp->f_pos = found_key.offset;
  3693. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  3694. di_cur = 0;
  3695. di_total = btrfs_item_size(leaf, item);
  3696. while (di_cur < di_total) {
  3697. struct btrfs_key location;
  3698. name_len = btrfs_dir_name_len(leaf, di);
  3699. if (name_len <= sizeof(tmp_name)) {
  3700. name_ptr = tmp_name;
  3701. } else {
  3702. name_ptr = kmalloc(name_len, GFP_NOFS);
  3703. if (!name_ptr) {
  3704. ret = -ENOMEM;
  3705. goto err;
  3706. }
  3707. }
  3708. read_extent_buffer(leaf, name_ptr,
  3709. (unsigned long)(di + 1), name_len);
  3710. d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
  3711. btrfs_dir_item_key_to_cpu(leaf, di, &location);
  3712. /* is this a reference to our own snapshot? If so
  3713. * skip it
  3714. */
  3715. if (location.type == BTRFS_ROOT_ITEM_KEY &&
  3716. location.objectid == root->root_key.objectid) {
  3717. over = 0;
  3718. goto skip;
  3719. }
  3720. over = filldir(dirent, name_ptr, name_len,
  3721. found_key.offset, location.objectid,
  3722. d_type);
  3723. skip:
  3724. if (name_ptr != tmp_name)
  3725. kfree(name_ptr);
  3726. if (over)
  3727. goto nopos;
  3728. di_len = btrfs_dir_name_len(leaf, di) +
  3729. btrfs_dir_data_len(leaf, di) + sizeof(*di);
  3730. di_cur += di_len;
  3731. di = (struct btrfs_dir_item *)((char *)di + di_len);
  3732. }
  3733. }
  3734. /* Reached end of directory/root. Bump pos past the last item. */
  3735. if (key_type == BTRFS_DIR_INDEX_KEY)
  3736. /*
  3737. * 32-bit glibc will use getdents64, but then strtol -
  3738. * so the last number we can serve is this.
  3739. */
  3740. filp->f_pos = 0x7fffffff;
  3741. else
  3742. filp->f_pos++;
  3743. nopos:
  3744. ret = 0;
  3745. err:
  3746. btrfs_free_path(path);
  3747. return ret;
  3748. }
  3749. int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
  3750. {
  3751. struct btrfs_root *root = BTRFS_I(inode)->root;
  3752. struct btrfs_trans_handle *trans;
  3753. int ret = 0;
  3754. if (BTRFS_I(inode)->dummy_inode)
  3755. return 0;
  3756. if (wbc->sync_mode == WB_SYNC_ALL) {
  3757. trans = btrfs_join_transaction(root, 1);
  3758. btrfs_set_trans_block_group(trans, inode);
  3759. ret = btrfs_commit_transaction(trans, root);
  3760. }
  3761. return ret;
  3762. }
  3763. /*
  3764. * This is somewhat expensive, updating the tree every time the
  3765. * inode changes. But, it is most likely to find the inode in cache.
  3766. * FIXME, needs more benchmarking...there are no reasons other than performance
  3767. * to keep or drop this code.
  3768. */
  3769. void btrfs_dirty_inode(struct inode *inode)
  3770. {
  3771. struct btrfs_root *root = BTRFS_I(inode)->root;
  3772. struct btrfs_trans_handle *trans;
  3773. int ret;
  3774. if (BTRFS_I(inode)->dummy_inode)
  3775. return;
  3776. trans = btrfs_join_transaction(root, 1);
  3777. btrfs_set_trans_block_group(trans, inode);
  3778. ret = btrfs_update_inode(trans, root, inode);
  3779. if (ret && ret == -ENOSPC) {
  3780. /* whoops, lets try again with the full transaction */
  3781. btrfs_end_transaction(trans, root);
  3782. trans = btrfs_start_transaction(root, 1);
  3783. if (IS_ERR(trans)) {
  3784. if (printk_ratelimit()) {
  3785. printk(KERN_ERR "btrfs: fail to "
  3786. "dirty inode %lu error %ld\n",
  3787. inode->i_ino, PTR_ERR(trans));
  3788. }
  3789. return;
  3790. }
  3791. btrfs_set_trans_block_group(trans, inode);
  3792. ret = btrfs_update_inode(trans, root, inode);
  3793. if (ret) {
  3794. if (printk_ratelimit()) {
  3795. printk(KERN_ERR "btrfs: fail to "
  3796. "dirty inode %lu error %d\n",
  3797. inode->i_ino, ret);
  3798. }
  3799. }
  3800. }
  3801. btrfs_end_transaction(trans, root);
  3802. }
  3803. /*
  3804. * find the highest existing sequence number in a directory
  3805. * and then set the in-memory index_cnt variable to reflect
  3806. * free sequence numbers
  3807. */
  3808. static int btrfs_set_inode_index_count(struct inode *inode)
  3809. {
  3810. struct btrfs_root *root = BTRFS_I(inode)->root;
  3811. struct btrfs_key key, found_key;
  3812. struct btrfs_path *path;
  3813. struct extent_buffer *leaf;
  3814. int ret;
  3815. key.objectid = inode->i_ino;
  3816. btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
  3817. key.offset = (u64)-1;
  3818. path = btrfs_alloc_path();
  3819. if (!path)
  3820. return -ENOMEM;
  3821. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3822. if (ret < 0)
  3823. goto out;
  3824. /* FIXME: we should be able to handle this */
  3825. if (ret == 0)
  3826. goto out;
  3827. ret = 0;
  3828. /*
  3829. * MAGIC NUMBER EXPLANATION:
  3830. * since we search a directory based on f_pos we have to start at 2
  3831. * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
  3832. * else has to start at 2
  3833. */
  3834. if (path->slots[0] == 0) {
  3835. BTRFS_I(inode)->index_cnt = 2;
  3836. goto out;
  3837. }
  3838. path->slots[0]--;
  3839. leaf = path->nodes[0];
  3840. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3841. if (found_key.objectid != inode->i_ino ||
  3842. btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
  3843. BTRFS_I(inode)->index_cnt = 2;
  3844. goto out;
  3845. }
  3846. BTRFS_I(inode)->index_cnt = found_key.offset + 1;
  3847. out:
  3848. btrfs_free_path(path);
  3849. return ret;
  3850. }
  3851. /*
  3852. * helper to find a free sequence number in a given directory. This current
  3853. * code is very simple, later versions will do smarter things in the btree
  3854. */
  3855. int btrfs_set_inode_index(struct inode *dir, u64 *index)
  3856. {
  3857. int ret = 0;
  3858. if (BTRFS_I(dir)->index_cnt == (u64)-1) {
  3859. ret = btrfs_set_inode_index_count(dir);
  3860. if (ret)
  3861. return ret;
  3862. }
  3863. *index = BTRFS_I(dir)->index_cnt;
  3864. BTRFS_I(dir)->index_cnt++;
  3865. return ret;
  3866. }
  3867. static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
  3868. struct btrfs_root *root,
  3869. struct inode *dir,
  3870. const char *name, int name_len,
  3871. u64 ref_objectid, u64 objectid,
  3872. u64 alloc_hint, int mode, u64 *index)
  3873. {
  3874. struct inode *inode;
  3875. struct btrfs_inode_item *inode_item;
  3876. struct btrfs_key *location;
  3877. struct btrfs_path *path;
  3878. struct btrfs_inode_ref *ref;
  3879. struct btrfs_key key[2];
  3880. u32 sizes[2];
  3881. unsigned long ptr;
  3882. int ret;
  3883. int owner;
  3884. path = btrfs_alloc_path();
  3885. BUG_ON(!path);
  3886. inode = new_inode(root->fs_info->sb);
  3887. if (!inode)
  3888. return ERR_PTR(-ENOMEM);
  3889. if (dir) {
  3890. ret = btrfs_set_inode_index(dir, index);
  3891. if (ret) {
  3892. iput(inode);
  3893. return ERR_PTR(ret);
  3894. }
  3895. }
  3896. /*
  3897. * index_cnt is ignored for everything but a dir,
  3898. * btrfs_get_inode_index_count has an explanation for the magic
  3899. * number
  3900. */
  3901. BTRFS_I(inode)->index_cnt = 2;
  3902. BTRFS_I(inode)->root = root;
  3903. BTRFS_I(inode)->generation = trans->transid;
  3904. btrfs_set_inode_space_info(root, inode);
  3905. if (mode & S_IFDIR)
  3906. owner = 0;
  3907. else
  3908. owner = 1;
  3909. BTRFS_I(inode)->block_group =
  3910. btrfs_find_block_group(root, 0, alloc_hint, owner);
  3911. key[0].objectid = objectid;
  3912. btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
  3913. key[0].offset = 0;
  3914. key[1].objectid = objectid;
  3915. btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
  3916. key[1].offset = ref_objectid;
  3917. sizes[0] = sizeof(struct btrfs_inode_item);
  3918. sizes[1] = name_len + sizeof(*ref);
  3919. path->leave_spinning = 1;
  3920. ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
  3921. if (ret != 0)
  3922. goto fail;
  3923. inode_init_owner(inode, dir, mode);
  3924. inode->i_ino = objectid;
  3925. inode_set_bytes(inode, 0);
  3926. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  3927. inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3928. struct btrfs_inode_item);
  3929. fill_inode_item(trans, path->nodes[0], inode_item, inode);
  3930. ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
  3931. struct btrfs_inode_ref);
  3932. btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
  3933. btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
  3934. ptr = (unsigned long)(ref + 1);
  3935. write_extent_buffer(path->nodes[0], name, ptr, name_len);
  3936. btrfs_mark_buffer_dirty(path->nodes[0]);
  3937. btrfs_free_path(path);
  3938. location = &BTRFS_I(inode)->location;
  3939. location->objectid = objectid;
  3940. location->offset = 0;
  3941. btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
  3942. btrfs_inherit_iflags(inode, dir);
  3943. if ((mode & S_IFREG)) {
  3944. if (btrfs_test_opt(root, NODATASUM))
  3945. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
  3946. if (btrfs_test_opt(root, NODATACOW))
  3947. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
  3948. }
  3949. insert_inode_hash(inode);
  3950. inode_tree_add(inode);
  3951. return inode;
  3952. fail:
  3953. if (dir)
  3954. BTRFS_I(dir)->index_cnt--;
  3955. btrfs_free_path(path);
  3956. iput(inode);
  3957. return ERR_PTR(ret);
  3958. }
  3959. static inline u8 btrfs_inode_type(struct inode *inode)
  3960. {
  3961. return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
  3962. }
  3963. /*
  3964. * utility function to add 'inode' into 'parent_inode' with
  3965. * a give name and a given sequence number.
  3966. * if 'add_backref' is true, also insert a backref from the
  3967. * inode to the parent directory.
  3968. */
  3969. int btrfs_add_link(struct btrfs_trans_handle *trans,
  3970. struct inode *parent_inode, struct inode *inode,
  3971. const char *name, int name_len, int add_backref, u64 index)
  3972. {
  3973. int ret = 0;
  3974. struct btrfs_key key;
  3975. struct btrfs_root *root = BTRFS_I(parent_inode)->root;
  3976. if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  3977. memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
  3978. } else {
  3979. key.objectid = inode->i_ino;
  3980. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  3981. key.offset = 0;
  3982. }
  3983. if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  3984. ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
  3985. key.objectid, root->root_key.objectid,
  3986. parent_inode->i_ino,
  3987. index, name, name_len);
  3988. } else if (add_backref) {
  3989. ret = btrfs_insert_inode_ref(trans, root,
  3990. name, name_len, inode->i_ino,
  3991. parent_inode->i_ino, index);
  3992. }
  3993. if (ret == 0) {
  3994. ret = btrfs_insert_dir_item(trans, root, name, name_len,
  3995. parent_inode->i_ino, &key,
  3996. btrfs_inode_type(inode), index);
  3997. BUG_ON(ret);
  3998. btrfs_i_size_write(parent_inode, parent_inode->i_size +
  3999. name_len * 2);
  4000. parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
  4001. ret = btrfs_update_inode(trans, root, parent_inode);
  4002. }
  4003. return ret;
  4004. }
  4005. static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
  4006. struct dentry *dentry, struct inode *inode,
  4007. int backref, u64 index)
  4008. {
  4009. int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
  4010. inode, dentry->d_name.name,
  4011. dentry->d_name.len, backref, index);
  4012. if (!err) {
  4013. d_instantiate(dentry, inode);
  4014. return 0;
  4015. }
  4016. if (err > 0)
  4017. err = -EEXIST;
  4018. return err;
  4019. }
  4020. static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
  4021. int mode, dev_t rdev)
  4022. {
  4023. struct btrfs_trans_handle *trans;
  4024. struct btrfs_root *root = BTRFS_I(dir)->root;
  4025. struct inode *inode = NULL;
  4026. int err;
  4027. int drop_inode = 0;
  4028. u64 objectid;
  4029. unsigned long nr = 0;
  4030. u64 index = 0;
  4031. if (!new_valid_dev(rdev))
  4032. return -EINVAL;
  4033. err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
  4034. if (err)
  4035. return err;
  4036. /*
  4037. * 2 for inode item and ref
  4038. * 2 for dir items
  4039. * 1 for xattr if selinux is on
  4040. */
  4041. trans = btrfs_start_transaction(root, 5);
  4042. if (IS_ERR(trans))
  4043. return PTR_ERR(trans);
  4044. btrfs_set_trans_block_group(trans, dir);
  4045. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  4046. dentry->d_name.len,
  4047. dentry->d_parent->d_inode->i_ino, objectid,
  4048. BTRFS_I(dir)->block_group, mode, &index);
  4049. err = PTR_ERR(inode);
  4050. if (IS_ERR(inode))
  4051. goto out_unlock;
  4052. err = btrfs_init_inode_security(trans, inode, dir);
  4053. if (err) {
  4054. drop_inode = 1;
  4055. goto out_unlock;
  4056. }
  4057. btrfs_set_trans_block_group(trans, inode);
  4058. err = btrfs_add_nondir(trans, dentry, inode, 0, index);
  4059. if (err)
  4060. drop_inode = 1;
  4061. else {
  4062. inode->i_op = &btrfs_special_inode_operations;
  4063. init_special_inode(inode, inode->i_mode, rdev);
  4064. btrfs_update_inode(trans, root, inode);
  4065. }
  4066. btrfs_update_inode_block_group(trans, inode);
  4067. btrfs_update_inode_block_group(trans, dir);
  4068. out_unlock:
  4069. nr = trans->blocks_used;
  4070. btrfs_end_transaction_throttle(trans, root);
  4071. btrfs_btree_balance_dirty(root, nr);
  4072. if (drop_inode) {
  4073. inode_dec_link_count(inode);
  4074. iput(inode);
  4075. }
  4076. return err;
  4077. }
  4078. static int btrfs_create(struct inode *dir, struct dentry *dentry,
  4079. int mode, struct nameidata *nd)
  4080. {
  4081. struct btrfs_trans_handle *trans;
  4082. struct btrfs_root *root = BTRFS_I(dir)->root;
  4083. struct inode *inode = NULL;
  4084. int drop_inode = 0;
  4085. int err;
  4086. unsigned long nr = 0;
  4087. u64 objectid;
  4088. u64 index = 0;
  4089. err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
  4090. if (err)
  4091. return err;
  4092. /*
  4093. * 2 for inode item and ref
  4094. * 2 for dir items
  4095. * 1 for xattr if selinux is on
  4096. */
  4097. trans = btrfs_start_transaction(root, 5);
  4098. if (IS_ERR(trans))
  4099. return PTR_ERR(trans);
  4100. btrfs_set_trans_block_group(trans, dir);
  4101. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  4102. dentry->d_name.len,
  4103. dentry->d_parent->d_inode->i_ino,
  4104. objectid, BTRFS_I(dir)->block_group, mode,
  4105. &index);
  4106. err = PTR_ERR(inode);
  4107. if (IS_ERR(inode))
  4108. goto out_unlock;
  4109. err = btrfs_init_inode_security(trans, inode, dir);
  4110. if (err) {
  4111. drop_inode = 1;
  4112. goto out_unlock;
  4113. }
  4114. btrfs_set_trans_block_group(trans, inode);
  4115. err = btrfs_add_nondir(trans, dentry, inode, 0, index);
  4116. if (err)
  4117. drop_inode = 1;
  4118. else {
  4119. inode->i_mapping->a_ops = &btrfs_aops;
  4120. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  4121. inode->i_fop = &btrfs_file_operations;
  4122. inode->i_op = &btrfs_file_inode_operations;
  4123. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  4124. }
  4125. btrfs_update_inode_block_group(trans, inode);
  4126. btrfs_update_inode_block_group(trans, dir);
  4127. out_unlock:
  4128. nr = trans->blocks_used;
  4129. btrfs_end_transaction_throttle(trans, root);
  4130. if (drop_inode) {
  4131. inode_dec_link_count(inode);
  4132. iput(inode);
  4133. }
  4134. btrfs_btree_balance_dirty(root, nr);
  4135. return err;
  4136. }
  4137. static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
  4138. struct dentry *dentry)
  4139. {
  4140. struct btrfs_trans_handle *trans;
  4141. struct btrfs_root *root = BTRFS_I(dir)->root;
  4142. struct inode *inode = old_dentry->d_inode;
  4143. u64 index;
  4144. unsigned long nr = 0;
  4145. int err;
  4146. int drop_inode = 0;
  4147. if (inode->i_nlink == 0)
  4148. return -ENOENT;
  4149. /* do not allow sys_link's with other subvols of the same device */
  4150. if (root->objectid != BTRFS_I(inode)->root->objectid)
  4151. return -EPERM;
  4152. btrfs_inc_nlink(inode);
  4153. err = btrfs_set_inode_index(dir, &index);
  4154. if (err)
  4155. goto fail;
  4156. /*
  4157. * 1 item for inode ref
  4158. * 2 items for dir items
  4159. */
  4160. trans = btrfs_start_transaction(root, 3);
  4161. if (IS_ERR(trans)) {
  4162. err = PTR_ERR(trans);
  4163. goto fail;
  4164. }
  4165. btrfs_set_trans_block_group(trans, dir);
  4166. atomic_inc(&inode->i_count);
  4167. err = btrfs_add_nondir(trans, dentry, inode, 1, index);
  4168. if (err) {
  4169. drop_inode = 1;
  4170. } else {
  4171. btrfs_update_inode_block_group(trans, dir);
  4172. err = btrfs_update_inode(trans, root, inode);
  4173. BUG_ON(err);
  4174. btrfs_log_new_name(trans, inode, NULL, dentry->d_parent);
  4175. }
  4176. nr = trans->blocks_used;
  4177. btrfs_end_transaction_throttle(trans, root);
  4178. fail:
  4179. if (drop_inode) {
  4180. inode_dec_link_count(inode);
  4181. iput(inode);
  4182. }
  4183. btrfs_btree_balance_dirty(root, nr);
  4184. return err;
  4185. }
  4186. static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  4187. {
  4188. struct inode *inode = NULL;
  4189. struct btrfs_trans_handle *trans;
  4190. struct btrfs_root *root = BTRFS_I(dir)->root;
  4191. int err = 0;
  4192. int drop_on_err = 0;
  4193. u64 objectid = 0;
  4194. u64 index = 0;
  4195. unsigned long nr = 1;
  4196. err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
  4197. if (err)
  4198. return err;
  4199. /*
  4200. * 2 items for inode and ref
  4201. * 2 items for dir items
  4202. * 1 for xattr if selinux is on
  4203. */
  4204. trans = btrfs_start_transaction(root, 5);
  4205. if (IS_ERR(trans))
  4206. return PTR_ERR(trans);
  4207. btrfs_set_trans_block_group(trans, dir);
  4208. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  4209. dentry->d_name.len,
  4210. dentry->d_parent->d_inode->i_ino, objectid,
  4211. BTRFS_I(dir)->block_group, S_IFDIR | mode,
  4212. &index);
  4213. if (IS_ERR(inode)) {
  4214. err = PTR_ERR(inode);
  4215. goto out_fail;
  4216. }
  4217. drop_on_err = 1;
  4218. err = btrfs_init_inode_security(trans, inode, dir);
  4219. if (err)
  4220. goto out_fail;
  4221. inode->i_op = &btrfs_dir_inode_operations;
  4222. inode->i_fop = &btrfs_dir_file_operations;
  4223. btrfs_set_trans_block_group(trans, inode);
  4224. btrfs_i_size_write(inode, 0);
  4225. err = btrfs_update_inode(trans, root, inode);
  4226. if (err)
  4227. goto out_fail;
  4228. err = btrfs_add_link(trans, dentry->d_parent->d_inode,
  4229. inode, dentry->d_name.name,
  4230. dentry->d_name.len, 0, index);
  4231. if (err)
  4232. goto out_fail;
  4233. d_instantiate(dentry, inode);
  4234. drop_on_err = 0;
  4235. btrfs_update_inode_block_group(trans, inode);
  4236. btrfs_update_inode_block_group(trans, dir);
  4237. out_fail:
  4238. nr = trans->blocks_used;
  4239. btrfs_end_transaction_throttle(trans, root);
  4240. if (drop_on_err)
  4241. iput(inode);
  4242. btrfs_btree_balance_dirty(root, nr);
  4243. return err;
  4244. }
  4245. /* helper for btfs_get_extent. Given an existing extent in the tree,
  4246. * and an extent that you want to insert, deal with overlap and insert
  4247. * the new extent into the tree.
  4248. */
  4249. static int merge_extent_mapping(struct extent_map_tree *em_tree,
  4250. struct extent_map *existing,
  4251. struct extent_map *em,
  4252. u64 map_start, u64 map_len)
  4253. {
  4254. u64 start_diff;
  4255. BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
  4256. start_diff = map_start - em->start;
  4257. em->start = map_start;
  4258. em->len = map_len;
  4259. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  4260. !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  4261. em->block_start += start_diff;
  4262. em->block_len -= start_diff;
  4263. }
  4264. return add_extent_mapping(em_tree, em);
  4265. }
  4266. static noinline int uncompress_inline(struct btrfs_path *path,
  4267. struct inode *inode, struct page *page,
  4268. size_t pg_offset, u64 extent_offset,
  4269. struct btrfs_file_extent_item *item)
  4270. {
  4271. int ret;
  4272. struct extent_buffer *leaf = path->nodes[0];
  4273. char *tmp;
  4274. size_t max_size;
  4275. unsigned long inline_size;
  4276. unsigned long ptr;
  4277. WARN_ON(pg_offset != 0);
  4278. max_size = btrfs_file_extent_ram_bytes(leaf, item);
  4279. inline_size = btrfs_file_extent_inline_item_len(leaf,
  4280. btrfs_item_nr(leaf, path->slots[0]));
  4281. tmp = kmalloc(inline_size, GFP_NOFS);
  4282. ptr = btrfs_file_extent_inline_start(item);
  4283. read_extent_buffer(leaf, tmp, ptr, inline_size);
  4284. max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
  4285. ret = btrfs_zlib_decompress(tmp, page, extent_offset,
  4286. inline_size, max_size);
  4287. if (ret) {
  4288. char *kaddr = kmap_atomic(page, KM_USER0);
  4289. unsigned long copy_size = min_t(u64,
  4290. PAGE_CACHE_SIZE - pg_offset,
  4291. max_size - extent_offset);
  4292. memset(kaddr + pg_offset, 0, copy_size);
  4293. kunmap_atomic(kaddr, KM_USER0);
  4294. }
  4295. kfree(tmp);
  4296. return 0;
  4297. }
  4298. /*
  4299. * a bit scary, this does extent mapping from logical file offset to the disk.
  4300. * the ugly parts come from merging extents from the disk with the in-ram
  4301. * representation. This gets more complex because of the data=ordered code,
  4302. * where the in-ram extents might be locked pending data=ordered completion.
  4303. *
  4304. * This also copies inline extents directly into the page.
  4305. */
  4306. struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
  4307. size_t pg_offset, u64 start, u64 len,
  4308. int create)
  4309. {
  4310. int ret;
  4311. int err = 0;
  4312. u64 bytenr;
  4313. u64 extent_start = 0;
  4314. u64 extent_end = 0;
  4315. u64 objectid = inode->i_ino;
  4316. u32 found_type;
  4317. struct btrfs_path *path = NULL;
  4318. struct btrfs_root *root = BTRFS_I(inode)->root;
  4319. struct btrfs_file_extent_item *item;
  4320. struct extent_buffer *leaf;
  4321. struct btrfs_key found_key;
  4322. struct extent_map *em = NULL;
  4323. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  4324. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  4325. struct btrfs_trans_handle *trans = NULL;
  4326. int compressed;
  4327. again:
  4328. read_lock(&em_tree->lock);
  4329. em = lookup_extent_mapping(em_tree, start, len);
  4330. if (em)
  4331. em->bdev = root->fs_info->fs_devices->latest_bdev;
  4332. read_unlock(&em_tree->lock);
  4333. if (em) {
  4334. if (em->start > start || em->start + em->len <= start)
  4335. free_extent_map(em);
  4336. else if (em->block_start == EXTENT_MAP_INLINE && page)
  4337. free_extent_map(em);
  4338. else
  4339. goto out;
  4340. }
  4341. em = alloc_extent_map(GFP_NOFS);
  4342. if (!em) {
  4343. err = -ENOMEM;
  4344. goto out;
  4345. }
  4346. em->bdev = root->fs_info->fs_devices->latest_bdev;
  4347. em->start = EXTENT_MAP_HOLE;
  4348. em->orig_start = EXTENT_MAP_HOLE;
  4349. em->len = (u64)-1;
  4350. em->block_len = (u64)-1;
  4351. if (!path) {
  4352. path = btrfs_alloc_path();
  4353. BUG_ON(!path);
  4354. }
  4355. ret = btrfs_lookup_file_extent(trans, root, path,
  4356. objectid, start, trans != NULL);
  4357. if (ret < 0) {
  4358. err = ret;
  4359. goto out;
  4360. }
  4361. if (ret != 0) {
  4362. if (path->slots[0] == 0)
  4363. goto not_found;
  4364. path->slots[0]--;
  4365. }
  4366. leaf = path->nodes[0];
  4367. item = btrfs_item_ptr(leaf, path->slots[0],
  4368. struct btrfs_file_extent_item);
  4369. /* are we inside the extent that was found? */
  4370. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  4371. found_type = btrfs_key_type(&found_key);
  4372. if (found_key.objectid != objectid ||
  4373. found_type != BTRFS_EXTENT_DATA_KEY) {
  4374. goto not_found;
  4375. }
  4376. found_type = btrfs_file_extent_type(leaf, item);
  4377. extent_start = found_key.offset;
  4378. compressed = btrfs_file_extent_compression(leaf, item);
  4379. if (found_type == BTRFS_FILE_EXTENT_REG ||
  4380. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  4381. extent_end = extent_start +
  4382. btrfs_file_extent_num_bytes(leaf, item);
  4383. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  4384. size_t size;
  4385. size = btrfs_file_extent_inline_len(leaf, item);
  4386. extent_end = (extent_start + size + root->sectorsize - 1) &
  4387. ~((u64)root->sectorsize - 1);
  4388. }
  4389. if (start >= extent_end) {
  4390. path->slots[0]++;
  4391. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  4392. ret = btrfs_next_leaf(root, path);
  4393. if (ret < 0) {
  4394. err = ret;
  4395. goto out;
  4396. }
  4397. if (ret > 0)
  4398. goto not_found;
  4399. leaf = path->nodes[0];
  4400. }
  4401. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  4402. if (found_key.objectid != objectid ||
  4403. found_key.type != BTRFS_EXTENT_DATA_KEY)
  4404. goto not_found;
  4405. if (start + len <= found_key.offset)
  4406. goto not_found;
  4407. em->start = start;
  4408. em->len = found_key.offset - start;
  4409. goto not_found_em;
  4410. }
  4411. if (found_type == BTRFS_FILE_EXTENT_REG ||
  4412. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  4413. em->start = extent_start;
  4414. em->len = extent_end - extent_start;
  4415. em->orig_start = extent_start -
  4416. btrfs_file_extent_offset(leaf, item);
  4417. bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
  4418. if (bytenr == 0) {
  4419. em->block_start = EXTENT_MAP_HOLE;
  4420. goto insert;
  4421. }
  4422. if (compressed) {
  4423. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  4424. em->block_start = bytenr;
  4425. em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
  4426. item);
  4427. } else {
  4428. bytenr += btrfs_file_extent_offset(leaf, item);
  4429. em->block_start = bytenr;
  4430. em->block_len = em->len;
  4431. if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
  4432. set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
  4433. }
  4434. goto insert;
  4435. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  4436. unsigned long ptr;
  4437. char *map;
  4438. size_t size;
  4439. size_t extent_offset;
  4440. size_t copy_size;
  4441. em->block_start = EXTENT_MAP_INLINE;
  4442. if (!page || create) {
  4443. em->start = extent_start;
  4444. em->len = extent_end - extent_start;
  4445. goto out;
  4446. }
  4447. size = btrfs_file_extent_inline_len(leaf, item);
  4448. extent_offset = page_offset(page) + pg_offset - extent_start;
  4449. copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
  4450. size - extent_offset);
  4451. em->start = extent_start + extent_offset;
  4452. em->len = (copy_size + root->sectorsize - 1) &
  4453. ~((u64)root->sectorsize - 1);
  4454. em->orig_start = EXTENT_MAP_INLINE;
  4455. if (compressed)
  4456. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  4457. ptr = btrfs_file_extent_inline_start(item) + extent_offset;
  4458. if (create == 0 && !PageUptodate(page)) {
  4459. if (btrfs_file_extent_compression(leaf, item) ==
  4460. BTRFS_COMPRESS_ZLIB) {
  4461. ret = uncompress_inline(path, inode, page,
  4462. pg_offset,
  4463. extent_offset, item);
  4464. BUG_ON(ret);
  4465. } else {
  4466. map = kmap(page);
  4467. read_extent_buffer(leaf, map + pg_offset, ptr,
  4468. copy_size);
  4469. if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
  4470. memset(map + pg_offset + copy_size, 0,
  4471. PAGE_CACHE_SIZE - pg_offset -
  4472. copy_size);
  4473. }
  4474. kunmap(page);
  4475. }
  4476. flush_dcache_page(page);
  4477. } else if (create && PageUptodate(page)) {
  4478. WARN_ON(1);
  4479. if (!trans) {
  4480. kunmap(page);
  4481. free_extent_map(em);
  4482. em = NULL;
  4483. btrfs_release_path(root, path);
  4484. trans = btrfs_join_transaction(root, 1);
  4485. goto again;
  4486. }
  4487. map = kmap(page);
  4488. write_extent_buffer(leaf, map + pg_offset, ptr,
  4489. copy_size);
  4490. kunmap(page);
  4491. btrfs_mark_buffer_dirty(leaf);
  4492. }
  4493. set_extent_uptodate(io_tree, em->start,
  4494. extent_map_end(em) - 1, GFP_NOFS);
  4495. goto insert;
  4496. } else {
  4497. printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
  4498. WARN_ON(1);
  4499. }
  4500. not_found:
  4501. em->start = start;
  4502. em->len = len;
  4503. not_found_em:
  4504. em->block_start = EXTENT_MAP_HOLE;
  4505. set_bit(EXTENT_FLAG_VACANCY, &em->flags);
  4506. insert:
  4507. btrfs_release_path(root, path);
  4508. if (em->start > start || extent_map_end(em) <= start) {
  4509. printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
  4510. "[%llu %llu]\n", (unsigned long long)em->start,
  4511. (unsigned long long)em->len,
  4512. (unsigned long long)start,
  4513. (unsigned long long)len);
  4514. err = -EIO;
  4515. goto out;
  4516. }
  4517. err = 0;
  4518. write_lock(&em_tree->lock);
  4519. ret = add_extent_mapping(em_tree, em);
  4520. /* it is possible that someone inserted the extent into the tree
  4521. * while we had the lock dropped. It is also possible that
  4522. * an overlapping map exists in the tree
  4523. */
  4524. if (ret == -EEXIST) {
  4525. struct extent_map *existing;
  4526. ret = 0;
  4527. existing = lookup_extent_mapping(em_tree, start, len);
  4528. if (existing && (existing->start > start ||
  4529. existing->start + existing->len <= start)) {
  4530. free_extent_map(existing);
  4531. existing = NULL;
  4532. }
  4533. if (!existing) {
  4534. existing = lookup_extent_mapping(em_tree, em->start,
  4535. em->len);
  4536. if (existing) {
  4537. err = merge_extent_mapping(em_tree, existing,
  4538. em, start,
  4539. root->sectorsize);
  4540. free_extent_map(existing);
  4541. if (err) {
  4542. free_extent_map(em);
  4543. em = NULL;
  4544. }
  4545. } else {
  4546. err = -EIO;
  4547. free_extent_map(em);
  4548. em = NULL;
  4549. }
  4550. } else {
  4551. free_extent_map(em);
  4552. em = existing;
  4553. err = 0;
  4554. }
  4555. }
  4556. write_unlock(&em_tree->lock);
  4557. out:
  4558. if (path)
  4559. btrfs_free_path(path);
  4560. if (trans) {
  4561. ret = btrfs_end_transaction(trans, root);
  4562. if (!err)
  4563. err = ret;
  4564. }
  4565. if (err) {
  4566. free_extent_map(em);
  4567. return ERR_PTR(err);
  4568. }
  4569. return em;
  4570. }
  4571. static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
  4572. u64 start, u64 len)
  4573. {
  4574. struct btrfs_root *root = BTRFS_I(inode)->root;
  4575. struct btrfs_trans_handle *trans;
  4576. struct extent_map *em;
  4577. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  4578. struct btrfs_key ins;
  4579. u64 alloc_hint;
  4580. int ret;
  4581. btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
  4582. trans = btrfs_join_transaction(root, 0);
  4583. if (!trans)
  4584. return ERR_PTR(-ENOMEM);
  4585. trans->block_rsv = &root->fs_info->delalloc_block_rsv;
  4586. alloc_hint = get_extent_allocation_hint(inode, start, len);
  4587. ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
  4588. alloc_hint, (u64)-1, &ins, 1);
  4589. if (ret) {
  4590. em = ERR_PTR(ret);
  4591. goto out;
  4592. }
  4593. em = alloc_extent_map(GFP_NOFS);
  4594. if (!em) {
  4595. em = ERR_PTR(-ENOMEM);
  4596. goto out;
  4597. }
  4598. em->start = start;
  4599. em->orig_start = em->start;
  4600. em->len = ins.offset;
  4601. em->block_start = ins.objectid;
  4602. em->block_len = ins.offset;
  4603. em->bdev = root->fs_info->fs_devices->latest_bdev;
  4604. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  4605. while (1) {
  4606. write_lock(&em_tree->lock);
  4607. ret = add_extent_mapping(em_tree, em);
  4608. write_unlock(&em_tree->lock);
  4609. if (ret != -EEXIST)
  4610. break;
  4611. btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0);
  4612. }
  4613. ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
  4614. ins.offset, ins.offset, 0);
  4615. if (ret) {
  4616. btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
  4617. em = ERR_PTR(ret);
  4618. }
  4619. out:
  4620. btrfs_end_transaction(trans, root);
  4621. return em;
  4622. }
  4623. /*
  4624. * returns 1 when the nocow is safe, < 1 on error, 0 if the
  4625. * block must be cow'd
  4626. */
  4627. static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
  4628. struct inode *inode, u64 offset, u64 len)
  4629. {
  4630. struct btrfs_path *path;
  4631. int ret;
  4632. struct extent_buffer *leaf;
  4633. struct btrfs_root *root = BTRFS_I(inode)->root;
  4634. struct btrfs_file_extent_item *fi;
  4635. struct btrfs_key key;
  4636. u64 disk_bytenr;
  4637. u64 backref_offset;
  4638. u64 extent_end;
  4639. u64 num_bytes;
  4640. int slot;
  4641. int found_type;
  4642. path = btrfs_alloc_path();
  4643. if (!path)
  4644. return -ENOMEM;
  4645. ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
  4646. offset, 0);
  4647. if (ret < 0)
  4648. goto out;
  4649. slot = path->slots[0];
  4650. if (ret == 1) {
  4651. if (slot == 0) {
  4652. /* can't find the item, must cow */
  4653. ret = 0;
  4654. goto out;
  4655. }
  4656. slot--;
  4657. }
  4658. ret = 0;
  4659. leaf = path->nodes[0];
  4660. btrfs_item_key_to_cpu(leaf, &key, slot);
  4661. if (key.objectid != inode->i_ino ||
  4662. key.type != BTRFS_EXTENT_DATA_KEY) {
  4663. /* not our file or wrong item type, must cow */
  4664. goto out;
  4665. }
  4666. if (key.offset > offset) {
  4667. /* Wrong offset, must cow */
  4668. goto out;
  4669. }
  4670. fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  4671. found_type = btrfs_file_extent_type(leaf, fi);
  4672. if (found_type != BTRFS_FILE_EXTENT_REG &&
  4673. found_type != BTRFS_FILE_EXTENT_PREALLOC) {
  4674. /* not a regular extent, must cow */
  4675. goto out;
  4676. }
  4677. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  4678. backref_offset = btrfs_file_extent_offset(leaf, fi);
  4679. extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
  4680. if (extent_end < offset + len) {
  4681. /* extent doesn't include our full range, must cow */
  4682. goto out;
  4683. }
  4684. if (btrfs_extent_readonly(root, disk_bytenr))
  4685. goto out;
  4686. /*
  4687. * look for other files referencing this extent, if we
  4688. * find any we must cow
  4689. */
  4690. if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
  4691. key.offset - backref_offset, disk_bytenr))
  4692. goto out;
  4693. /*
  4694. * adjust disk_bytenr and num_bytes to cover just the bytes
  4695. * in this extent we are about to write. If there
  4696. * are any csums in that range we have to cow in order
  4697. * to keep the csums correct
  4698. */
  4699. disk_bytenr += backref_offset;
  4700. disk_bytenr += offset - key.offset;
  4701. num_bytes = min(offset + len, extent_end) - offset;
  4702. if (csum_exist_in_range(root, disk_bytenr, num_bytes))
  4703. goto out;
  4704. /*
  4705. * all of the above have passed, it is safe to overwrite this extent
  4706. * without cow
  4707. */
  4708. ret = 1;
  4709. out:
  4710. btrfs_free_path(path);
  4711. return ret;
  4712. }
  4713. static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
  4714. struct buffer_head *bh_result, int create)
  4715. {
  4716. struct extent_map *em;
  4717. struct btrfs_root *root = BTRFS_I(inode)->root;
  4718. u64 start = iblock << inode->i_blkbits;
  4719. u64 len = bh_result->b_size;
  4720. struct btrfs_trans_handle *trans;
  4721. em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
  4722. if (IS_ERR(em))
  4723. return PTR_ERR(em);
  4724. /*
  4725. * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
  4726. * io. INLINE is special, and we could probably kludge it in here, but
  4727. * it's still buffered so for safety lets just fall back to the generic
  4728. * buffered path.
  4729. *
  4730. * For COMPRESSED we _have_ to read the entire extent in so we can
  4731. * decompress it, so there will be buffering required no matter what we
  4732. * do, so go ahead and fallback to buffered.
  4733. *
  4734. * We return -ENOTBLK because thats what makes DIO go ahead and go back
  4735. * to buffered IO. Don't blame me, this is the price we pay for using
  4736. * the generic code.
  4737. */
  4738. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
  4739. em->block_start == EXTENT_MAP_INLINE) {
  4740. free_extent_map(em);
  4741. return -ENOTBLK;
  4742. }
  4743. /* Just a good old fashioned hole, return */
  4744. if (!create && (em->block_start == EXTENT_MAP_HOLE ||
  4745. test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
  4746. free_extent_map(em);
  4747. /* DIO will do one hole at a time, so just unlock a sector */
  4748. unlock_extent(&BTRFS_I(inode)->io_tree, start,
  4749. start + root->sectorsize - 1, GFP_NOFS);
  4750. return 0;
  4751. }
  4752. /*
  4753. * We don't allocate a new extent in the following cases
  4754. *
  4755. * 1) The inode is marked as NODATACOW. In this case we'll just use the
  4756. * existing extent.
  4757. * 2) The extent is marked as PREALLOC. We're good to go here and can
  4758. * just use the extent.
  4759. *
  4760. */
  4761. if (!create) {
  4762. len = em->len - (start - em->start);
  4763. goto map;
  4764. }
  4765. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
  4766. ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
  4767. em->block_start != EXTENT_MAP_HOLE)) {
  4768. int type;
  4769. int ret;
  4770. u64 block_start;
  4771. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  4772. type = BTRFS_ORDERED_PREALLOC;
  4773. else
  4774. type = BTRFS_ORDERED_NOCOW;
  4775. len = min(len, em->len - (start - em->start));
  4776. block_start = em->block_start + (start - em->start);
  4777. /*
  4778. * we're not going to log anything, but we do need
  4779. * to make sure the current transaction stays open
  4780. * while we look for nocow cross refs
  4781. */
  4782. trans = btrfs_join_transaction(root, 0);
  4783. if (!trans)
  4784. goto must_cow;
  4785. if (can_nocow_odirect(trans, inode, start, len) == 1) {
  4786. ret = btrfs_add_ordered_extent_dio(inode, start,
  4787. block_start, len, len, type);
  4788. btrfs_end_transaction(trans, root);
  4789. if (ret) {
  4790. free_extent_map(em);
  4791. return ret;
  4792. }
  4793. goto unlock;
  4794. }
  4795. btrfs_end_transaction(trans, root);
  4796. }
  4797. must_cow:
  4798. /*
  4799. * this will cow the extent, reset the len in case we changed
  4800. * it above
  4801. */
  4802. len = bh_result->b_size;
  4803. free_extent_map(em);
  4804. em = btrfs_new_extent_direct(inode, start, len);
  4805. if (IS_ERR(em))
  4806. return PTR_ERR(em);
  4807. len = min(len, em->len - (start - em->start));
  4808. unlock:
  4809. clear_extent_bit(&BTRFS_I(inode)->io_tree, start, start + len - 1,
  4810. EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DIRTY, 1,
  4811. 0, NULL, GFP_NOFS);
  4812. map:
  4813. bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
  4814. inode->i_blkbits;
  4815. bh_result->b_size = len;
  4816. bh_result->b_bdev = em->bdev;
  4817. set_buffer_mapped(bh_result);
  4818. if (create && !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  4819. set_buffer_new(bh_result);
  4820. free_extent_map(em);
  4821. return 0;
  4822. }
  4823. struct btrfs_dio_private {
  4824. struct inode *inode;
  4825. u64 logical_offset;
  4826. u64 disk_bytenr;
  4827. u64 bytes;
  4828. u32 *csums;
  4829. void *private;
  4830. };
  4831. static void btrfs_endio_direct_read(struct bio *bio, int err)
  4832. {
  4833. struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
  4834. struct bio_vec *bvec = bio->bi_io_vec;
  4835. struct btrfs_dio_private *dip = bio->bi_private;
  4836. struct inode *inode = dip->inode;
  4837. struct btrfs_root *root = BTRFS_I(inode)->root;
  4838. u64 start;
  4839. u32 *private = dip->csums;
  4840. start = dip->logical_offset;
  4841. do {
  4842. if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
  4843. struct page *page = bvec->bv_page;
  4844. char *kaddr;
  4845. u32 csum = ~(u32)0;
  4846. unsigned long flags;
  4847. local_irq_save(flags);
  4848. kaddr = kmap_atomic(page, KM_IRQ0);
  4849. csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
  4850. csum, bvec->bv_len);
  4851. btrfs_csum_final(csum, (char *)&csum);
  4852. kunmap_atomic(kaddr, KM_IRQ0);
  4853. local_irq_restore(flags);
  4854. flush_dcache_page(bvec->bv_page);
  4855. if (csum != *private) {
  4856. printk(KERN_ERR "btrfs csum failed ino %lu off"
  4857. " %llu csum %u private %u\n",
  4858. inode->i_ino, (unsigned long long)start,
  4859. csum, *private);
  4860. err = -EIO;
  4861. }
  4862. }
  4863. start += bvec->bv_len;
  4864. private++;
  4865. bvec++;
  4866. } while (bvec <= bvec_end);
  4867. unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
  4868. dip->logical_offset + dip->bytes - 1, GFP_NOFS);
  4869. bio->bi_private = dip->private;
  4870. kfree(dip->csums);
  4871. kfree(dip);
  4872. dio_end_io(bio, err);
  4873. }
  4874. static void btrfs_endio_direct_write(struct bio *bio, int err)
  4875. {
  4876. struct btrfs_dio_private *dip = bio->bi_private;
  4877. struct inode *inode = dip->inode;
  4878. struct btrfs_root *root = BTRFS_I(inode)->root;
  4879. struct btrfs_trans_handle *trans;
  4880. struct btrfs_ordered_extent *ordered = NULL;
  4881. struct extent_state *cached_state = NULL;
  4882. int ret;
  4883. if (err)
  4884. goto out_done;
  4885. ret = btrfs_dec_test_ordered_pending(inode, &ordered,
  4886. dip->logical_offset, dip->bytes);
  4887. if (!ret)
  4888. goto out_done;
  4889. BUG_ON(!ordered);
  4890. trans = btrfs_join_transaction(root, 1);
  4891. if (!trans) {
  4892. err = -ENOMEM;
  4893. goto out;
  4894. }
  4895. trans->block_rsv = &root->fs_info->delalloc_block_rsv;
  4896. if (test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) {
  4897. ret = btrfs_ordered_update_i_size(inode, 0, ordered);
  4898. if (!ret)
  4899. ret = btrfs_update_inode(trans, root, inode);
  4900. err = ret;
  4901. goto out;
  4902. }
  4903. lock_extent_bits(&BTRFS_I(inode)->io_tree, ordered->file_offset,
  4904. ordered->file_offset + ordered->len - 1, 0,
  4905. &cached_state, GFP_NOFS);
  4906. if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) {
  4907. ret = btrfs_mark_extent_written(trans, inode,
  4908. ordered->file_offset,
  4909. ordered->file_offset +
  4910. ordered->len);
  4911. if (ret) {
  4912. err = ret;
  4913. goto out_unlock;
  4914. }
  4915. } else {
  4916. ret = insert_reserved_file_extent(trans, inode,
  4917. ordered->file_offset,
  4918. ordered->start,
  4919. ordered->disk_len,
  4920. ordered->len,
  4921. ordered->len,
  4922. 0, 0, 0,
  4923. BTRFS_FILE_EXTENT_REG);
  4924. unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
  4925. ordered->file_offset, ordered->len);
  4926. if (ret) {
  4927. err = ret;
  4928. WARN_ON(1);
  4929. goto out_unlock;
  4930. }
  4931. }
  4932. add_pending_csums(trans, inode, ordered->file_offset, &ordered->list);
  4933. btrfs_ordered_update_i_size(inode, 0, ordered);
  4934. btrfs_update_inode(trans, root, inode);
  4935. out_unlock:
  4936. unlock_extent_cached(&BTRFS_I(inode)->io_tree, ordered->file_offset,
  4937. ordered->file_offset + ordered->len - 1,
  4938. &cached_state, GFP_NOFS);
  4939. out:
  4940. btrfs_delalloc_release_metadata(inode, ordered->len);
  4941. btrfs_end_transaction(trans, root);
  4942. btrfs_put_ordered_extent(ordered);
  4943. btrfs_put_ordered_extent(ordered);
  4944. out_done:
  4945. bio->bi_private = dip->private;
  4946. kfree(dip->csums);
  4947. kfree(dip);
  4948. dio_end_io(bio, err);
  4949. }
  4950. static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
  4951. struct bio *bio, int mirror_num,
  4952. unsigned long bio_flags, u64 offset)
  4953. {
  4954. int ret;
  4955. struct btrfs_root *root = BTRFS_I(inode)->root;
  4956. ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
  4957. BUG_ON(ret);
  4958. return 0;
  4959. }
  4960. static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
  4961. loff_t file_offset)
  4962. {
  4963. struct btrfs_root *root = BTRFS_I(inode)->root;
  4964. struct btrfs_dio_private *dip;
  4965. struct bio_vec *bvec = bio->bi_io_vec;
  4966. u64 start;
  4967. int skip_sum;
  4968. int write = rw & (1 << BIO_RW);
  4969. int ret = 0;
  4970. skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  4971. dip = kmalloc(sizeof(*dip), GFP_NOFS);
  4972. if (!dip) {
  4973. ret = -ENOMEM;
  4974. goto free_ordered;
  4975. }
  4976. dip->csums = NULL;
  4977. if (!skip_sum) {
  4978. dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS);
  4979. if (!dip->csums) {
  4980. ret = -ENOMEM;
  4981. goto free_ordered;
  4982. }
  4983. }
  4984. dip->private = bio->bi_private;
  4985. dip->inode = inode;
  4986. dip->logical_offset = file_offset;
  4987. start = dip->logical_offset;
  4988. dip->bytes = 0;
  4989. do {
  4990. dip->bytes += bvec->bv_len;
  4991. bvec++;
  4992. } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
  4993. dip->disk_bytenr = (u64)bio->bi_sector << 9;
  4994. bio->bi_private = dip;
  4995. if (write)
  4996. bio->bi_end_io = btrfs_endio_direct_write;
  4997. else
  4998. bio->bi_end_io = btrfs_endio_direct_read;
  4999. ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
  5000. if (ret)
  5001. goto out_err;
  5002. if (write && !skip_sum) {
  5003. ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
  5004. inode, rw, bio, 0, 0,
  5005. dip->logical_offset,
  5006. __btrfs_submit_bio_start_direct_io,
  5007. __btrfs_submit_bio_done);
  5008. if (ret)
  5009. goto out_err;
  5010. return;
  5011. } else if (!skip_sum)
  5012. btrfs_lookup_bio_sums_dio(root, inode, bio,
  5013. dip->logical_offset, dip->csums);
  5014. ret = btrfs_map_bio(root, rw, bio, 0, 1);
  5015. if (ret)
  5016. goto out_err;
  5017. return;
  5018. out_err:
  5019. kfree(dip->csums);
  5020. kfree(dip);
  5021. free_ordered:
  5022. /*
  5023. * If this is a write, we need to clean up the reserved space and kill
  5024. * the ordered extent.
  5025. */
  5026. if (write) {
  5027. struct btrfs_ordered_extent *ordered;
  5028. ordered = btrfs_lookup_ordered_extent(inode,
  5029. dip->logical_offset);
  5030. if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
  5031. !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
  5032. btrfs_free_reserved_extent(root, ordered->start,
  5033. ordered->disk_len);
  5034. btrfs_put_ordered_extent(ordered);
  5035. btrfs_put_ordered_extent(ordered);
  5036. }
  5037. bio_endio(bio, ret);
  5038. }
  5039. static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
  5040. const struct iovec *iov, loff_t offset,
  5041. unsigned long nr_segs)
  5042. {
  5043. int seg;
  5044. size_t size;
  5045. unsigned long addr;
  5046. unsigned blocksize_mask = root->sectorsize - 1;
  5047. ssize_t retval = -EINVAL;
  5048. loff_t end = offset;
  5049. if (offset & blocksize_mask)
  5050. goto out;
  5051. /* Check the memory alignment. Blocks cannot straddle pages */
  5052. for (seg = 0; seg < nr_segs; seg++) {
  5053. addr = (unsigned long)iov[seg].iov_base;
  5054. size = iov[seg].iov_len;
  5055. end += size;
  5056. if ((addr & blocksize_mask) || (size & blocksize_mask))
  5057. goto out;
  5058. }
  5059. retval = 0;
  5060. out:
  5061. return retval;
  5062. }
  5063. static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
  5064. const struct iovec *iov, loff_t offset,
  5065. unsigned long nr_segs)
  5066. {
  5067. struct file *file = iocb->ki_filp;
  5068. struct inode *inode = file->f_mapping->host;
  5069. struct btrfs_ordered_extent *ordered;
  5070. struct extent_state *cached_state = NULL;
  5071. u64 lockstart, lockend;
  5072. ssize_t ret;
  5073. int writing = rw & WRITE;
  5074. int write_bits = 0;
  5075. size_t count = iov_length(iov, nr_segs);
  5076. if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
  5077. offset, nr_segs)) {
  5078. return 0;
  5079. }
  5080. lockstart = offset;
  5081. lockend = offset + count - 1;
  5082. if (writing) {
  5083. ret = btrfs_delalloc_reserve_space(inode, count);
  5084. if (ret)
  5085. goto out;
  5086. }
  5087. while (1) {
  5088. lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  5089. 0, &cached_state, GFP_NOFS);
  5090. /*
  5091. * We're concerned with the entire range that we're going to be
  5092. * doing DIO to, so we need to make sure theres no ordered
  5093. * extents in this range.
  5094. */
  5095. ordered = btrfs_lookup_ordered_range(inode, lockstart,
  5096. lockend - lockstart + 1);
  5097. if (!ordered)
  5098. break;
  5099. unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  5100. &cached_state, GFP_NOFS);
  5101. btrfs_start_ordered_extent(inode, ordered, 1);
  5102. btrfs_put_ordered_extent(ordered);
  5103. cond_resched();
  5104. }
  5105. /*
  5106. * we don't use btrfs_set_extent_delalloc because we don't want
  5107. * the dirty or uptodate bits
  5108. */
  5109. if (writing) {
  5110. write_bits = EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING;
  5111. ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  5112. EXTENT_DELALLOC, 0, NULL, &cached_state,
  5113. GFP_NOFS);
  5114. if (ret) {
  5115. clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
  5116. lockend, EXTENT_LOCKED | write_bits,
  5117. 1, 0, &cached_state, GFP_NOFS);
  5118. goto out;
  5119. }
  5120. }
  5121. free_extent_state(cached_state);
  5122. cached_state = NULL;
  5123. ret = __blockdev_direct_IO(rw, iocb, inode,
  5124. BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
  5125. iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
  5126. btrfs_submit_direct, 0);
  5127. if (ret < 0 && ret != -EIOCBQUEUED) {
  5128. clear_extent_bit(&BTRFS_I(inode)->io_tree, offset,
  5129. offset + iov_length(iov, nr_segs) - 1,
  5130. EXTENT_LOCKED | write_bits, 1, 0,
  5131. &cached_state, GFP_NOFS);
  5132. } else if (ret >= 0 && ret < iov_length(iov, nr_segs)) {
  5133. /*
  5134. * We're falling back to buffered, unlock the section we didn't
  5135. * do IO on.
  5136. */
  5137. clear_extent_bit(&BTRFS_I(inode)->io_tree, offset + ret,
  5138. offset + iov_length(iov, nr_segs) - 1,
  5139. EXTENT_LOCKED | write_bits, 1, 0,
  5140. &cached_state, GFP_NOFS);
  5141. }
  5142. out:
  5143. free_extent_state(cached_state);
  5144. return ret;
  5145. }
  5146. static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  5147. __u64 start, __u64 len)
  5148. {
  5149. return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent);
  5150. }
  5151. int btrfs_readpage(struct file *file, struct page *page)
  5152. {
  5153. struct extent_io_tree *tree;
  5154. tree = &BTRFS_I(page->mapping->host)->io_tree;
  5155. return extent_read_full_page(tree, page, btrfs_get_extent);
  5156. }
  5157. static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
  5158. {
  5159. struct extent_io_tree *tree;
  5160. if (current->flags & PF_MEMALLOC) {
  5161. redirty_page_for_writepage(wbc, page);
  5162. unlock_page(page);
  5163. return 0;
  5164. }
  5165. tree = &BTRFS_I(page->mapping->host)->io_tree;
  5166. return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
  5167. }
  5168. int btrfs_writepages(struct address_space *mapping,
  5169. struct writeback_control *wbc)
  5170. {
  5171. struct extent_io_tree *tree;
  5172. tree = &BTRFS_I(mapping->host)->io_tree;
  5173. return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
  5174. }
  5175. static int
  5176. btrfs_readpages(struct file *file, struct address_space *mapping,
  5177. struct list_head *pages, unsigned nr_pages)
  5178. {
  5179. struct extent_io_tree *tree;
  5180. tree = &BTRFS_I(mapping->host)->io_tree;
  5181. return extent_readpages(tree, mapping, pages, nr_pages,
  5182. btrfs_get_extent);
  5183. }
  5184. static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  5185. {
  5186. struct extent_io_tree *tree;
  5187. struct extent_map_tree *map;
  5188. int ret;
  5189. tree = &BTRFS_I(page->mapping->host)->io_tree;
  5190. map = &BTRFS_I(page->mapping->host)->extent_tree;
  5191. ret = try_release_extent_mapping(map, tree, page, gfp_flags);
  5192. if (ret == 1) {
  5193. ClearPagePrivate(page);
  5194. set_page_private(page, 0);
  5195. page_cache_release(page);
  5196. }
  5197. return ret;
  5198. }
  5199. static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  5200. {
  5201. if (PageWriteback(page) || PageDirty(page))
  5202. return 0;
  5203. return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
  5204. }
  5205. static void btrfs_invalidatepage(struct page *page, unsigned long offset)
  5206. {
  5207. struct extent_io_tree *tree;
  5208. struct btrfs_ordered_extent *ordered;
  5209. struct extent_state *cached_state = NULL;
  5210. u64 page_start = page_offset(page);
  5211. u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
  5212. /*
  5213. * we have the page locked, so new writeback can't start,
  5214. * and the dirty bit won't be cleared while we are here.
  5215. *
  5216. * Wait for IO on this page so that we can safely clear
  5217. * the PagePrivate2 bit and do ordered accounting
  5218. */
  5219. wait_on_page_writeback(page);
  5220. tree = &BTRFS_I(page->mapping->host)->io_tree;
  5221. if (offset) {
  5222. btrfs_releasepage(page, GFP_NOFS);
  5223. return;
  5224. }
  5225. lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
  5226. GFP_NOFS);
  5227. ordered = btrfs_lookup_ordered_extent(page->mapping->host,
  5228. page_offset(page));
  5229. if (ordered) {
  5230. /*
  5231. * IO on this page will never be started, so we need
  5232. * to account for any ordered extents now
  5233. */
  5234. clear_extent_bit(tree, page_start, page_end,
  5235. EXTENT_DIRTY | EXTENT_DELALLOC |
  5236. EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
  5237. &cached_state, GFP_NOFS);
  5238. /*
  5239. * whoever cleared the private bit is responsible
  5240. * for the finish_ordered_io
  5241. */
  5242. if (TestClearPagePrivate2(page)) {
  5243. btrfs_finish_ordered_io(page->mapping->host,
  5244. page_start, page_end);
  5245. }
  5246. btrfs_put_ordered_extent(ordered);
  5247. cached_state = NULL;
  5248. lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
  5249. GFP_NOFS);
  5250. }
  5251. clear_extent_bit(tree, page_start, page_end,
  5252. EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
  5253. EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS);
  5254. __btrfs_releasepage(page, GFP_NOFS);
  5255. ClearPageChecked(page);
  5256. if (PagePrivate(page)) {
  5257. ClearPagePrivate(page);
  5258. set_page_private(page, 0);
  5259. page_cache_release(page);
  5260. }
  5261. }
  5262. /*
  5263. * btrfs_page_mkwrite() is not allowed to change the file size as it gets
  5264. * called from a page fault handler when a page is first dirtied. Hence we must
  5265. * be careful to check for EOF conditions here. We set the page up correctly
  5266. * for a written page which means we get ENOSPC checking when writing into
  5267. * holes and correct delalloc and unwritten extent mapping on filesystems that
  5268. * support these features.
  5269. *
  5270. * We are not allowed to take the i_mutex here so we have to play games to
  5271. * protect against truncate races as the page could now be beyond EOF. Because
  5272. * vmtruncate() writes the inode size before removing pages, once we have the
  5273. * page lock we can determine safely if the page is beyond EOF. If it is not
  5274. * beyond EOF, then the page is guaranteed safe against truncation until we
  5275. * unlock the page.
  5276. */
  5277. int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  5278. {
  5279. struct page *page = vmf->page;
  5280. struct inode *inode = fdentry(vma->vm_file)->d_inode;
  5281. struct btrfs_root *root = BTRFS_I(inode)->root;
  5282. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  5283. struct btrfs_ordered_extent *ordered;
  5284. struct extent_state *cached_state = NULL;
  5285. char *kaddr;
  5286. unsigned long zero_start;
  5287. loff_t size;
  5288. int ret;
  5289. u64 page_start;
  5290. u64 page_end;
  5291. ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
  5292. if (ret) {
  5293. if (ret == -ENOMEM)
  5294. ret = VM_FAULT_OOM;
  5295. else /* -ENOSPC, -EIO, etc */
  5296. ret = VM_FAULT_SIGBUS;
  5297. goto out;
  5298. }
  5299. ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
  5300. again:
  5301. lock_page(page);
  5302. size = i_size_read(inode);
  5303. page_start = page_offset(page);
  5304. page_end = page_start + PAGE_CACHE_SIZE - 1;
  5305. if ((page->mapping != inode->i_mapping) ||
  5306. (page_start >= size)) {
  5307. /* page got truncated out from underneath us */
  5308. goto out_unlock;
  5309. }
  5310. wait_on_page_writeback(page);
  5311. lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
  5312. GFP_NOFS);
  5313. set_page_extent_mapped(page);
  5314. /*
  5315. * we can't set the delalloc bits if there are pending ordered
  5316. * extents. Drop our locks and wait for them to finish
  5317. */
  5318. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  5319. if (ordered) {
  5320. unlock_extent_cached(io_tree, page_start, page_end,
  5321. &cached_state, GFP_NOFS);
  5322. unlock_page(page);
  5323. btrfs_start_ordered_extent(inode, ordered, 1);
  5324. btrfs_put_ordered_extent(ordered);
  5325. goto again;
  5326. }
  5327. /*
  5328. * XXX - page_mkwrite gets called every time the page is dirtied, even
  5329. * if it was already dirty, so for space accounting reasons we need to
  5330. * clear any delalloc bits for the range we are fixing to save. There
  5331. * is probably a better way to do this, but for now keep consistent with
  5332. * prepare_pages in the normal write path.
  5333. */
  5334. clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
  5335. EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
  5336. 0, 0, &cached_state, GFP_NOFS);
  5337. ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
  5338. &cached_state);
  5339. if (ret) {
  5340. unlock_extent_cached(io_tree, page_start, page_end,
  5341. &cached_state, GFP_NOFS);
  5342. ret = VM_FAULT_SIGBUS;
  5343. goto out_unlock;
  5344. }
  5345. ret = 0;
  5346. /* page is wholly or partially inside EOF */
  5347. if (page_start + PAGE_CACHE_SIZE > size)
  5348. zero_start = size & ~PAGE_CACHE_MASK;
  5349. else
  5350. zero_start = PAGE_CACHE_SIZE;
  5351. if (zero_start != PAGE_CACHE_SIZE) {
  5352. kaddr = kmap(page);
  5353. memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
  5354. flush_dcache_page(page);
  5355. kunmap(page);
  5356. }
  5357. ClearPageChecked(page);
  5358. set_page_dirty(page);
  5359. SetPageUptodate(page);
  5360. BTRFS_I(inode)->last_trans = root->fs_info->generation;
  5361. BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
  5362. unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
  5363. out_unlock:
  5364. if (!ret)
  5365. return VM_FAULT_LOCKED;
  5366. unlock_page(page);
  5367. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  5368. out:
  5369. return ret;
  5370. }
  5371. static void btrfs_truncate(struct inode *inode)
  5372. {
  5373. struct btrfs_root *root = BTRFS_I(inode)->root;
  5374. int ret;
  5375. struct btrfs_trans_handle *trans;
  5376. unsigned long nr;
  5377. u64 mask = root->sectorsize - 1;
  5378. if (!S_ISREG(inode->i_mode)) {
  5379. WARN_ON(1);
  5380. return;
  5381. }
  5382. ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
  5383. if (ret)
  5384. return;
  5385. btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
  5386. btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
  5387. trans = btrfs_start_transaction(root, 0);
  5388. BUG_ON(IS_ERR(trans));
  5389. btrfs_set_trans_block_group(trans, inode);
  5390. trans->block_rsv = root->orphan_block_rsv;
  5391. /*
  5392. * setattr is responsible for setting the ordered_data_close flag,
  5393. * but that is only tested during the last file release. That
  5394. * could happen well after the next commit, leaving a great big
  5395. * window where new writes may get lost if someone chooses to write
  5396. * to this file after truncating to zero
  5397. *
  5398. * The inode doesn't have any dirty data here, and so if we commit
  5399. * this is a noop. If someone immediately starts writing to the inode
  5400. * it is very likely we'll catch some of their writes in this
  5401. * transaction, and the commit will find this file on the ordered
  5402. * data list with good things to send down.
  5403. *
  5404. * This is a best effort solution, there is still a window where
  5405. * using truncate to replace the contents of the file will
  5406. * end up with a zero length file after a crash.
  5407. */
  5408. if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
  5409. btrfs_add_ordered_operation(trans, root, inode);
  5410. while (1) {
  5411. if (!trans) {
  5412. trans = btrfs_start_transaction(root, 0);
  5413. BUG_ON(IS_ERR(trans));
  5414. btrfs_set_trans_block_group(trans, inode);
  5415. trans->block_rsv = root->orphan_block_rsv;
  5416. }
  5417. ret = btrfs_block_rsv_check(trans, root,
  5418. root->orphan_block_rsv, 0, 5);
  5419. if (ret) {
  5420. BUG_ON(ret != -EAGAIN);
  5421. ret = btrfs_commit_transaction(trans, root);
  5422. BUG_ON(ret);
  5423. trans = NULL;
  5424. continue;
  5425. }
  5426. ret = btrfs_truncate_inode_items(trans, root, inode,
  5427. inode->i_size,
  5428. BTRFS_EXTENT_DATA_KEY);
  5429. if (ret != -EAGAIN)
  5430. break;
  5431. ret = btrfs_update_inode(trans, root, inode);
  5432. BUG_ON(ret);
  5433. nr = trans->blocks_used;
  5434. btrfs_end_transaction(trans, root);
  5435. trans = NULL;
  5436. btrfs_btree_balance_dirty(root, nr);
  5437. }
  5438. if (ret == 0 && inode->i_nlink > 0) {
  5439. ret = btrfs_orphan_del(trans, inode);
  5440. BUG_ON(ret);
  5441. }
  5442. ret = btrfs_update_inode(trans, root, inode);
  5443. BUG_ON(ret);
  5444. nr = trans->blocks_used;
  5445. ret = btrfs_end_transaction_throttle(trans, root);
  5446. BUG_ON(ret);
  5447. btrfs_btree_balance_dirty(root, nr);
  5448. }
  5449. /*
  5450. * create a new subvolume directory/inode (helper for the ioctl).
  5451. */
  5452. int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
  5453. struct btrfs_root *new_root,
  5454. u64 new_dirid, u64 alloc_hint)
  5455. {
  5456. struct inode *inode;
  5457. int err;
  5458. u64 index = 0;
  5459. inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
  5460. new_dirid, alloc_hint, S_IFDIR | 0700, &index);
  5461. if (IS_ERR(inode))
  5462. return PTR_ERR(inode);
  5463. inode->i_op = &btrfs_dir_inode_operations;
  5464. inode->i_fop = &btrfs_dir_file_operations;
  5465. inode->i_nlink = 1;
  5466. btrfs_i_size_write(inode, 0);
  5467. err = btrfs_update_inode(trans, new_root, inode);
  5468. BUG_ON(err);
  5469. iput(inode);
  5470. return 0;
  5471. }
  5472. /* helper function for file defrag and space balancing. This
  5473. * forces readahead on a given range of bytes in an inode
  5474. */
  5475. unsigned long btrfs_force_ra(struct address_space *mapping,
  5476. struct file_ra_state *ra, struct file *file,
  5477. pgoff_t offset, pgoff_t last_index)
  5478. {
  5479. pgoff_t req_size = last_index - offset + 1;
  5480. page_cache_sync_readahead(mapping, ra, file, offset, req_size);
  5481. return offset + req_size;
  5482. }
  5483. struct inode *btrfs_alloc_inode(struct super_block *sb)
  5484. {
  5485. struct btrfs_inode *ei;
  5486. struct inode *inode;
  5487. ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
  5488. if (!ei)
  5489. return NULL;
  5490. ei->root = NULL;
  5491. ei->space_info = NULL;
  5492. ei->generation = 0;
  5493. ei->sequence = 0;
  5494. ei->last_trans = 0;
  5495. ei->last_sub_trans = 0;
  5496. ei->logged_trans = 0;
  5497. ei->delalloc_bytes = 0;
  5498. ei->reserved_bytes = 0;
  5499. ei->disk_i_size = 0;
  5500. ei->flags = 0;
  5501. ei->index_cnt = (u64)-1;
  5502. ei->last_unlink_trans = 0;
  5503. spin_lock_init(&ei->accounting_lock);
  5504. atomic_set(&ei->outstanding_extents, 0);
  5505. ei->reserved_extents = 0;
  5506. ei->ordered_data_close = 0;
  5507. ei->orphan_meta_reserved = 0;
  5508. ei->dummy_inode = 0;
  5509. ei->force_compress = 0;
  5510. inode = &ei->vfs_inode;
  5511. extent_map_tree_init(&ei->extent_tree, GFP_NOFS);
  5512. extent_io_tree_init(&ei->io_tree, &inode->i_data, GFP_NOFS);
  5513. extent_io_tree_init(&ei->io_failure_tree, &inode->i_data, GFP_NOFS);
  5514. mutex_init(&ei->log_mutex);
  5515. btrfs_ordered_inode_tree_init(&ei->ordered_tree);
  5516. INIT_LIST_HEAD(&ei->i_orphan);
  5517. INIT_LIST_HEAD(&ei->delalloc_inodes);
  5518. INIT_LIST_HEAD(&ei->ordered_operations);
  5519. RB_CLEAR_NODE(&ei->rb_node);
  5520. return inode;
  5521. }
  5522. void btrfs_destroy_inode(struct inode *inode)
  5523. {
  5524. struct btrfs_ordered_extent *ordered;
  5525. struct btrfs_root *root = BTRFS_I(inode)->root;
  5526. WARN_ON(!list_empty(&inode->i_dentry));
  5527. WARN_ON(inode->i_data.nrpages);
  5528. WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents));
  5529. WARN_ON(BTRFS_I(inode)->reserved_extents);
  5530. /*
  5531. * This can happen where we create an inode, but somebody else also
  5532. * created the same inode and we need to destroy the one we already
  5533. * created.
  5534. */
  5535. if (!root)
  5536. goto free;
  5537. /*
  5538. * Make sure we're properly removed from the ordered operation
  5539. * lists.
  5540. */
  5541. smp_mb();
  5542. if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
  5543. spin_lock(&root->fs_info->ordered_extent_lock);
  5544. list_del_init(&BTRFS_I(inode)->ordered_operations);
  5545. spin_unlock(&root->fs_info->ordered_extent_lock);
  5546. }
  5547. spin_lock(&root->orphan_lock);
  5548. if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
  5549. printk(KERN_INFO "BTRFS: inode %lu still on the orphan list\n",
  5550. inode->i_ino);
  5551. list_del_init(&BTRFS_I(inode)->i_orphan);
  5552. }
  5553. spin_unlock(&root->orphan_lock);
  5554. while (1) {
  5555. ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
  5556. if (!ordered)
  5557. break;
  5558. else {
  5559. printk(KERN_ERR "btrfs found ordered "
  5560. "extent %llu %llu on inode cleanup\n",
  5561. (unsigned long long)ordered->file_offset,
  5562. (unsigned long long)ordered->len);
  5563. btrfs_remove_ordered_extent(inode, ordered);
  5564. btrfs_put_ordered_extent(ordered);
  5565. btrfs_put_ordered_extent(ordered);
  5566. }
  5567. }
  5568. inode_tree_del(inode);
  5569. btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
  5570. free:
  5571. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  5572. }
  5573. void btrfs_drop_inode(struct inode *inode)
  5574. {
  5575. struct btrfs_root *root = BTRFS_I(inode)->root;
  5576. if (inode->i_nlink > 0 && btrfs_root_refs(&root->root_item) == 0)
  5577. generic_delete_inode(inode);
  5578. else
  5579. generic_drop_inode(inode);
  5580. }
  5581. static void init_once(void *foo)
  5582. {
  5583. struct btrfs_inode *ei = (struct btrfs_inode *) foo;
  5584. inode_init_once(&ei->vfs_inode);
  5585. }
  5586. void btrfs_destroy_cachep(void)
  5587. {
  5588. if (btrfs_inode_cachep)
  5589. kmem_cache_destroy(btrfs_inode_cachep);
  5590. if (btrfs_trans_handle_cachep)
  5591. kmem_cache_destroy(btrfs_trans_handle_cachep);
  5592. if (btrfs_transaction_cachep)
  5593. kmem_cache_destroy(btrfs_transaction_cachep);
  5594. if (btrfs_path_cachep)
  5595. kmem_cache_destroy(btrfs_path_cachep);
  5596. }
  5597. int btrfs_init_cachep(void)
  5598. {
  5599. btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
  5600. sizeof(struct btrfs_inode), 0,
  5601. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
  5602. if (!btrfs_inode_cachep)
  5603. goto fail;
  5604. btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
  5605. sizeof(struct btrfs_trans_handle), 0,
  5606. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  5607. if (!btrfs_trans_handle_cachep)
  5608. goto fail;
  5609. btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
  5610. sizeof(struct btrfs_transaction), 0,
  5611. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  5612. if (!btrfs_transaction_cachep)
  5613. goto fail;
  5614. btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
  5615. sizeof(struct btrfs_path), 0,
  5616. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  5617. if (!btrfs_path_cachep)
  5618. goto fail;
  5619. return 0;
  5620. fail:
  5621. btrfs_destroy_cachep();
  5622. return -ENOMEM;
  5623. }
  5624. static int btrfs_getattr(struct vfsmount *mnt,
  5625. struct dentry *dentry, struct kstat *stat)
  5626. {
  5627. struct inode *inode = dentry->d_inode;
  5628. generic_fillattr(inode, stat);
  5629. stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
  5630. stat->blksize = PAGE_CACHE_SIZE;
  5631. stat->blocks = (inode_get_bytes(inode) +
  5632. BTRFS_I(inode)->delalloc_bytes) >> 9;
  5633. return 0;
  5634. }
  5635. static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  5636. struct inode *new_dir, struct dentry *new_dentry)
  5637. {
  5638. struct btrfs_trans_handle *trans;
  5639. struct btrfs_root *root = BTRFS_I(old_dir)->root;
  5640. struct btrfs_root *dest = BTRFS_I(new_dir)->root;
  5641. struct inode *new_inode = new_dentry->d_inode;
  5642. struct inode *old_inode = old_dentry->d_inode;
  5643. struct timespec ctime = CURRENT_TIME;
  5644. u64 index = 0;
  5645. u64 root_objectid;
  5646. int ret;
  5647. if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
  5648. return -EPERM;
  5649. /* we only allow rename subvolume link between subvolumes */
  5650. if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
  5651. return -EXDEV;
  5652. if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
  5653. (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID))
  5654. return -ENOTEMPTY;
  5655. if (S_ISDIR(old_inode->i_mode) && new_inode &&
  5656. new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
  5657. return -ENOTEMPTY;
  5658. /*
  5659. * we're using rename to replace one file with another.
  5660. * and the replacement file is large. Start IO on it now so
  5661. * we don't add too much work to the end of the transaction
  5662. */
  5663. if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
  5664. old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
  5665. filemap_flush(old_inode->i_mapping);
  5666. /* close the racy window with snapshot create/destroy ioctl */
  5667. if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
  5668. down_read(&root->fs_info->subvol_sem);
  5669. /*
  5670. * We want to reserve the absolute worst case amount of items. So if
  5671. * both inodes are subvols and we need to unlink them then that would
  5672. * require 4 item modifications, but if they are both normal inodes it
  5673. * would require 5 item modifications, so we'll assume their normal
  5674. * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
  5675. * should cover the worst case number of items we'll modify.
  5676. */
  5677. trans = btrfs_start_transaction(root, 20);
  5678. if (IS_ERR(trans))
  5679. return PTR_ERR(trans);
  5680. btrfs_set_trans_block_group(trans, new_dir);
  5681. if (dest != root)
  5682. btrfs_record_root_in_trans(trans, dest);
  5683. ret = btrfs_set_inode_index(new_dir, &index);
  5684. if (ret)
  5685. goto out_fail;
  5686. if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  5687. /* force full log commit if subvolume involved. */
  5688. root->fs_info->last_trans_log_full_commit = trans->transid;
  5689. } else {
  5690. ret = btrfs_insert_inode_ref(trans, dest,
  5691. new_dentry->d_name.name,
  5692. new_dentry->d_name.len,
  5693. old_inode->i_ino,
  5694. new_dir->i_ino, index);
  5695. if (ret)
  5696. goto out_fail;
  5697. /*
  5698. * this is an ugly little race, but the rename is required
  5699. * to make sure that if we crash, the inode is either at the
  5700. * old name or the new one. pinning the log transaction lets
  5701. * us make sure we don't allow a log commit to come in after
  5702. * we unlink the name but before we add the new name back in.
  5703. */
  5704. btrfs_pin_log_trans(root);
  5705. }
  5706. /*
  5707. * make sure the inode gets flushed if it is replacing
  5708. * something.
  5709. */
  5710. if (new_inode && new_inode->i_size &&
  5711. old_inode && S_ISREG(old_inode->i_mode)) {
  5712. btrfs_add_ordered_operation(trans, root, old_inode);
  5713. }
  5714. old_dir->i_ctime = old_dir->i_mtime = ctime;
  5715. new_dir->i_ctime = new_dir->i_mtime = ctime;
  5716. old_inode->i_ctime = ctime;
  5717. if (old_dentry->d_parent != new_dentry->d_parent)
  5718. btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
  5719. if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  5720. root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
  5721. ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
  5722. old_dentry->d_name.name,
  5723. old_dentry->d_name.len);
  5724. } else {
  5725. btrfs_inc_nlink(old_dentry->d_inode);
  5726. ret = btrfs_unlink_inode(trans, root, old_dir,
  5727. old_dentry->d_inode,
  5728. old_dentry->d_name.name,
  5729. old_dentry->d_name.len);
  5730. }
  5731. BUG_ON(ret);
  5732. if (new_inode) {
  5733. new_inode->i_ctime = CURRENT_TIME;
  5734. if (unlikely(new_inode->i_ino ==
  5735. BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
  5736. root_objectid = BTRFS_I(new_inode)->location.objectid;
  5737. ret = btrfs_unlink_subvol(trans, dest, new_dir,
  5738. root_objectid,
  5739. new_dentry->d_name.name,
  5740. new_dentry->d_name.len);
  5741. BUG_ON(new_inode->i_nlink == 0);
  5742. } else {
  5743. ret = btrfs_unlink_inode(trans, dest, new_dir,
  5744. new_dentry->d_inode,
  5745. new_dentry->d_name.name,
  5746. new_dentry->d_name.len);
  5747. }
  5748. BUG_ON(ret);
  5749. if (new_inode->i_nlink == 0) {
  5750. ret = btrfs_orphan_add(trans, new_dentry->d_inode);
  5751. BUG_ON(ret);
  5752. }
  5753. }
  5754. ret = btrfs_add_link(trans, new_dir, old_inode,
  5755. new_dentry->d_name.name,
  5756. new_dentry->d_name.len, 0, index);
  5757. BUG_ON(ret);
  5758. if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
  5759. btrfs_log_new_name(trans, old_inode, old_dir,
  5760. new_dentry->d_parent);
  5761. btrfs_end_log_trans(root);
  5762. }
  5763. out_fail:
  5764. btrfs_end_transaction_throttle(trans, root);
  5765. if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
  5766. up_read(&root->fs_info->subvol_sem);
  5767. return ret;
  5768. }
  5769. /*
  5770. * some fairly slow code that needs optimization. This walks the list
  5771. * of all the inodes with pending delalloc and forces them to disk.
  5772. */
  5773. int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
  5774. {
  5775. struct list_head *head = &root->fs_info->delalloc_inodes;
  5776. struct btrfs_inode *binode;
  5777. struct inode *inode;
  5778. if (root->fs_info->sb->s_flags & MS_RDONLY)
  5779. return -EROFS;
  5780. spin_lock(&root->fs_info->delalloc_lock);
  5781. while (!list_empty(head)) {
  5782. binode = list_entry(head->next, struct btrfs_inode,
  5783. delalloc_inodes);
  5784. inode = igrab(&binode->vfs_inode);
  5785. if (!inode)
  5786. list_del_init(&binode->delalloc_inodes);
  5787. spin_unlock(&root->fs_info->delalloc_lock);
  5788. if (inode) {
  5789. filemap_flush(inode->i_mapping);
  5790. if (delay_iput)
  5791. btrfs_add_delayed_iput(inode);
  5792. else
  5793. iput(inode);
  5794. }
  5795. cond_resched();
  5796. spin_lock(&root->fs_info->delalloc_lock);
  5797. }
  5798. spin_unlock(&root->fs_info->delalloc_lock);
  5799. /* the filemap_flush will queue IO into the worker threads, but
  5800. * we have to make sure the IO is actually started and that
  5801. * ordered extents get created before we return
  5802. */
  5803. atomic_inc(&root->fs_info->async_submit_draining);
  5804. while (atomic_read(&root->fs_info->nr_async_submits) ||
  5805. atomic_read(&root->fs_info->async_delalloc_pages)) {
  5806. wait_event(root->fs_info->async_submit_wait,
  5807. (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
  5808. atomic_read(&root->fs_info->async_delalloc_pages) == 0));
  5809. }
  5810. atomic_dec(&root->fs_info->async_submit_draining);
  5811. return 0;
  5812. }
  5813. int btrfs_start_one_delalloc_inode(struct btrfs_root *root, int delay_iput)
  5814. {
  5815. struct btrfs_inode *binode;
  5816. struct inode *inode = NULL;
  5817. spin_lock(&root->fs_info->delalloc_lock);
  5818. while (!list_empty(&root->fs_info->delalloc_inodes)) {
  5819. binode = list_entry(root->fs_info->delalloc_inodes.next,
  5820. struct btrfs_inode, delalloc_inodes);
  5821. inode = igrab(&binode->vfs_inode);
  5822. if (inode) {
  5823. list_move_tail(&binode->delalloc_inodes,
  5824. &root->fs_info->delalloc_inodes);
  5825. break;
  5826. }
  5827. list_del_init(&binode->delalloc_inodes);
  5828. cond_resched_lock(&root->fs_info->delalloc_lock);
  5829. }
  5830. spin_unlock(&root->fs_info->delalloc_lock);
  5831. if (inode) {
  5832. write_inode_now(inode, 0);
  5833. if (delay_iput)
  5834. btrfs_add_delayed_iput(inode);
  5835. else
  5836. iput(inode);
  5837. return 1;
  5838. }
  5839. return 0;
  5840. }
  5841. static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
  5842. const char *symname)
  5843. {
  5844. struct btrfs_trans_handle *trans;
  5845. struct btrfs_root *root = BTRFS_I(dir)->root;
  5846. struct btrfs_path *path;
  5847. struct btrfs_key key;
  5848. struct inode *inode = NULL;
  5849. int err;
  5850. int drop_inode = 0;
  5851. u64 objectid;
  5852. u64 index = 0 ;
  5853. int name_len;
  5854. int datasize;
  5855. unsigned long ptr;
  5856. struct btrfs_file_extent_item *ei;
  5857. struct extent_buffer *leaf;
  5858. unsigned long nr = 0;
  5859. name_len = strlen(symname) + 1;
  5860. if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
  5861. return -ENAMETOOLONG;
  5862. err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
  5863. if (err)
  5864. return err;
  5865. /*
  5866. * 2 items for inode item and ref
  5867. * 2 items for dir items
  5868. * 1 item for xattr if selinux is on
  5869. */
  5870. trans = btrfs_start_transaction(root, 5);
  5871. if (IS_ERR(trans))
  5872. return PTR_ERR(trans);
  5873. btrfs_set_trans_block_group(trans, dir);
  5874. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  5875. dentry->d_name.len,
  5876. dentry->d_parent->d_inode->i_ino, objectid,
  5877. BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
  5878. &index);
  5879. err = PTR_ERR(inode);
  5880. if (IS_ERR(inode))
  5881. goto out_unlock;
  5882. err = btrfs_init_inode_security(trans, inode, dir);
  5883. if (err) {
  5884. drop_inode = 1;
  5885. goto out_unlock;
  5886. }
  5887. btrfs_set_trans_block_group(trans, inode);
  5888. err = btrfs_add_nondir(trans, dentry, inode, 0, index);
  5889. if (err)
  5890. drop_inode = 1;
  5891. else {
  5892. inode->i_mapping->a_ops = &btrfs_aops;
  5893. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  5894. inode->i_fop = &btrfs_file_operations;
  5895. inode->i_op = &btrfs_file_inode_operations;
  5896. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  5897. }
  5898. btrfs_update_inode_block_group(trans, inode);
  5899. btrfs_update_inode_block_group(trans, dir);
  5900. if (drop_inode)
  5901. goto out_unlock;
  5902. path = btrfs_alloc_path();
  5903. BUG_ON(!path);
  5904. key.objectid = inode->i_ino;
  5905. key.offset = 0;
  5906. btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
  5907. datasize = btrfs_file_extent_calc_inline_size(name_len);
  5908. err = btrfs_insert_empty_item(trans, root, path, &key,
  5909. datasize);
  5910. if (err) {
  5911. drop_inode = 1;
  5912. goto out_unlock;
  5913. }
  5914. leaf = path->nodes[0];
  5915. ei = btrfs_item_ptr(leaf, path->slots[0],
  5916. struct btrfs_file_extent_item);
  5917. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  5918. btrfs_set_file_extent_type(leaf, ei,
  5919. BTRFS_FILE_EXTENT_INLINE);
  5920. btrfs_set_file_extent_encryption(leaf, ei, 0);
  5921. btrfs_set_file_extent_compression(leaf, ei, 0);
  5922. btrfs_set_file_extent_other_encoding(leaf, ei, 0);
  5923. btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
  5924. ptr = btrfs_file_extent_inline_start(ei);
  5925. write_extent_buffer(leaf, symname, ptr, name_len);
  5926. btrfs_mark_buffer_dirty(leaf);
  5927. btrfs_free_path(path);
  5928. inode->i_op = &btrfs_symlink_inode_operations;
  5929. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  5930. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  5931. inode_set_bytes(inode, name_len);
  5932. btrfs_i_size_write(inode, name_len - 1);
  5933. err = btrfs_update_inode(trans, root, inode);
  5934. if (err)
  5935. drop_inode = 1;
  5936. out_unlock:
  5937. nr = trans->blocks_used;
  5938. btrfs_end_transaction_throttle(trans, root);
  5939. if (drop_inode) {
  5940. inode_dec_link_count(inode);
  5941. iput(inode);
  5942. }
  5943. btrfs_btree_balance_dirty(root, nr);
  5944. return err;
  5945. }
  5946. int btrfs_prealloc_file_range(struct inode *inode, int mode,
  5947. u64 start, u64 num_bytes, u64 min_size,
  5948. loff_t actual_len, u64 *alloc_hint)
  5949. {
  5950. struct btrfs_trans_handle *trans;
  5951. struct btrfs_root *root = BTRFS_I(inode)->root;
  5952. struct btrfs_key ins;
  5953. u64 cur_offset = start;
  5954. int ret = 0;
  5955. while (num_bytes > 0) {
  5956. trans = btrfs_start_transaction(root, 3);
  5957. if (IS_ERR(trans)) {
  5958. ret = PTR_ERR(trans);
  5959. break;
  5960. }
  5961. ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
  5962. 0, *alloc_hint, (u64)-1, &ins, 1);
  5963. if (ret) {
  5964. btrfs_end_transaction(trans, root);
  5965. break;
  5966. }
  5967. ret = insert_reserved_file_extent(trans, inode,
  5968. cur_offset, ins.objectid,
  5969. ins.offset, ins.offset,
  5970. ins.offset, 0, 0, 0,
  5971. BTRFS_FILE_EXTENT_PREALLOC);
  5972. BUG_ON(ret);
  5973. btrfs_drop_extent_cache(inode, cur_offset,
  5974. cur_offset + ins.offset -1, 0);
  5975. num_bytes -= ins.offset;
  5976. cur_offset += ins.offset;
  5977. *alloc_hint = ins.objectid + ins.offset;
  5978. inode->i_ctime = CURRENT_TIME;
  5979. BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
  5980. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  5981. (actual_len > inode->i_size) &&
  5982. (cur_offset > inode->i_size)) {
  5983. if (cur_offset > actual_len)
  5984. i_size_write(inode, actual_len);
  5985. else
  5986. i_size_write(inode, cur_offset);
  5987. i_size_write(inode, cur_offset);
  5988. btrfs_ordered_update_i_size(inode, cur_offset, NULL);
  5989. }
  5990. ret = btrfs_update_inode(trans, root, inode);
  5991. BUG_ON(ret);
  5992. btrfs_end_transaction(trans, root);
  5993. }
  5994. return ret;
  5995. }
  5996. static long btrfs_fallocate(struct inode *inode, int mode,
  5997. loff_t offset, loff_t len)
  5998. {
  5999. struct extent_state *cached_state = NULL;
  6000. u64 cur_offset;
  6001. u64 last_byte;
  6002. u64 alloc_start;
  6003. u64 alloc_end;
  6004. u64 alloc_hint = 0;
  6005. u64 locked_end;
  6006. u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
  6007. struct extent_map *em;
  6008. int ret;
  6009. alloc_start = offset & ~mask;
  6010. alloc_end = (offset + len + mask) & ~mask;
  6011. /*
  6012. * wait for ordered IO before we have any locks. We'll loop again
  6013. * below with the locks held.
  6014. */
  6015. btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
  6016. mutex_lock(&inode->i_mutex);
  6017. if (alloc_start > inode->i_size) {
  6018. ret = btrfs_cont_expand(inode, alloc_start);
  6019. if (ret)
  6020. goto out;
  6021. }
  6022. ret = btrfs_check_data_free_space(inode, alloc_end - alloc_start);
  6023. if (ret)
  6024. goto out;
  6025. locked_end = alloc_end - 1;
  6026. while (1) {
  6027. struct btrfs_ordered_extent *ordered;
  6028. /* the extent lock is ordered inside the running
  6029. * transaction
  6030. */
  6031. lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
  6032. locked_end, 0, &cached_state, GFP_NOFS);
  6033. ordered = btrfs_lookup_first_ordered_extent(inode,
  6034. alloc_end - 1);
  6035. if (ordered &&
  6036. ordered->file_offset + ordered->len > alloc_start &&
  6037. ordered->file_offset < alloc_end) {
  6038. btrfs_put_ordered_extent(ordered);
  6039. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  6040. alloc_start, locked_end,
  6041. &cached_state, GFP_NOFS);
  6042. /*
  6043. * we can't wait on the range with the transaction
  6044. * running or with the extent lock held
  6045. */
  6046. btrfs_wait_ordered_range(inode, alloc_start,
  6047. alloc_end - alloc_start);
  6048. } else {
  6049. if (ordered)
  6050. btrfs_put_ordered_extent(ordered);
  6051. break;
  6052. }
  6053. }
  6054. cur_offset = alloc_start;
  6055. while (1) {
  6056. em = btrfs_get_extent(inode, NULL, 0, cur_offset,
  6057. alloc_end - cur_offset, 0);
  6058. BUG_ON(IS_ERR(em) || !em);
  6059. last_byte = min(extent_map_end(em), alloc_end);
  6060. last_byte = (last_byte + mask) & ~mask;
  6061. if (em->block_start == EXTENT_MAP_HOLE ||
  6062. (cur_offset >= inode->i_size &&
  6063. !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
  6064. ret = btrfs_prealloc_file_range(inode, mode, cur_offset,
  6065. last_byte - cur_offset,
  6066. 1 << inode->i_blkbits,
  6067. offset + len,
  6068. &alloc_hint);
  6069. if (ret < 0) {
  6070. free_extent_map(em);
  6071. break;
  6072. }
  6073. }
  6074. free_extent_map(em);
  6075. cur_offset = last_byte;
  6076. if (cur_offset >= alloc_end) {
  6077. ret = 0;
  6078. break;
  6079. }
  6080. }
  6081. unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
  6082. &cached_state, GFP_NOFS);
  6083. btrfs_free_reserved_data_space(inode, alloc_end - alloc_start);
  6084. out:
  6085. mutex_unlock(&inode->i_mutex);
  6086. return ret;
  6087. }
  6088. static int btrfs_set_page_dirty(struct page *page)
  6089. {
  6090. return __set_page_dirty_nobuffers(page);
  6091. }
  6092. static int btrfs_permission(struct inode *inode, int mask)
  6093. {
  6094. if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
  6095. return -EACCES;
  6096. return generic_permission(inode, mask, btrfs_check_acl);
  6097. }
  6098. static const struct inode_operations btrfs_dir_inode_operations = {
  6099. .getattr = btrfs_getattr,
  6100. .lookup = btrfs_lookup,
  6101. .create = btrfs_create,
  6102. .unlink = btrfs_unlink,
  6103. .link = btrfs_link,
  6104. .mkdir = btrfs_mkdir,
  6105. .rmdir = btrfs_rmdir,
  6106. .rename = btrfs_rename,
  6107. .symlink = btrfs_symlink,
  6108. .setattr = btrfs_setattr,
  6109. .mknod = btrfs_mknod,
  6110. .setxattr = btrfs_setxattr,
  6111. .getxattr = btrfs_getxattr,
  6112. .listxattr = btrfs_listxattr,
  6113. .removexattr = btrfs_removexattr,
  6114. .permission = btrfs_permission,
  6115. };
  6116. static const struct inode_operations btrfs_dir_ro_inode_operations = {
  6117. .lookup = btrfs_lookup,
  6118. .permission = btrfs_permission,
  6119. };
  6120. static const struct file_operations btrfs_dir_file_operations = {
  6121. .llseek = generic_file_llseek,
  6122. .read = generic_read_dir,
  6123. .readdir = btrfs_real_readdir,
  6124. .unlocked_ioctl = btrfs_ioctl,
  6125. #ifdef CONFIG_COMPAT
  6126. .compat_ioctl = btrfs_ioctl,
  6127. #endif
  6128. .release = btrfs_release_file,
  6129. .fsync = btrfs_sync_file,
  6130. };
  6131. static struct extent_io_ops btrfs_extent_io_ops = {
  6132. .fill_delalloc = run_delalloc_range,
  6133. .submit_bio_hook = btrfs_submit_bio_hook,
  6134. .merge_bio_hook = btrfs_merge_bio_hook,
  6135. .readpage_end_io_hook = btrfs_readpage_end_io_hook,
  6136. .writepage_end_io_hook = btrfs_writepage_end_io_hook,
  6137. .writepage_start_hook = btrfs_writepage_start_hook,
  6138. .readpage_io_failed_hook = btrfs_io_failed_hook,
  6139. .set_bit_hook = btrfs_set_bit_hook,
  6140. .clear_bit_hook = btrfs_clear_bit_hook,
  6141. .merge_extent_hook = btrfs_merge_extent_hook,
  6142. .split_extent_hook = btrfs_split_extent_hook,
  6143. };
  6144. /*
  6145. * btrfs doesn't support the bmap operation because swapfiles
  6146. * use bmap to make a mapping of extents in the file. They assume
  6147. * these extents won't change over the life of the file and they
  6148. * use the bmap result to do IO directly to the drive.
  6149. *
  6150. * the btrfs bmap call would return logical addresses that aren't
  6151. * suitable for IO and they also will change frequently as COW
  6152. * operations happen. So, swapfile + btrfs == corruption.
  6153. *
  6154. * For now we're avoiding this by dropping bmap.
  6155. */
  6156. static const struct address_space_operations btrfs_aops = {
  6157. .readpage = btrfs_readpage,
  6158. .writepage = btrfs_writepage,
  6159. .writepages = btrfs_writepages,
  6160. .readpages = btrfs_readpages,
  6161. .sync_page = block_sync_page,
  6162. .direct_IO = btrfs_direct_IO,
  6163. .invalidatepage = btrfs_invalidatepage,
  6164. .releasepage = btrfs_releasepage,
  6165. .set_page_dirty = btrfs_set_page_dirty,
  6166. .error_remove_page = generic_error_remove_page,
  6167. };
  6168. static const struct address_space_operations btrfs_symlink_aops = {
  6169. .readpage = btrfs_readpage,
  6170. .writepage = btrfs_writepage,
  6171. .invalidatepage = btrfs_invalidatepage,
  6172. .releasepage = btrfs_releasepage,
  6173. };
  6174. static const struct inode_operations btrfs_file_inode_operations = {
  6175. .truncate = btrfs_truncate,
  6176. .getattr = btrfs_getattr,
  6177. .setattr = btrfs_setattr,
  6178. .setxattr = btrfs_setxattr,
  6179. .getxattr = btrfs_getxattr,
  6180. .listxattr = btrfs_listxattr,
  6181. .removexattr = btrfs_removexattr,
  6182. .permission = btrfs_permission,
  6183. .fallocate = btrfs_fallocate,
  6184. .fiemap = btrfs_fiemap,
  6185. };
  6186. static const struct inode_operations btrfs_special_inode_operations = {
  6187. .getattr = btrfs_getattr,
  6188. .setattr = btrfs_setattr,
  6189. .permission = btrfs_permission,
  6190. .setxattr = btrfs_setxattr,
  6191. .getxattr = btrfs_getxattr,
  6192. .listxattr = btrfs_listxattr,
  6193. .removexattr = btrfs_removexattr,
  6194. };
  6195. static const struct inode_operations btrfs_symlink_inode_operations = {
  6196. .readlink = generic_readlink,
  6197. .follow_link = page_follow_link_light,
  6198. .put_link = page_put_link,
  6199. .permission = btrfs_permission,
  6200. .setxattr = btrfs_setxattr,
  6201. .getxattr = btrfs_getxattr,
  6202. .listxattr = btrfs_listxattr,
  6203. .removexattr = btrfs_removexattr,
  6204. };
  6205. const struct dentry_operations btrfs_dentry_operations = {
  6206. .d_delete = btrfs_dentry_delete,
  6207. };