inode.c 196 KB

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