inode.c 196 KB

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