inode.c 196 KB

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