inode.c 197 KB

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