inode.c 197 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431
  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. /*
  3150. * This function puts in dummy file extents for the area we're creating a hole
  3151. * for. So if we are truncating this file to a larger size we need to insert
  3152. * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
  3153. * the range between oldsize and size
  3154. */
  3155. int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
  3156. {
  3157. struct btrfs_trans_handle *trans;
  3158. struct btrfs_root *root = BTRFS_I(inode)->root;
  3159. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  3160. struct extent_map *em = NULL;
  3161. struct extent_state *cached_state = NULL;
  3162. u64 mask = root->sectorsize - 1;
  3163. u64 hole_start = (oldsize + mask) & ~mask;
  3164. u64 block_end = (size + mask) & ~mask;
  3165. u64 last_byte;
  3166. u64 cur_offset;
  3167. u64 hole_size;
  3168. int err = 0;
  3169. if (size <= hole_start)
  3170. return 0;
  3171. while (1) {
  3172. struct btrfs_ordered_extent *ordered;
  3173. btrfs_wait_ordered_range(inode, hole_start,
  3174. block_end - hole_start);
  3175. lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
  3176. &cached_state, GFP_NOFS);
  3177. ordered = btrfs_lookup_ordered_extent(inode, hole_start);
  3178. if (!ordered)
  3179. break;
  3180. unlock_extent_cached(io_tree, hole_start, block_end - 1,
  3181. &cached_state, GFP_NOFS);
  3182. btrfs_put_ordered_extent(ordered);
  3183. }
  3184. cur_offset = hole_start;
  3185. while (1) {
  3186. em = btrfs_get_extent(inode, NULL, 0, cur_offset,
  3187. block_end - cur_offset, 0);
  3188. BUG_ON(IS_ERR(em) || !em);
  3189. last_byte = min(extent_map_end(em), block_end);
  3190. last_byte = (last_byte + mask) & ~mask;
  3191. if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
  3192. u64 hint_byte = 0;
  3193. hole_size = last_byte - cur_offset;
  3194. trans = btrfs_start_transaction(root, 2);
  3195. if (IS_ERR(trans)) {
  3196. err = PTR_ERR(trans);
  3197. break;
  3198. }
  3199. btrfs_set_trans_block_group(trans, inode);
  3200. err = btrfs_drop_extents(trans, inode, cur_offset,
  3201. cur_offset + hole_size,
  3202. &hint_byte, 1);
  3203. if (err)
  3204. break;
  3205. err = btrfs_insert_file_extent(trans, root,
  3206. inode->i_ino, cur_offset, 0,
  3207. 0, hole_size, 0, hole_size,
  3208. 0, 0, 0);
  3209. if (err)
  3210. break;
  3211. btrfs_drop_extent_cache(inode, hole_start,
  3212. last_byte - 1, 0);
  3213. btrfs_end_transaction(trans, root);
  3214. }
  3215. free_extent_map(em);
  3216. em = NULL;
  3217. cur_offset = last_byte;
  3218. if (cur_offset >= block_end)
  3219. break;
  3220. }
  3221. free_extent_map(em);
  3222. unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
  3223. GFP_NOFS);
  3224. return err;
  3225. }
  3226. static int btrfs_setsize(struct inode *inode, loff_t newsize)
  3227. {
  3228. loff_t oldsize = i_size_read(inode);
  3229. int ret;
  3230. if (newsize == oldsize)
  3231. return 0;
  3232. if (newsize > oldsize) {
  3233. i_size_write(inode, newsize);
  3234. btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
  3235. truncate_pagecache(inode, oldsize, newsize);
  3236. ret = btrfs_cont_expand(inode, oldsize, newsize);
  3237. if (ret) {
  3238. btrfs_setsize(inode, oldsize);
  3239. return ret;
  3240. }
  3241. mark_inode_dirty(inode);
  3242. } else {
  3243. /*
  3244. * We're truncating a file that used to have good data down to
  3245. * zero. Make sure it gets into the ordered flush list so that
  3246. * any new writes get down to disk quickly.
  3247. */
  3248. if (newsize == 0)
  3249. BTRFS_I(inode)->ordered_data_close = 1;
  3250. /* we don't support swapfiles, so vmtruncate shouldn't fail */
  3251. truncate_setsize(inode, newsize);
  3252. ret = btrfs_truncate(inode);
  3253. }
  3254. return ret;
  3255. }
  3256. static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
  3257. {
  3258. struct inode *inode = dentry->d_inode;
  3259. struct btrfs_root *root = BTRFS_I(inode)->root;
  3260. int err;
  3261. if (btrfs_root_readonly(root))
  3262. return -EROFS;
  3263. err = inode_change_ok(inode, attr);
  3264. if (err)
  3265. return err;
  3266. if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
  3267. err = btrfs_setsize(inode, attr->ia_size);
  3268. if (err)
  3269. return err;
  3270. }
  3271. if (attr->ia_valid) {
  3272. setattr_copy(inode, attr);
  3273. mark_inode_dirty(inode);
  3274. if (attr->ia_valid & ATTR_MODE)
  3275. err = btrfs_acl_chmod(inode);
  3276. }
  3277. return err;
  3278. }
  3279. void btrfs_evict_inode(struct inode *inode)
  3280. {
  3281. struct btrfs_trans_handle *trans;
  3282. struct btrfs_root *root = BTRFS_I(inode)->root;
  3283. unsigned long nr;
  3284. int ret;
  3285. truncate_inode_pages(&inode->i_data, 0);
  3286. if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
  3287. root == root->fs_info->tree_root))
  3288. goto no_delete;
  3289. if (is_bad_inode(inode)) {
  3290. btrfs_orphan_del(NULL, inode);
  3291. goto no_delete;
  3292. }
  3293. /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
  3294. btrfs_wait_ordered_range(inode, 0, (u64)-1);
  3295. if (root->fs_info->log_root_recovering) {
  3296. BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan));
  3297. goto no_delete;
  3298. }
  3299. if (inode->i_nlink > 0) {
  3300. BUG_ON(btrfs_root_refs(&root->root_item) != 0);
  3301. goto no_delete;
  3302. }
  3303. btrfs_i_size_write(inode, 0);
  3304. while (1) {
  3305. trans = btrfs_start_transaction(root, 0);
  3306. BUG_ON(IS_ERR(trans));
  3307. btrfs_set_trans_block_group(trans, inode);
  3308. trans->block_rsv = root->orphan_block_rsv;
  3309. ret = btrfs_block_rsv_check(trans, root,
  3310. root->orphan_block_rsv, 0, 5);
  3311. if (ret) {
  3312. BUG_ON(ret != -EAGAIN);
  3313. ret = btrfs_commit_transaction(trans, root);
  3314. BUG_ON(ret);
  3315. continue;
  3316. }
  3317. ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
  3318. if (ret != -EAGAIN)
  3319. break;
  3320. nr = trans->blocks_used;
  3321. btrfs_end_transaction(trans, root);
  3322. trans = NULL;
  3323. btrfs_btree_balance_dirty(root, nr);
  3324. }
  3325. if (ret == 0) {
  3326. ret = btrfs_orphan_del(trans, inode);
  3327. BUG_ON(ret);
  3328. }
  3329. nr = trans->blocks_used;
  3330. btrfs_end_transaction(trans, root);
  3331. btrfs_btree_balance_dirty(root, nr);
  3332. no_delete:
  3333. end_writeback(inode);
  3334. return;
  3335. }
  3336. /*
  3337. * this returns the key found in the dir entry in the location pointer.
  3338. * If no dir entries were found, location->objectid is 0.
  3339. */
  3340. static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
  3341. struct btrfs_key *location)
  3342. {
  3343. const char *name = dentry->d_name.name;
  3344. int namelen = dentry->d_name.len;
  3345. struct btrfs_dir_item *di;
  3346. struct btrfs_path *path;
  3347. struct btrfs_root *root = BTRFS_I(dir)->root;
  3348. int ret = 0;
  3349. path = btrfs_alloc_path();
  3350. BUG_ON(!path);
  3351. di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
  3352. namelen, 0);
  3353. if (IS_ERR(di))
  3354. ret = PTR_ERR(di);
  3355. if (!di || IS_ERR(di))
  3356. goto out_err;
  3357. btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
  3358. out:
  3359. btrfs_free_path(path);
  3360. return ret;
  3361. out_err:
  3362. location->objectid = 0;
  3363. goto out;
  3364. }
  3365. /*
  3366. * when we hit a tree root in a directory, the btrfs part of the inode
  3367. * needs to be changed to reflect the root directory of the tree root. This
  3368. * is kind of like crossing a mount point.
  3369. */
  3370. static int fixup_tree_root_location(struct btrfs_root *root,
  3371. struct inode *dir,
  3372. struct dentry *dentry,
  3373. struct btrfs_key *location,
  3374. struct btrfs_root **sub_root)
  3375. {
  3376. struct btrfs_path *path;
  3377. struct btrfs_root *new_root;
  3378. struct btrfs_root_ref *ref;
  3379. struct extent_buffer *leaf;
  3380. int ret;
  3381. int err = 0;
  3382. path = btrfs_alloc_path();
  3383. if (!path) {
  3384. err = -ENOMEM;
  3385. goto out;
  3386. }
  3387. err = -ENOENT;
  3388. ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
  3389. BTRFS_I(dir)->root->root_key.objectid,
  3390. location->objectid);
  3391. if (ret) {
  3392. if (ret < 0)
  3393. err = ret;
  3394. goto out;
  3395. }
  3396. leaf = path->nodes[0];
  3397. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
  3398. if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino ||
  3399. btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
  3400. goto out;
  3401. ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
  3402. (unsigned long)(ref + 1),
  3403. dentry->d_name.len);
  3404. if (ret)
  3405. goto out;
  3406. btrfs_release_path(root->fs_info->tree_root, path);
  3407. new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
  3408. if (IS_ERR(new_root)) {
  3409. err = PTR_ERR(new_root);
  3410. goto out;
  3411. }
  3412. if (btrfs_root_refs(&new_root->root_item) == 0) {
  3413. err = -ENOENT;
  3414. goto out;
  3415. }
  3416. *sub_root = new_root;
  3417. location->objectid = btrfs_root_dirid(&new_root->root_item);
  3418. location->type = BTRFS_INODE_ITEM_KEY;
  3419. location->offset = 0;
  3420. err = 0;
  3421. out:
  3422. btrfs_free_path(path);
  3423. return err;
  3424. }
  3425. static void inode_tree_add(struct inode *inode)
  3426. {
  3427. struct btrfs_root *root = BTRFS_I(inode)->root;
  3428. struct btrfs_inode *entry;
  3429. struct rb_node **p;
  3430. struct rb_node *parent;
  3431. again:
  3432. p = &root->inode_tree.rb_node;
  3433. parent = NULL;
  3434. if (inode_unhashed(inode))
  3435. return;
  3436. spin_lock(&root->inode_lock);
  3437. while (*p) {
  3438. parent = *p;
  3439. entry = rb_entry(parent, struct btrfs_inode, rb_node);
  3440. if (inode->i_ino < entry->vfs_inode.i_ino)
  3441. p = &parent->rb_left;
  3442. else if (inode->i_ino > entry->vfs_inode.i_ino)
  3443. p = &parent->rb_right;
  3444. else {
  3445. WARN_ON(!(entry->vfs_inode.i_state &
  3446. (I_WILL_FREE | I_FREEING)));
  3447. rb_erase(parent, &root->inode_tree);
  3448. RB_CLEAR_NODE(parent);
  3449. spin_unlock(&root->inode_lock);
  3450. goto again;
  3451. }
  3452. }
  3453. rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
  3454. rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
  3455. spin_unlock(&root->inode_lock);
  3456. }
  3457. static void inode_tree_del(struct inode *inode)
  3458. {
  3459. struct btrfs_root *root = BTRFS_I(inode)->root;
  3460. int empty = 0;
  3461. spin_lock(&root->inode_lock);
  3462. if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
  3463. rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
  3464. RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
  3465. empty = RB_EMPTY_ROOT(&root->inode_tree);
  3466. }
  3467. spin_unlock(&root->inode_lock);
  3468. /*
  3469. * Free space cache has inodes in the tree root, but the tree root has a
  3470. * root_refs of 0, so this could end up dropping the tree root as a
  3471. * snapshot, so we need the extra !root->fs_info->tree_root check to
  3472. * make sure we don't drop it.
  3473. */
  3474. if (empty && btrfs_root_refs(&root->root_item) == 0 &&
  3475. root != root->fs_info->tree_root) {
  3476. synchronize_srcu(&root->fs_info->subvol_srcu);
  3477. spin_lock(&root->inode_lock);
  3478. empty = RB_EMPTY_ROOT(&root->inode_tree);
  3479. spin_unlock(&root->inode_lock);
  3480. if (empty)
  3481. btrfs_add_dead_root(root);
  3482. }
  3483. }
  3484. int btrfs_invalidate_inodes(struct btrfs_root *root)
  3485. {
  3486. struct rb_node *node;
  3487. struct rb_node *prev;
  3488. struct btrfs_inode *entry;
  3489. struct inode *inode;
  3490. u64 objectid = 0;
  3491. WARN_ON(btrfs_root_refs(&root->root_item) != 0);
  3492. spin_lock(&root->inode_lock);
  3493. again:
  3494. node = root->inode_tree.rb_node;
  3495. prev = NULL;
  3496. while (node) {
  3497. prev = node;
  3498. entry = rb_entry(node, struct btrfs_inode, rb_node);
  3499. if (objectid < entry->vfs_inode.i_ino)
  3500. node = node->rb_left;
  3501. else if (objectid > entry->vfs_inode.i_ino)
  3502. node = node->rb_right;
  3503. else
  3504. break;
  3505. }
  3506. if (!node) {
  3507. while (prev) {
  3508. entry = rb_entry(prev, struct btrfs_inode, rb_node);
  3509. if (objectid <= entry->vfs_inode.i_ino) {
  3510. node = prev;
  3511. break;
  3512. }
  3513. prev = rb_next(prev);
  3514. }
  3515. }
  3516. while (node) {
  3517. entry = rb_entry(node, struct btrfs_inode, rb_node);
  3518. objectid = entry->vfs_inode.i_ino + 1;
  3519. inode = igrab(&entry->vfs_inode);
  3520. if (inode) {
  3521. spin_unlock(&root->inode_lock);
  3522. if (atomic_read(&inode->i_count) > 1)
  3523. d_prune_aliases(inode);
  3524. /*
  3525. * btrfs_drop_inode will have it removed from
  3526. * the inode cache when its usage count
  3527. * hits zero.
  3528. */
  3529. iput(inode);
  3530. cond_resched();
  3531. spin_lock(&root->inode_lock);
  3532. goto again;
  3533. }
  3534. if (cond_resched_lock(&root->inode_lock))
  3535. goto again;
  3536. node = rb_next(node);
  3537. }
  3538. spin_unlock(&root->inode_lock);
  3539. return 0;
  3540. }
  3541. static int btrfs_init_locked_inode(struct inode *inode, void *p)
  3542. {
  3543. struct btrfs_iget_args *args = p;
  3544. inode->i_ino = args->ino;
  3545. BTRFS_I(inode)->root = args->root;
  3546. btrfs_set_inode_space_info(args->root, inode);
  3547. return 0;
  3548. }
  3549. static int btrfs_find_actor(struct inode *inode, void *opaque)
  3550. {
  3551. struct btrfs_iget_args *args = opaque;
  3552. return args->ino == inode->i_ino &&
  3553. args->root == BTRFS_I(inode)->root;
  3554. }
  3555. static struct inode *btrfs_iget_locked(struct super_block *s,
  3556. u64 objectid,
  3557. struct btrfs_root *root)
  3558. {
  3559. struct inode *inode;
  3560. struct btrfs_iget_args args;
  3561. args.ino = objectid;
  3562. args.root = root;
  3563. inode = iget5_locked(s, objectid, btrfs_find_actor,
  3564. btrfs_init_locked_inode,
  3565. (void *)&args);
  3566. return inode;
  3567. }
  3568. /* Get an inode object given its location and corresponding root.
  3569. * Returns in *is_new if the inode was read from disk
  3570. */
  3571. struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
  3572. struct btrfs_root *root, int *new)
  3573. {
  3574. struct inode *inode;
  3575. inode = btrfs_iget_locked(s, location->objectid, root);
  3576. if (!inode)
  3577. return ERR_PTR(-ENOMEM);
  3578. if (inode->i_state & I_NEW) {
  3579. BTRFS_I(inode)->root = root;
  3580. memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
  3581. btrfs_read_locked_inode(inode);
  3582. inode_tree_add(inode);
  3583. unlock_new_inode(inode);
  3584. if (new)
  3585. *new = 1;
  3586. }
  3587. return inode;
  3588. }
  3589. static struct inode *new_simple_dir(struct super_block *s,
  3590. struct btrfs_key *key,
  3591. struct btrfs_root *root)
  3592. {
  3593. struct inode *inode = new_inode(s);
  3594. if (!inode)
  3595. return ERR_PTR(-ENOMEM);
  3596. BTRFS_I(inode)->root = root;
  3597. memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
  3598. BTRFS_I(inode)->dummy_inode = 1;
  3599. inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
  3600. inode->i_op = &simple_dir_inode_operations;
  3601. inode->i_fop = &simple_dir_operations;
  3602. inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
  3603. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  3604. return inode;
  3605. }
  3606. struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
  3607. {
  3608. struct inode *inode;
  3609. struct btrfs_root *root = BTRFS_I(dir)->root;
  3610. struct btrfs_root *sub_root = root;
  3611. struct btrfs_key location;
  3612. int index;
  3613. int ret;
  3614. if (dentry->d_name.len > BTRFS_NAME_LEN)
  3615. return ERR_PTR(-ENAMETOOLONG);
  3616. ret = btrfs_inode_by_name(dir, dentry, &location);
  3617. if (ret < 0)
  3618. return ERR_PTR(ret);
  3619. if (location.objectid == 0)
  3620. return NULL;
  3621. if (location.type == BTRFS_INODE_ITEM_KEY) {
  3622. inode = btrfs_iget(dir->i_sb, &location, root, NULL);
  3623. return inode;
  3624. }
  3625. BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
  3626. index = srcu_read_lock(&root->fs_info->subvol_srcu);
  3627. ret = fixup_tree_root_location(root, dir, dentry,
  3628. &location, &sub_root);
  3629. if (ret < 0) {
  3630. if (ret != -ENOENT)
  3631. inode = ERR_PTR(ret);
  3632. else
  3633. inode = new_simple_dir(dir->i_sb, &location, sub_root);
  3634. } else {
  3635. inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
  3636. }
  3637. srcu_read_unlock(&root->fs_info->subvol_srcu, index);
  3638. if (!IS_ERR(inode) && root != sub_root) {
  3639. down_read(&root->fs_info->cleanup_work_sem);
  3640. if (!(inode->i_sb->s_flags & MS_RDONLY))
  3641. ret = btrfs_orphan_cleanup(sub_root);
  3642. up_read(&root->fs_info->cleanup_work_sem);
  3643. if (ret)
  3644. inode = ERR_PTR(ret);
  3645. }
  3646. return inode;
  3647. }
  3648. static int btrfs_dentry_delete(const struct dentry *dentry)
  3649. {
  3650. struct btrfs_root *root;
  3651. if (!dentry->d_inode && !IS_ROOT(dentry))
  3652. dentry = dentry->d_parent;
  3653. if (dentry->d_inode) {
  3654. root = BTRFS_I(dentry->d_inode)->root;
  3655. if (btrfs_root_refs(&root->root_item) == 0)
  3656. return 1;
  3657. }
  3658. return 0;
  3659. }
  3660. static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
  3661. struct nameidata *nd)
  3662. {
  3663. struct inode *inode;
  3664. inode = btrfs_lookup_dentry(dir, dentry);
  3665. if (IS_ERR(inode))
  3666. return ERR_CAST(inode);
  3667. return d_splice_alias(inode, dentry);
  3668. }
  3669. static unsigned char btrfs_filetype_table[] = {
  3670. DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
  3671. };
  3672. static int btrfs_real_readdir(struct file *filp, void *dirent,
  3673. filldir_t filldir)
  3674. {
  3675. struct inode *inode = filp->f_dentry->d_inode;
  3676. struct btrfs_root *root = BTRFS_I(inode)->root;
  3677. struct btrfs_item *item;
  3678. struct btrfs_dir_item *di;
  3679. struct btrfs_key key;
  3680. struct btrfs_key found_key;
  3681. struct btrfs_path *path;
  3682. int ret;
  3683. u32 nritems;
  3684. struct extent_buffer *leaf;
  3685. int slot;
  3686. int advance;
  3687. unsigned char d_type;
  3688. int over = 0;
  3689. u32 di_cur;
  3690. u32 di_total;
  3691. u32 di_len;
  3692. int key_type = BTRFS_DIR_INDEX_KEY;
  3693. char tmp_name[32];
  3694. char *name_ptr;
  3695. int name_len;
  3696. /* FIXME, use a real flag for deciding about the key type */
  3697. if (root->fs_info->tree_root == root)
  3698. key_type = BTRFS_DIR_ITEM_KEY;
  3699. /* special case for "." */
  3700. if (filp->f_pos == 0) {
  3701. over = filldir(dirent, ".", 1,
  3702. 1, inode->i_ino,
  3703. DT_DIR);
  3704. if (over)
  3705. return 0;
  3706. filp->f_pos = 1;
  3707. }
  3708. /* special case for .., just use the back ref */
  3709. if (filp->f_pos == 1) {
  3710. u64 pino = parent_ino(filp->f_path.dentry);
  3711. over = filldir(dirent, "..", 2,
  3712. 2, pino, DT_DIR);
  3713. if (over)
  3714. return 0;
  3715. filp->f_pos = 2;
  3716. }
  3717. path = btrfs_alloc_path();
  3718. path->reada = 2;
  3719. btrfs_set_key_type(&key, key_type);
  3720. key.offset = filp->f_pos;
  3721. key.objectid = inode->i_ino;
  3722. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3723. if (ret < 0)
  3724. goto err;
  3725. advance = 0;
  3726. while (1) {
  3727. leaf = path->nodes[0];
  3728. nritems = btrfs_header_nritems(leaf);
  3729. slot = path->slots[0];
  3730. if (advance || slot >= nritems) {
  3731. if (slot >= nritems - 1) {
  3732. ret = btrfs_next_leaf(root, path);
  3733. if (ret)
  3734. break;
  3735. leaf = path->nodes[0];
  3736. nritems = btrfs_header_nritems(leaf);
  3737. slot = path->slots[0];
  3738. } else {
  3739. slot++;
  3740. path->slots[0]++;
  3741. }
  3742. }
  3743. advance = 1;
  3744. item = btrfs_item_nr(leaf, slot);
  3745. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  3746. if (found_key.objectid != key.objectid)
  3747. break;
  3748. if (btrfs_key_type(&found_key) != key_type)
  3749. break;
  3750. if (found_key.offset < filp->f_pos)
  3751. continue;
  3752. filp->f_pos = found_key.offset;
  3753. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  3754. di_cur = 0;
  3755. di_total = btrfs_item_size(leaf, item);
  3756. while (di_cur < di_total) {
  3757. struct btrfs_key location;
  3758. if (verify_dir_item(root, leaf, di))
  3759. break;
  3760. name_len = btrfs_dir_name_len(leaf, di);
  3761. if (name_len <= sizeof(tmp_name)) {
  3762. name_ptr = tmp_name;
  3763. } else {
  3764. name_ptr = kmalloc(name_len, GFP_NOFS);
  3765. if (!name_ptr) {
  3766. ret = -ENOMEM;
  3767. goto err;
  3768. }
  3769. }
  3770. read_extent_buffer(leaf, name_ptr,
  3771. (unsigned long)(di + 1), name_len);
  3772. d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
  3773. btrfs_dir_item_key_to_cpu(leaf, di, &location);
  3774. /* is this a reference to our own snapshot? If so
  3775. * skip it
  3776. */
  3777. if (location.type == BTRFS_ROOT_ITEM_KEY &&
  3778. location.objectid == root->root_key.objectid) {
  3779. over = 0;
  3780. goto skip;
  3781. }
  3782. over = filldir(dirent, name_ptr, name_len,
  3783. found_key.offset, location.objectid,
  3784. d_type);
  3785. skip:
  3786. if (name_ptr != tmp_name)
  3787. kfree(name_ptr);
  3788. if (over)
  3789. goto nopos;
  3790. di_len = btrfs_dir_name_len(leaf, di) +
  3791. btrfs_dir_data_len(leaf, di) + sizeof(*di);
  3792. di_cur += di_len;
  3793. di = (struct btrfs_dir_item *)((char *)di + di_len);
  3794. }
  3795. }
  3796. /* Reached end of directory/root. Bump pos past the last item. */
  3797. if (key_type == BTRFS_DIR_INDEX_KEY)
  3798. /*
  3799. * 32-bit glibc will use getdents64, but then strtol -
  3800. * so the last number we can serve is this.
  3801. */
  3802. filp->f_pos = 0x7fffffff;
  3803. else
  3804. filp->f_pos++;
  3805. nopos:
  3806. ret = 0;
  3807. err:
  3808. btrfs_free_path(path);
  3809. return ret;
  3810. }
  3811. int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
  3812. {
  3813. struct btrfs_root *root = BTRFS_I(inode)->root;
  3814. struct btrfs_trans_handle *trans;
  3815. int ret = 0;
  3816. bool nolock = false;
  3817. if (BTRFS_I(inode)->dummy_inode)
  3818. return 0;
  3819. smp_mb();
  3820. nolock = (root->fs_info->closing && root == root->fs_info->tree_root);
  3821. if (wbc->sync_mode == WB_SYNC_ALL) {
  3822. if (nolock)
  3823. trans = btrfs_join_transaction_nolock(root, 1);
  3824. else
  3825. trans = btrfs_join_transaction(root, 1);
  3826. if (IS_ERR(trans))
  3827. return PTR_ERR(trans);
  3828. btrfs_set_trans_block_group(trans, inode);
  3829. if (nolock)
  3830. ret = btrfs_end_transaction_nolock(trans, root);
  3831. else
  3832. ret = btrfs_commit_transaction(trans, root);
  3833. }
  3834. return ret;
  3835. }
  3836. /*
  3837. * This is somewhat expensive, updating the tree every time the
  3838. * inode changes. But, it is most likely to find the inode in cache.
  3839. * FIXME, needs more benchmarking...there are no reasons other than performance
  3840. * to keep or drop this code.
  3841. */
  3842. void btrfs_dirty_inode(struct inode *inode)
  3843. {
  3844. struct btrfs_root *root = BTRFS_I(inode)->root;
  3845. struct btrfs_trans_handle *trans;
  3846. int ret;
  3847. if (BTRFS_I(inode)->dummy_inode)
  3848. return;
  3849. trans = btrfs_join_transaction(root, 1);
  3850. BUG_ON(IS_ERR(trans));
  3851. btrfs_set_trans_block_group(trans, inode);
  3852. ret = btrfs_update_inode(trans, root, inode);
  3853. if (ret && ret == -ENOSPC) {
  3854. /* whoops, lets try again with the full transaction */
  3855. btrfs_end_transaction(trans, root);
  3856. trans = btrfs_start_transaction(root, 1);
  3857. if (IS_ERR(trans)) {
  3858. if (printk_ratelimit()) {
  3859. printk(KERN_ERR "btrfs: fail to "
  3860. "dirty inode %lu error %ld\n",
  3861. inode->i_ino, PTR_ERR(trans));
  3862. }
  3863. return;
  3864. }
  3865. btrfs_set_trans_block_group(trans, inode);
  3866. ret = btrfs_update_inode(trans, root, inode);
  3867. if (ret) {
  3868. if (printk_ratelimit()) {
  3869. printk(KERN_ERR "btrfs: fail to "
  3870. "dirty inode %lu error %d\n",
  3871. inode->i_ino, ret);
  3872. }
  3873. }
  3874. }
  3875. btrfs_end_transaction(trans, root);
  3876. }
  3877. /*
  3878. * find the highest existing sequence number in a directory
  3879. * and then set the in-memory index_cnt variable to reflect
  3880. * free sequence numbers
  3881. */
  3882. static int btrfs_set_inode_index_count(struct inode *inode)
  3883. {
  3884. struct btrfs_root *root = BTRFS_I(inode)->root;
  3885. struct btrfs_key key, found_key;
  3886. struct btrfs_path *path;
  3887. struct extent_buffer *leaf;
  3888. int ret;
  3889. key.objectid = inode->i_ino;
  3890. btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
  3891. key.offset = (u64)-1;
  3892. path = btrfs_alloc_path();
  3893. if (!path)
  3894. return -ENOMEM;
  3895. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3896. if (ret < 0)
  3897. goto out;
  3898. /* FIXME: we should be able to handle this */
  3899. if (ret == 0)
  3900. goto out;
  3901. ret = 0;
  3902. /*
  3903. * MAGIC NUMBER EXPLANATION:
  3904. * since we search a directory based on f_pos we have to start at 2
  3905. * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
  3906. * else has to start at 2
  3907. */
  3908. if (path->slots[0] == 0) {
  3909. BTRFS_I(inode)->index_cnt = 2;
  3910. goto out;
  3911. }
  3912. path->slots[0]--;
  3913. leaf = path->nodes[0];
  3914. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3915. if (found_key.objectid != inode->i_ino ||
  3916. btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
  3917. BTRFS_I(inode)->index_cnt = 2;
  3918. goto out;
  3919. }
  3920. BTRFS_I(inode)->index_cnt = found_key.offset + 1;
  3921. out:
  3922. btrfs_free_path(path);
  3923. return ret;
  3924. }
  3925. /*
  3926. * helper to find a free sequence number in a given directory. This current
  3927. * code is very simple, later versions will do smarter things in the btree
  3928. */
  3929. int btrfs_set_inode_index(struct inode *dir, u64 *index)
  3930. {
  3931. int ret = 0;
  3932. if (BTRFS_I(dir)->index_cnt == (u64)-1) {
  3933. ret = btrfs_set_inode_index_count(dir);
  3934. if (ret)
  3935. return ret;
  3936. }
  3937. *index = BTRFS_I(dir)->index_cnt;
  3938. BTRFS_I(dir)->index_cnt++;
  3939. return ret;
  3940. }
  3941. static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
  3942. struct btrfs_root *root,
  3943. struct inode *dir,
  3944. const char *name, int name_len,
  3945. u64 ref_objectid, u64 objectid,
  3946. u64 alloc_hint, int mode, u64 *index)
  3947. {
  3948. struct inode *inode;
  3949. struct btrfs_inode_item *inode_item;
  3950. struct btrfs_key *location;
  3951. struct btrfs_path *path;
  3952. struct btrfs_inode_ref *ref;
  3953. struct btrfs_key key[2];
  3954. u32 sizes[2];
  3955. unsigned long ptr;
  3956. int ret;
  3957. int owner;
  3958. path = btrfs_alloc_path();
  3959. BUG_ON(!path);
  3960. inode = new_inode(root->fs_info->sb);
  3961. if (!inode)
  3962. return ERR_PTR(-ENOMEM);
  3963. if (dir) {
  3964. ret = btrfs_set_inode_index(dir, index);
  3965. if (ret) {
  3966. iput(inode);
  3967. return ERR_PTR(ret);
  3968. }
  3969. }
  3970. /*
  3971. * index_cnt is ignored for everything but a dir,
  3972. * btrfs_get_inode_index_count has an explanation for the magic
  3973. * number
  3974. */
  3975. BTRFS_I(inode)->index_cnt = 2;
  3976. BTRFS_I(inode)->root = root;
  3977. BTRFS_I(inode)->generation = trans->transid;
  3978. inode->i_generation = BTRFS_I(inode)->generation;
  3979. btrfs_set_inode_space_info(root, inode);
  3980. if (mode & S_IFDIR)
  3981. owner = 0;
  3982. else
  3983. owner = 1;
  3984. BTRFS_I(inode)->block_group =
  3985. btrfs_find_block_group(root, 0, alloc_hint, owner);
  3986. key[0].objectid = objectid;
  3987. btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
  3988. key[0].offset = 0;
  3989. key[1].objectid = objectid;
  3990. btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
  3991. key[1].offset = ref_objectid;
  3992. sizes[0] = sizeof(struct btrfs_inode_item);
  3993. sizes[1] = name_len + sizeof(*ref);
  3994. path->leave_spinning = 1;
  3995. ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
  3996. if (ret != 0)
  3997. goto fail;
  3998. inode_init_owner(inode, dir, mode);
  3999. inode->i_ino = objectid;
  4000. inode_set_bytes(inode, 0);
  4001. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  4002. inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  4003. struct btrfs_inode_item);
  4004. fill_inode_item(trans, path->nodes[0], inode_item, inode);
  4005. ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
  4006. struct btrfs_inode_ref);
  4007. btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
  4008. btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
  4009. ptr = (unsigned long)(ref + 1);
  4010. write_extent_buffer(path->nodes[0], name, ptr, name_len);
  4011. btrfs_mark_buffer_dirty(path->nodes[0]);
  4012. btrfs_free_path(path);
  4013. location = &BTRFS_I(inode)->location;
  4014. location->objectid = objectid;
  4015. location->offset = 0;
  4016. btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
  4017. btrfs_inherit_iflags(inode, dir);
  4018. if ((mode & S_IFREG)) {
  4019. if (btrfs_test_opt(root, NODATASUM))
  4020. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
  4021. if (btrfs_test_opt(root, NODATACOW))
  4022. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
  4023. }
  4024. insert_inode_hash(inode);
  4025. inode_tree_add(inode);
  4026. return inode;
  4027. fail:
  4028. if (dir)
  4029. BTRFS_I(dir)->index_cnt--;
  4030. btrfs_free_path(path);
  4031. iput(inode);
  4032. return ERR_PTR(ret);
  4033. }
  4034. static inline u8 btrfs_inode_type(struct inode *inode)
  4035. {
  4036. return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
  4037. }
  4038. /*
  4039. * utility function to add 'inode' into 'parent_inode' with
  4040. * a give name and a given sequence number.
  4041. * if 'add_backref' is true, also insert a backref from the
  4042. * inode to the parent directory.
  4043. */
  4044. int btrfs_add_link(struct btrfs_trans_handle *trans,
  4045. struct inode *parent_inode, struct inode *inode,
  4046. const char *name, int name_len, int add_backref, u64 index)
  4047. {
  4048. int ret = 0;
  4049. struct btrfs_key key;
  4050. struct btrfs_root *root = BTRFS_I(parent_inode)->root;
  4051. if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  4052. memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
  4053. } else {
  4054. key.objectid = inode->i_ino;
  4055. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  4056. key.offset = 0;
  4057. }
  4058. if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  4059. ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
  4060. key.objectid, root->root_key.objectid,
  4061. parent_inode->i_ino,
  4062. index, name, name_len);
  4063. } else if (add_backref) {
  4064. ret = btrfs_insert_inode_ref(trans, root,
  4065. name, name_len, inode->i_ino,
  4066. parent_inode->i_ino, index);
  4067. }
  4068. if (ret == 0) {
  4069. ret = btrfs_insert_dir_item(trans, root, name, name_len,
  4070. parent_inode->i_ino, &key,
  4071. btrfs_inode_type(inode), index);
  4072. BUG_ON(ret);
  4073. btrfs_i_size_write(parent_inode, parent_inode->i_size +
  4074. name_len * 2);
  4075. parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
  4076. ret = btrfs_update_inode(trans, root, parent_inode);
  4077. }
  4078. return ret;
  4079. }
  4080. static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
  4081. struct inode *dir, struct dentry *dentry,
  4082. struct inode *inode, int backref, u64 index)
  4083. {
  4084. int err = btrfs_add_link(trans, dir, inode,
  4085. dentry->d_name.name, dentry->d_name.len,
  4086. backref, index);
  4087. if (!err) {
  4088. d_instantiate(dentry, inode);
  4089. return 0;
  4090. }
  4091. if (err > 0)
  4092. err = -EEXIST;
  4093. return err;
  4094. }
  4095. static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
  4096. int mode, dev_t rdev)
  4097. {
  4098. struct btrfs_trans_handle *trans;
  4099. struct btrfs_root *root = BTRFS_I(dir)->root;
  4100. struct inode *inode = NULL;
  4101. int err;
  4102. int drop_inode = 0;
  4103. u64 objectid;
  4104. unsigned long nr = 0;
  4105. u64 index = 0;
  4106. if (!new_valid_dev(rdev))
  4107. return -EINVAL;
  4108. err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
  4109. if (err)
  4110. return err;
  4111. /*
  4112. * 2 for inode item and ref
  4113. * 2 for dir items
  4114. * 1 for xattr if selinux is on
  4115. */
  4116. trans = btrfs_start_transaction(root, 5);
  4117. if (IS_ERR(trans))
  4118. return PTR_ERR(trans);
  4119. btrfs_set_trans_block_group(trans, dir);
  4120. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  4121. dentry->d_name.len, dir->i_ino, objectid,
  4122. BTRFS_I(dir)->block_group, mode, &index);
  4123. err = PTR_ERR(inode);
  4124. if (IS_ERR(inode))
  4125. goto out_unlock;
  4126. err = btrfs_init_inode_security(trans, inode, dir);
  4127. if (err) {
  4128. drop_inode = 1;
  4129. goto out_unlock;
  4130. }
  4131. btrfs_set_trans_block_group(trans, inode);
  4132. err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
  4133. if (err)
  4134. drop_inode = 1;
  4135. else {
  4136. inode->i_op = &btrfs_special_inode_operations;
  4137. init_special_inode(inode, inode->i_mode, rdev);
  4138. btrfs_update_inode(trans, root, inode);
  4139. }
  4140. btrfs_update_inode_block_group(trans, inode);
  4141. btrfs_update_inode_block_group(trans, dir);
  4142. out_unlock:
  4143. nr = trans->blocks_used;
  4144. btrfs_end_transaction_throttle(trans, root);
  4145. btrfs_btree_balance_dirty(root, nr);
  4146. if (drop_inode) {
  4147. inode_dec_link_count(inode);
  4148. iput(inode);
  4149. }
  4150. return err;
  4151. }
  4152. static int btrfs_create(struct inode *dir, struct dentry *dentry,
  4153. int mode, struct nameidata *nd)
  4154. {
  4155. struct btrfs_trans_handle *trans;
  4156. struct btrfs_root *root = BTRFS_I(dir)->root;
  4157. struct inode *inode = NULL;
  4158. int drop_inode = 0;
  4159. int err;
  4160. unsigned long nr = 0;
  4161. u64 objectid;
  4162. u64 index = 0;
  4163. err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
  4164. if (err)
  4165. return err;
  4166. /*
  4167. * 2 for inode item and ref
  4168. * 2 for dir items
  4169. * 1 for xattr if selinux is on
  4170. */
  4171. trans = btrfs_start_transaction(root, 5);
  4172. if (IS_ERR(trans))
  4173. return PTR_ERR(trans);
  4174. btrfs_set_trans_block_group(trans, dir);
  4175. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  4176. dentry->d_name.len, dir->i_ino, objectid,
  4177. BTRFS_I(dir)->block_group, mode, &index);
  4178. err = PTR_ERR(inode);
  4179. if (IS_ERR(inode))
  4180. goto out_unlock;
  4181. err = btrfs_init_inode_security(trans, inode, dir);
  4182. if (err) {
  4183. drop_inode = 1;
  4184. goto out_unlock;
  4185. }
  4186. btrfs_set_trans_block_group(trans, inode);
  4187. err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
  4188. if (err)
  4189. drop_inode = 1;
  4190. else {
  4191. inode->i_mapping->a_ops = &btrfs_aops;
  4192. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  4193. inode->i_fop = &btrfs_file_operations;
  4194. inode->i_op = &btrfs_file_inode_operations;
  4195. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  4196. }
  4197. btrfs_update_inode_block_group(trans, inode);
  4198. btrfs_update_inode_block_group(trans, dir);
  4199. out_unlock:
  4200. nr = trans->blocks_used;
  4201. btrfs_end_transaction_throttle(trans, root);
  4202. if (drop_inode) {
  4203. inode_dec_link_count(inode);
  4204. iput(inode);
  4205. }
  4206. btrfs_btree_balance_dirty(root, nr);
  4207. return err;
  4208. }
  4209. static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
  4210. struct dentry *dentry)
  4211. {
  4212. struct btrfs_trans_handle *trans;
  4213. struct btrfs_root *root = BTRFS_I(dir)->root;
  4214. struct inode *inode = old_dentry->d_inode;
  4215. u64 index;
  4216. unsigned long nr = 0;
  4217. int err;
  4218. int drop_inode = 0;
  4219. if (inode->i_nlink == 0)
  4220. return -ENOENT;
  4221. /* do not allow sys_link's with other subvols of the same device */
  4222. if (root->objectid != BTRFS_I(inode)->root->objectid)
  4223. return -EPERM;
  4224. btrfs_inc_nlink(inode);
  4225. inode->i_ctime = CURRENT_TIME;
  4226. err = btrfs_set_inode_index(dir, &index);
  4227. if (err)
  4228. goto fail;
  4229. /*
  4230. * 2 items for inode and inode ref
  4231. * 2 items for dir items
  4232. * 1 item for parent inode
  4233. */
  4234. trans = btrfs_start_transaction(root, 5);
  4235. if (IS_ERR(trans)) {
  4236. err = PTR_ERR(trans);
  4237. goto fail;
  4238. }
  4239. btrfs_set_trans_block_group(trans, dir);
  4240. ihold(inode);
  4241. err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
  4242. if (err) {
  4243. drop_inode = 1;
  4244. } else {
  4245. struct dentry *parent = dget_parent(dentry);
  4246. btrfs_update_inode_block_group(trans, dir);
  4247. err = btrfs_update_inode(trans, root, inode);
  4248. BUG_ON(err);
  4249. btrfs_log_new_name(trans, inode, NULL, parent);
  4250. dput(parent);
  4251. }
  4252. nr = trans->blocks_used;
  4253. btrfs_end_transaction_throttle(trans, root);
  4254. fail:
  4255. if (drop_inode) {
  4256. inode_dec_link_count(inode);
  4257. iput(inode);
  4258. }
  4259. btrfs_btree_balance_dirty(root, nr);
  4260. return err;
  4261. }
  4262. static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  4263. {
  4264. struct inode *inode = NULL;
  4265. struct btrfs_trans_handle *trans;
  4266. struct btrfs_root *root = BTRFS_I(dir)->root;
  4267. int err = 0;
  4268. int drop_on_err = 0;
  4269. u64 objectid = 0;
  4270. u64 index = 0;
  4271. unsigned long nr = 1;
  4272. err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
  4273. if (err)
  4274. return err;
  4275. /*
  4276. * 2 items for inode and ref
  4277. * 2 items for dir items
  4278. * 1 for xattr if selinux is on
  4279. */
  4280. trans = btrfs_start_transaction(root, 5);
  4281. if (IS_ERR(trans))
  4282. return PTR_ERR(trans);
  4283. btrfs_set_trans_block_group(trans, dir);
  4284. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  4285. dentry->d_name.len, dir->i_ino, objectid,
  4286. BTRFS_I(dir)->block_group, S_IFDIR | mode,
  4287. &index);
  4288. if (IS_ERR(inode)) {
  4289. err = PTR_ERR(inode);
  4290. goto out_fail;
  4291. }
  4292. drop_on_err = 1;
  4293. err = btrfs_init_inode_security(trans, inode, dir);
  4294. if (err)
  4295. goto out_fail;
  4296. inode->i_op = &btrfs_dir_inode_operations;
  4297. inode->i_fop = &btrfs_dir_file_operations;
  4298. btrfs_set_trans_block_group(trans, inode);
  4299. btrfs_i_size_write(inode, 0);
  4300. err = btrfs_update_inode(trans, root, inode);
  4301. if (err)
  4302. goto out_fail;
  4303. err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
  4304. dentry->d_name.len, 0, index);
  4305. if (err)
  4306. goto out_fail;
  4307. d_instantiate(dentry, inode);
  4308. drop_on_err = 0;
  4309. btrfs_update_inode_block_group(trans, inode);
  4310. btrfs_update_inode_block_group(trans, dir);
  4311. out_fail:
  4312. nr = trans->blocks_used;
  4313. btrfs_end_transaction_throttle(trans, root);
  4314. if (drop_on_err)
  4315. iput(inode);
  4316. btrfs_btree_balance_dirty(root, nr);
  4317. return err;
  4318. }
  4319. /* helper for btfs_get_extent. Given an existing extent in the tree,
  4320. * and an extent that you want to insert, deal with overlap and insert
  4321. * the new extent into the tree.
  4322. */
  4323. static int merge_extent_mapping(struct extent_map_tree *em_tree,
  4324. struct extent_map *existing,
  4325. struct extent_map *em,
  4326. u64 map_start, u64 map_len)
  4327. {
  4328. u64 start_diff;
  4329. BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
  4330. start_diff = map_start - em->start;
  4331. em->start = map_start;
  4332. em->len = map_len;
  4333. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  4334. !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  4335. em->block_start += start_diff;
  4336. em->block_len -= start_diff;
  4337. }
  4338. return add_extent_mapping(em_tree, em);
  4339. }
  4340. static noinline int uncompress_inline(struct btrfs_path *path,
  4341. struct inode *inode, struct page *page,
  4342. size_t pg_offset, u64 extent_offset,
  4343. struct btrfs_file_extent_item *item)
  4344. {
  4345. int ret;
  4346. struct extent_buffer *leaf = path->nodes[0];
  4347. char *tmp;
  4348. size_t max_size;
  4349. unsigned long inline_size;
  4350. unsigned long ptr;
  4351. int compress_type;
  4352. WARN_ON(pg_offset != 0);
  4353. compress_type = btrfs_file_extent_compression(leaf, item);
  4354. max_size = btrfs_file_extent_ram_bytes(leaf, item);
  4355. inline_size = btrfs_file_extent_inline_item_len(leaf,
  4356. btrfs_item_nr(leaf, path->slots[0]));
  4357. tmp = kmalloc(inline_size, GFP_NOFS);
  4358. ptr = btrfs_file_extent_inline_start(item);
  4359. read_extent_buffer(leaf, tmp, ptr, inline_size);
  4360. max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
  4361. ret = btrfs_decompress(compress_type, tmp, page,
  4362. extent_offset, inline_size, max_size);
  4363. if (ret) {
  4364. char *kaddr = kmap_atomic(page, KM_USER0);
  4365. unsigned long copy_size = min_t(u64,
  4366. PAGE_CACHE_SIZE - pg_offset,
  4367. max_size - extent_offset);
  4368. memset(kaddr + pg_offset, 0, copy_size);
  4369. kunmap_atomic(kaddr, KM_USER0);
  4370. }
  4371. kfree(tmp);
  4372. return 0;
  4373. }
  4374. /*
  4375. * a bit scary, this does extent mapping from logical file offset to the disk.
  4376. * the ugly parts come from merging extents from the disk with the in-ram
  4377. * representation. This gets more complex because of the data=ordered code,
  4378. * where the in-ram extents might be locked pending data=ordered completion.
  4379. *
  4380. * This also copies inline extents directly into the page.
  4381. */
  4382. struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
  4383. size_t pg_offset, u64 start, u64 len,
  4384. int create)
  4385. {
  4386. int ret;
  4387. int err = 0;
  4388. u64 bytenr;
  4389. u64 extent_start = 0;
  4390. u64 extent_end = 0;
  4391. u64 objectid = inode->i_ino;
  4392. u32 found_type;
  4393. struct btrfs_path *path = NULL;
  4394. struct btrfs_root *root = BTRFS_I(inode)->root;
  4395. struct btrfs_file_extent_item *item;
  4396. struct extent_buffer *leaf;
  4397. struct btrfs_key found_key;
  4398. struct extent_map *em = NULL;
  4399. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  4400. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  4401. struct btrfs_trans_handle *trans = NULL;
  4402. int compress_type;
  4403. again:
  4404. read_lock(&em_tree->lock);
  4405. em = lookup_extent_mapping(em_tree, start, len);
  4406. if (em)
  4407. em->bdev = root->fs_info->fs_devices->latest_bdev;
  4408. read_unlock(&em_tree->lock);
  4409. if (em) {
  4410. if (em->start > start || em->start + em->len <= start)
  4411. free_extent_map(em);
  4412. else if (em->block_start == EXTENT_MAP_INLINE && page)
  4413. free_extent_map(em);
  4414. else
  4415. goto out;
  4416. }
  4417. em = alloc_extent_map(GFP_NOFS);
  4418. if (!em) {
  4419. err = -ENOMEM;
  4420. goto out;
  4421. }
  4422. em->bdev = root->fs_info->fs_devices->latest_bdev;
  4423. em->start = EXTENT_MAP_HOLE;
  4424. em->orig_start = EXTENT_MAP_HOLE;
  4425. em->len = (u64)-1;
  4426. em->block_len = (u64)-1;
  4427. if (!path) {
  4428. path = btrfs_alloc_path();
  4429. BUG_ON(!path);
  4430. }
  4431. ret = btrfs_lookup_file_extent(trans, root, path,
  4432. objectid, start, trans != NULL);
  4433. if (ret < 0) {
  4434. err = ret;
  4435. goto out;
  4436. }
  4437. if (ret != 0) {
  4438. if (path->slots[0] == 0)
  4439. goto not_found;
  4440. path->slots[0]--;
  4441. }
  4442. leaf = path->nodes[0];
  4443. item = btrfs_item_ptr(leaf, path->slots[0],
  4444. struct btrfs_file_extent_item);
  4445. /* are we inside the extent that was found? */
  4446. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  4447. found_type = btrfs_key_type(&found_key);
  4448. if (found_key.objectid != objectid ||
  4449. found_type != BTRFS_EXTENT_DATA_KEY) {
  4450. goto not_found;
  4451. }
  4452. found_type = btrfs_file_extent_type(leaf, item);
  4453. extent_start = found_key.offset;
  4454. compress_type = btrfs_file_extent_compression(leaf, item);
  4455. if (found_type == BTRFS_FILE_EXTENT_REG ||
  4456. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  4457. extent_end = extent_start +
  4458. btrfs_file_extent_num_bytes(leaf, item);
  4459. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  4460. size_t size;
  4461. size = btrfs_file_extent_inline_len(leaf, item);
  4462. extent_end = (extent_start + size + root->sectorsize - 1) &
  4463. ~((u64)root->sectorsize - 1);
  4464. }
  4465. if (start >= extent_end) {
  4466. path->slots[0]++;
  4467. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  4468. ret = btrfs_next_leaf(root, path);
  4469. if (ret < 0) {
  4470. err = ret;
  4471. goto out;
  4472. }
  4473. if (ret > 0)
  4474. goto not_found;
  4475. leaf = path->nodes[0];
  4476. }
  4477. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  4478. if (found_key.objectid != objectid ||
  4479. found_key.type != BTRFS_EXTENT_DATA_KEY)
  4480. goto not_found;
  4481. if (start + len <= found_key.offset)
  4482. goto not_found;
  4483. em->start = start;
  4484. em->len = found_key.offset - start;
  4485. goto not_found_em;
  4486. }
  4487. if (found_type == BTRFS_FILE_EXTENT_REG ||
  4488. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  4489. em->start = extent_start;
  4490. em->len = extent_end - extent_start;
  4491. em->orig_start = extent_start -
  4492. btrfs_file_extent_offset(leaf, item);
  4493. bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
  4494. if (bytenr == 0) {
  4495. em->block_start = EXTENT_MAP_HOLE;
  4496. goto insert;
  4497. }
  4498. if (compress_type != BTRFS_COMPRESS_NONE) {
  4499. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  4500. em->compress_type = compress_type;
  4501. em->block_start = bytenr;
  4502. em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
  4503. item);
  4504. } else {
  4505. bytenr += btrfs_file_extent_offset(leaf, item);
  4506. em->block_start = bytenr;
  4507. em->block_len = em->len;
  4508. if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
  4509. set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
  4510. }
  4511. goto insert;
  4512. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  4513. unsigned long ptr;
  4514. char *map;
  4515. size_t size;
  4516. size_t extent_offset;
  4517. size_t copy_size;
  4518. em->block_start = EXTENT_MAP_INLINE;
  4519. if (!page || create) {
  4520. em->start = extent_start;
  4521. em->len = extent_end - extent_start;
  4522. goto out;
  4523. }
  4524. size = btrfs_file_extent_inline_len(leaf, item);
  4525. extent_offset = page_offset(page) + pg_offset - extent_start;
  4526. copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
  4527. size - extent_offset);
  4528. em->start = extent_start + extent_offset;
  4529. em->len = (copy_size + root->sectorsize - 1) &
  4530. ~((u64)root->sectorsize - 1);
  4531. em->orig_start = EXTENT_MAP_INLINE;
  4532. if (compress_type) {
  4533. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  4534. em->compress_type = compress_type;
  4535. }
  4536. ptr = btrfs_file_extent_inline_start(item) + extent_offset;
  4537. if (create == 0 && !PageUptodate(page)) {
  4538. if (btrfs_file_extent_compression(leaf, item) !=
  4539. BTRFS_COMPRESS_NONE) {
  4540. ret = uncompress_inline(path, inode, page,
  4541. pg_offset,
  4542. extent_offset, item);
  4543. BUG_ON(ret);
  4544. } else {
  4545. map = kmap(page);
  4546. read_extent_buffer(leaf, map + pg_offset, ptr,
  4547. copy_size);
  4548. if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
  4549. memset(map + pg_offset + copy_size, 0,
  4550. PAGE_CACHE_SIZE - pg_offset -
  4551. copy_size);
  4552. }
  4553. kunmap(page);
  4554. }
  4555. flush_dcache_page(page);
  4556. } else if (create && PageUptodate(page)) {
  4557. WARN_ON(1);
  4558. if (!trans) {
  4559. kunmap(page);
  4560. free_extent_map(em);
  4561. em = NULL;
  4562. btrfs_release_path(root, path);
  4563. trans = btrfs_join_transaction(root, 1);
  4564. if (IS_ERR(trans))
  4565. return ERR_CAST(trans);
  4566. goto again;
  4567. }
  4568. map = kmap(page);
  4569. write_extent_buffer(leaf, map + pg_offset, ptr,
  4570. copy_size);
  4571. kunmap(page);
  4572. btrfs_mark_buffer_dirty(leaf);
  4573. }
  4574. set_extent_uptodate(io_tree, em->start,
  4575. extent_map_end(em) - 1, GFP_NOFS);
  4576. goto insert;
  4577. } else {
  4578. printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
  4579. WARN_ON(1);
  4580. }
  4581. not_found:
  4582. em->start = start;
  4583. em->len = len;
  4584. not_found_em:
  4585. em->block_start = EXTENT_MAP_HOLE;
  4586. set_bit(EXTENT_FLAG_VACANCY, &em->flags);
  4587. insert:
  4588. btrfs_release_path(root, path);
  4589. if (em->start > start || extent_map_end(em) <= start) {
  4590. printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
  4591. "[%llu %llu]\n", (unsigned long long)em->start,
  4592. (unsigned long long)em->len,
  4593. (unsigned long long)start,
  4594. (unsigned long long)len);
  4595. err = -EIO;
  4596. goto out;
  4597. }
  4598. err = 0;
  4599. write_lock(&em_tree->lock);
  4600. ret = add_extent_mapping(em_tree, em);
  4601. /* it is possible that someone inserted the extent into the tree
  4602. * while we had the lock dropped. It is also possible that
  4603. * an overlapping map exists in the tree
  4604. */
  4605. if (ret == -EEXIST) {
  4606. struct extent_map *existing;
  4607. ret = 0;
  4608. existing = lookup_extent_mapping(em_tree, start, len);
  4609. if (existing && (existing->start > start ||
  4610. existing->start + existing->len <= start)) {
  4611. free_extent_map(existing);
  4612. existing = NULL;
  4613. }
  4614. if (!existing) {
  4615. existing = lookup_extent_mapping(em_tree, em->start,
  4616. em->len);
  4617. if (existing) {
  4618. err = merge_extent_mapping(em_tree, existing,
  4619. em, start,
  4620. root->sectorsize);
  4621. free_extent_map(existing);
  4622. if (err) {
  4623. free_extent_map(em);
  4624. em = NULL;
  4625. }
  4626. } else {
  4627. err = -EIO;
  4628. free_extent_map(em);
  4629. em = NULL;
  4630. }
  4631. } else {
  4632. free_extent_map(em);
  4633. em = existing;
  4634. err = 0;
  4635. }
  4636. }
  4637. write_unlock(&em_tree->lock);
  4638. out:
  4639. if (path)
  4640. btrfs_free_path(path);
  4641. if (trans) {
  4642. ret = btrfs_end_transaction(trans, root);
  4643. if (!err)
  4644. err = ret;
  4645. }
  4646. if (err) {
  4647. free_extent_map(em);
  4648. return ERR_PTR(err);
  4649. }
  4650. return em;
  4651. }
  4652. struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
  4653. size_t pg_offset, u64 start, u64 len,
  4654. int create)
  4655. {
  4656. struct extent_map *em;
  4657. struct extent_map *hole_em = NULL;
  4658. u64 range_start = start;
  4659. u64 end;
  4660. u64 found;
  4661. u64 found_end;
  4662. int err = 0;
  4663. em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
  4664. if (IS_ERR(em))
  4665. return em;
  4666. if (em) {
  4667. /*
  4668. * if our em maps to a hole, there might
  4669. * actually be delalloc bytes behind it
  4670. */
  4671. if (em->block_start != EXTENT_MAP_HOLE)
  4672. return em;
  4673. else
  4674. hole_em = em;
  4675. }
  4676. /* check to see if we've wrapped (len == -1 or similar) */
  4677. end = start + len;
  4678. if (end < start)
  4679. end = (u64)-1;
  4680. else
  4681. end -= 1;
  4682. em = NULL;
  4683. /* ok, we didn't find anything, lets look for delalloc */
  4684. found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
  4685. end, len, EXTENT_DELALLOC, 1);
  4686. found_end = range_start + found;
  4687. if (found_end < range_start)
  4688. found_end = (u64)-1;
  4689. /*
  4690. * we didn't find anything useful, return
  4691. * the original results from get_extent()
  4692. */
  4693. if (range_start > end || found_end <= start) {
  4694. em = hole_em;
  4695. hole_em = NULL;
  4696. goto out;
  4697. }
  4698. /* adjust the range_start to make sure it doesn't
  4699. * go backwards from the start they passed in
  4700. */
  4701. range_start = max(start,range_start);
  4702. found = found_end - range_start;
  4703. if (found > 0) {
  4704. u64 hole_start = start;
  4705. u64 hole_len = len;
  4706. em = alloc_extent_map(GFP_NOFS);
  4707. if (!em) {
  4708. err = -ENOMEM;
  4709. goto out;
  4710. }
  4711. /*
  4712. * when btrfs_get_extent can't find anything it
  4713. * returns one huge hole
  4714. *
  4715. * make sure what it found really fits our range, and
  4716. * adjust to make sure it is based on the start from
  4717. * the caller
  4718. */
  4719. if (hole_em) {
  4720. u64 calc_end = extent_map_end(hole_em);
  4721. if (calc_end <= start || (hole_em->start > end)) {
  4722. free_extent_map(hole_em);
  4723. hole_em = NULL;
  4724. } else {
  4725. hole_start = max(hole_em->start, start);
  4726. hole_len = calc_end - hole_start;
  4727. }
  4728. }
  4729. em->bdev = NULL;
  4730. if (hole_em && range_start > hole_start) {
  4731. /* our hole starts before our delalloc, so we
  4732. * have to return just the parts of the hole
  4733. * that go until the delalloc starts
  4734. */
  4735. em->len = min(hole_len,
  4736. range_start - hole_start);
  4737. em->start = hole_start;
  4738. em->orig_start = hole_start;
  4739. /*
  4740. * don't adjust block start at all,
  4741. * it is fixed at EXTENT_MAP_HOLE
  4742. */
  4743. em->block_start = hole_em->block_start;
  4744. em->block_len = hole_len;
  4745. } else {
  4746. em->start = range_start;
  4747. em->len = found;
  4748. em->orig_start = range_start;
  4749. em->block_start = EXTENT_MAP_DELALLOC;
  4750. em->block_len = found;
  4751. }
  4752. } else if (hole_em) {
  4753. return hole_em;
  4754. }
  4755. out:
  4756. free_extent_map(hole_em);
  4757. if (err) {
  4758. free_extent_map(em);
  4759. return ERR_PTR(err);
  4760. }
  4761. return em;
  4762. }
  4763. static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
  4764. u64 start, u64 len)
  4765. {
  4766. struct btrfs_root *root = BTRFS_I(inode)->root;
  4767. struct btrfs_trans_handle *trans;
  4768. struct extent_map *em;
  4769. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  4770. struct btrfs_key ins;
  4771. u64 alloc_hint;
  4772. int ret;
  4773. btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
  4774. trans = btrfs_join_transaction(root, 0);
  4775. if (IS_ERR(trans))
  4776. return ERR_CAST(trans);
  4777. trans->block_rsv = &root->fs_info->delalloc_block_rsv;
  4778. alloc_hint = get_extent_allocation_hint(inode, start, len);
  4779. ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
  4780. alloc_hint, (u64)-1, &ins, 1);
  4781. if (ret) {
  4782. em = ERR_PTR(ret);
  4783. goto out;
  4784. }
  4785. em = alloc_extent_map(GFP_NOFS);
  4786. if (!em) {
  4787. em = ERR_PTR(-ENOMEM);
  4788. goto out;
  4789. }
  4790. em->start = start;
  4791. em->orig_start = em->start;
  4792. em->len = ins.offset;
  4793. em->block_start = ins.objectid;
  4794. em->block_len = ins.offset;
  4795. em->bdev = root->fs_info->fs_devices->latest_bdev;
  4796. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  4797. while (1) {
  4798. write_lock(&em_tree->lock);
  4799. ret = add_extent_mapping(em_tree, em);
  4800. write_unlock(&em_tree->lock);
  4801. if (ret != -EEXIST)
  4802. break;
  4803. btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0);
  4804. }
  4805. ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
  4806. ins.offset, ins.offset, 0);
  4807. if (ret) {
  4808. btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
  4809. em = ERR_PTR(ret);
  4810. }
  4811. out:
  4812. btrfs_end_transaction(trans, root);
  4813. return em;
  4814. }
  4815. /*
  4816. * returns 1 when the nocow is safe, < 1 on error, 0 if the
  4817. * block must be cow'd
  4818. */
  4819. static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
  4820. struct inode *inode, u64 offset, u64 len)
  4821. {
  4822. struct btrfs_path *path;
  4823. int ret;
  4824. struct extent_buffer *leaf;
  4825. struct btrfs_root *root = BTRFS_I(inode)->root;
  4826. struct btrfs_file_extent_item *fi;
  4827. struct btrfs_key key;
  4828. u64 disk_bytenr;
  4829. u64 backref_offset;
  4830. u64 extent_end;
  4831. u64 num_bytes;
  4832. int slot;
  4833. int found_type;
  4834. path = btrfs_alloc_path();
  4835. if (!path)
  4836. return -ENOMEM;
  4837. ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
  4838. offset, 0);
  4839. if (ret < 0)
  4840. goto out;
  4841. slot = path->slots[0];
  4842. if (ret == 1) {
  4843. if (slot == 0) {
  4844. /* can't find the item, must cow */
  4845. ret = 0;
  4846. goto out;
  4847. }
  4848. slot--;
  4849. }
  4850. ret = 0;
  4851. leaf = path->nodes[0];
  4852. btrfs_item_key_to_cpu(leaf, &key, slot);
  4853. if (key.objectid != inode->i_ino ||
  4854. key.type != BTRFS_EXTENT_DATA_KEY) {
  4855. /* not our file or wrong item type, must cow */
  4856. goto out;
  4857. }
  4858. if (key.offset > offset) {
  4859. /* Wrong offset, must cow */
  4860. goto out;
  4861. }
  4862. fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  4863. found_type = btrfs_file_extent_type(leaf, fi);
  4864. if (found_type != BTRFS_FILE_EXTENT_REG &&
  4865. found_type != BTRFS_FILE_EXTENT_PREALLOC) {
  4866. /* not a regular extent, must cow */
  4867. goto out;
  4868. }
  4869. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  4870. backref_offset = btrfs_file_extent_offset(leaf, fi);
  4871. extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
  4872. if (extent_end < offset + len) {
  4873. /* extent doesn't include our full range, must cow */
  4874. goto out;
  4875. }
  4876. if (btrfs_extent_readonly(root, disk_bytenr))
  4877. goto out;
  4878. /*
  4879. * look for other files referencing this extent, if we
  4880. * find any we must cow
  4881. */
  4882. if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
  4883. key.offset - backref_offset, disk_bytenr))
  4884. goto out;
  4885. /*
  4886. * adjust disk_bytenr and num_bytes to cover just the bytes
  4887. * in this extent we are about to write. If there
  4888. * are any csums in that range we have to cow in order
  4889. * to keep the csums correct
  4890. */
  4891. disk_bytenr += backref_offset;
  4892. disk_bytenr += offset - key.offset;
  4893. num_bytes = min(offset + len, extent_end) - offset;
  4894. if (csum_exist_in_range(root, disk_bytenr, num_bytes))
  4895. goto out;
  4896. /*
  4897. * all of the above have passed, it is safe to overwrite this extent
  4898. * without cow
  4899. */
  4900. ret = 1;
  4901. out:
  4902. btrfs_free_path(path);
  4903. return ret;
  4904. }
  4905. static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
  4906. struct buffer_head *bh_result, int create)
  4907. {
  4908. struct extent_map *em;
  4909. struct btrfs_root *root = BTRFS_I(inode)->root;
  4910. u64 start = iblock << inode->i_blkbits;
  4911. u64 len = bh_result->b_size;
  4912. struct btrfs_trans_handle *trans;
  4913. em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
  4914. if (IS_ERR(em))
  4915. return PTR_ERR(em);
  4916. /*
  4917. * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
  4918. * io. INLINE is special, and we could probably kludge it in here, but
  4919. * it's still buffered so for safety lets just fall back to the generic
  4920. * buffered path.
  4921. *
  4922. * For COMPRESSED we _have_ to read the entire extent in so we can
  4923. * decompress it, so there will be buffering required no matter what we
  4924. * do, so go ahead and fallback to buffered.
  4925. *
  4926. * We return -ENOTBLK because thats what makes DIO go ahead and go back
  4927. * to buffered IO. Don't blame me, this is the price we pay for using
  4928. * the generic code.
  4929. */
  4930. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
  4931. em->block_start == EXTENT_MAP_INLINE) {
  4932. free_extent_map(em);
  4933. return -ENOTBLK;
  4934. }
  4935. /* Just a good old fashioned hole, return */
  4936. if (!create && (em->block_start == EXTENT_MAP_HOLE ||
  4937. test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
  4938. free_extent_map(em);
  4939. /* DIO will do one hole at a time, so just unlock a sector */
  4940. unlock_extent(&BTRFS_I(inode)->io_tree, start,
  4941. start + root->sectorsize - 1, GFP_NOFS);
  4942. return 0;
  4943. }
  4944. /*
  4945. * We don't allocate a new extent in the following cases
  4946. *
  4947. * 1) The inode is marked as NODATACOW. In this case we'll just use the
  4948. * existing extent.
  4949. * 2) The extent is marked as PREALLOC. We're good to go here and can
  4950. * just use the extent.
  4951. *
  4952. */
  4953. if (!create) {
  4954. len = em->len - (start - em->start);
  4955. goto map;
  4956. }
  4957. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
  4958. ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
  4959. em->block_start != EXTENT_MAP_HOLE)) {
  4960. int type;
  4961. int ret;
  4962. u64 block_start;
  4963. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  4964. type = BTRFS_ORDERED_PREALLOC;
  4965. else
  4966. type = BTRFS_ORDERED_NOCOW;
  4967. len = min(len, em->len - (start - em->start));
  4968. block_start = em->block_start + (start - em->start);
  4969. /*
  4970. * we're not going to log anything, but we do need
  4971. * to make sure the current transaction stays open
  4972. * while we look for nocow cross refs
  4973. */
  4974. trans = btrfs_join_transaction(root, 0);
  4975. if (IS_ERR(trans))
  4976. goto must_cow;
  4977. if (can_nocow_odirect(trans, inode, start, len) == 1) {
  4978. ret = btrfs_add_ordered_extent_dio(inode, start,
  4979. block_start, len, len, type);
  4980. btrfs_end_transaction(trans, root);
  4981. if (ret) {
  4982. free_extent_map(em);
  4983. return ret;
  4984. }
  4985. goto unlock;
  4986. }
  4987. btrfs_end_transaction(trans, root);
  4988. }
  4989. must_cow:
  4990. /*
  4991. * this will cow the extent, reset the len in case we changed
  4992. * it above
  4993. */
  4994. len = bh_result->b_size;
  4995. free_extent_map(em);
  4996. em = btrfs_new_extent_direct(inode, start, len);
  4997. if (IS_ERR(em))
  4998. return PTR_ERR(em);
  4999. len = min(len, em->len - (start - em->start));
  5000. unlock:
  5001. clear_extent_bit(&BTRFS_I(inode)->io_tree, start, start + len - 1,
  5002. EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DIRTY, 1,
  5003. 0, NULL, GFP_NOFS);
  5004. map:
  5005. bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
  5006. inode->i_blkbits;
  5007. bh_result->b_size = len;
  5008. bh_result->b_bdev = em->bdev;
  5009. set_buffer_mapped(bh_result);
  5010. if (create && !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  5011. set_buffer_new(bh_result);
  5012. free_extent_map(em);
  5013. return 0;
  5014. }
  5015. struct btrfs_dio_private {
  5016. struct inode *inode;
  5017. u64 logical_offset;
  5018. u64 disk_bytenr;
  5019. u64 bytes;
  5020. u32 *csums;
  5021. void *private;
  5022. /* number of bios pending for this dio */
  5023. atomic_t pending_bios;
  5024. /* IO errors */
  5025. int errors;
  5026. struct bio *orig_bio;
  5027. };
  5028. static void btrfs_endio_direct_read(struct bio *bio, int err)
  5029. {
  5030. struct btrfs_dio_private *dip = bio->bi_private;
  5031. struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
  5032. struct bio_vec *bvec = bio->bi_io_vec;
  5033. struct inode *inode = dip->inode;
  5034. struct btrfs_root *root = BTRFS_I(inode)->root;
  5035. u64 start;
  5036. u32 *private = dip->csums;
  5037. start = dip->logical_offset;
  5038. do {
  5039. if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
  5040. struct page *page = bvec->bv_page;
  5041. char *kaddr;
  5042. u32 csum = ~(u32)0;
  5043. unsigned long flags;
  5044. local_irq_save(flags);
  5045. kaddr = kmap_atomic(page, KM_IRQ0);
  5046. csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
  5047. csum, bvec->bv_len);
  5048. btrfs_csum_final(csum, (char *)&csum);
  5049. kunmap_atomic(kaddr, KM_IRQ0);
  5050. local_irq_restore(flags);
  5051. flush_dcache_page(bvec->bv_page);
  5052. if (csum != *private) {
  5053. printk(KERN_ERR "btrfs csum failed ino %lu off"
  5054. " %llu csum %u private %u\n",
  5055. inode->i_ino, (unsigned long long)start,
  5056. csum, *private);
  5057. err = -EIO;
  5058. }
  5059. }
  5060. start += bvec->bv_len;
  5061. private++;
  5062. bvec++;
  5063. } while (bvec <= bvec_end);
  5064. unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
  5065. dip->logical_offset + dip->bytes - 1, GFP_NOFS);
  5066. bio->bi_private = dip->private;
  5067. kfree(dip->csums);
  5068. kfree(dip);
  5069. dio_end_io(bio, err);
  5070. }
  5071. static void btrfs_endio_direct_write(struct bio *bio, int err)
  5072. {
  5073. struct btrfs_dio_private *dip = bio->bi_private;
  5074. struct inode *inode = dip->inode;
  5075. struct btrfs_root *root = BTRFS_I(inode)->root;
  5076. struct btrfs_trans_handle *trans;
  5077. struct btrfs_ordered_extent *ordered = NULL;
  5078. struct extent_state *cached_state = NULL;
  5079. u64 ordered_offset = dip->logical_offset;
  5080. u64 ordered_bytes = dip->bytes;
  5081. int ret;
  5082. if (err)
  5083. goto out_done;
  5084. again:
  5085. ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
  5086. &ordered_offset,
  5087. ordered_bytes);
  5088. if (!ret)
  5089. goto out_test;
  5090. BUG_ON(!ordered);
  5091. trans = btrfs_join_transaction(root, 1);
  5092. if (IS_ERR(trans)) {
  5093. err = -ENOMEM;
  5094. goto out;
  5095. }
  5096. trans->block_rsv = &root->fs_info->delalloc_block_rsv;
  5097. if (test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) {
  5098. ret = btrfs_ordered_update_i_size(inode, 0, ordered);
  5099. if (!ret)
  5100. ret = btrfs_update_inode(trans, root, inode);
  5101. err = ret;
  5102. goto out;
  5103. }
  5104. lock_extent_bits(&BTRFS_I(inode)->io_tree, ordered->file_offset,
  5105. ordered->file_offset + ordered->len - 1, 0,
  5106. &cached_state, GFP_NOFS);
  5107. if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) {
  5108. ret = btrfs_mark_extent_written(trans, inode,
  5109. ordered->file_offset,
  5110. ordered->file_offset +
  5111. ordered->len);
  5112. if (ret) {
  5113. err = ret;
  5114. goto out_unlock;
  5115. }
  5116. } else {
  5117. ret = insert_reserved_file_extent(trans, inode,
  5118. ordered->file_offset,
  5119. ordered->start,
  5120. ordered->disk_len,
  5121. ordered->len,
  5122. ordered->len,
  5123. 0, 0, 0,
  5124. BTRFS_FILE_EXTENT_REG);
  5125. unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
  5126. ordered->file_offset, ordered->len);
  5127. if (ret) {
  5128. err = ret;
  5129. WARN_ON(1);
  5130. goto out_unlock;
  5131. }
  5132. }
  5133. add_pending_csums(trans, inode, ordered->file_offset, &ordered->list);
  5134. btrfs_ordered_update_i_size(inode, 0, ordered);
  5135. btrfs_update_inode(trans, root, inode);
  5136. out_unlock:
  5137. unlock_extent_cached(&BTRFS_I(inode)->io_tree, ordered->file_offset,
  5138. ordered->file_offset + ordered->len - 1,
  5139. &cached_state, GFP_NOFS);
  5140. out:
  5141. btrfs_delalloc_release_metadata(inode, ordered->len);
  5142. btrfs_end_transaction(trans, root);
  5143. ordered_offset = ordered->file_offset + ordered->len;
  5144. btrfs_put_ordered_extent(ordered);
  5145. btrfs_put_ordered_extent(ordered);
  5146. out_test:
  5147. /*
  5148. * our bio might span multiple ordered extents. If we haven't
  5149. * completed the accounting for the whole dio, go back and try again
  5150. */
  5151. if (ordered_offset < dip->logical_offset + dip->bytes) {
  5152. ordered_bytes = dip->logical_offset + dip->bytes -
  5153. ordered_offset;
  5154. goto again;
  5155. }
  5156. out_done:
  5157. bio->bi_private = dip->private;
  5158. kfree(dip->csums);
  5159. kfree(dip);
  5160. dio_end_io(bio, err);
  5161. }
  5162. static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
  5163. struct bio *bio, int mirror_num,
  5164. unsigned long bio_flags, u64 offset)
  5165. {
  5166. int ret;
  5167. struct btrfs_root *root = BTRFS_I(inode)->root;
  5168. ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
  5169. BUG_ON(ret);
  5170. return 0;
  5171. }
  5172. static void btrfs_end_dio_bio(struct bio *bio, int err)
  5173. {
  5174. struct btrfs_dio_private *dip = bio->bi_private;
  5175. if (err) {
  5176. printk(KERN_ERR "btrfs direct IO failed ino %lu rw %lu "
  5177. "sector %#Lx len %u err no %d\n",
  5178. dip->inode->i_ino, bio->bi_rw,
  5179. (unsigned long long)bio->bi_sector, bio->bi_size, err);
  5180. dip->errors = 1;
  5181. /*
  5182. * before atomic variable goto zero, we must make sure
  5183. * dip->errors is perceived to be set.
  5184. */
  5185. smp_mb__before_atomic_dec();
  5186. }
  5187. /* if there are more bios still pending for this dio, just exit */
  5188. if (!atomic_dec_and_test(&dip->pending_bios))
  5189. goto out;
  5190. if (dip->errors)
  5191. bio_io_error(dip->orig_bio);
  5192. else {
  5193. set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
  5194. bio_endio(dip->orig_bio, 0);
  5195. }
  5196. out:
  5197. bio_put(bio);
  5198. }
  5199. static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
  5200. u64 first_sector, gfp_t gfp_flags)
  5201. {
  5202. int nr_vecs = bio_get_nr_vecs(bdev);
  5203. return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
  5204. }
  5205. static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
  5206. int rw, u64 file_offset, int skip_sum,
  5207. u32 *csums)
  5208. {
  5209. int write = rw & REQ_WRITE;
  5210. struct btrfs_root *root = BTRFS_I(inode)->root;
  5211. int ret;
  5212. bio_get(bio);
  5213. ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
  5214. if (ret)
  5215. goto err;
  5216. if (write && !skip_sum) {
  5217. ret = btrfs_wq_submit_bio(root->fs_info,
  5218. inode, rw, bio, 0, 0,
  5219. file_offset,
  5220. __btrfs_submit_bio_start_direct_io,
  5221. __btrfs_submit_bio_done);
  5222. goto err;
  5223. } else if (!skip_sum)
  5224. btrfs_lookup_bio_sums_dio(root, inode, bio,
  5225. file_offset, csums);
  5226. ret = btrfs_map_bio(root, rw, bio, 0, 1);
  5227. err:
  5228. bio_put(bio);
  5229. return ret;
  5230. }
  5231. static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
  5232. int skip_sum)
  5233. {
  5234. struct inode *inode = dip->inode;
  5235. struct btrfs_root *root = BTRFS_I(inode)->root;
  5236. struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
  5237. struct bio *bio;
  5238. struct bio *orig_bio = dip->orig_bio;
  5239. struct bio_vec *bvec = orig_bio->bi_io_vec;
  5240. u64 start_sector = orig_bio->bi_sector;
  5241. u64 file_offset = dip->logical_offset;
  5242. u64 submit_len = 0;
  5243. u64 map_length;
  5244. int nr_pages = 0;
  5245. u32 *csums = dip->csums;
  5246. int ret = 0;
  5247. int write = rw & REQ_WRITE;
  5248. bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
  5249. if (!bio)
  5250. return -ENOMEM;
  5251. bio->bi_private = dip;
  5252. bio->bi_end_io = btrfs_end_dio_bio;
  5253. atomic_inc(&dip->pending_bios);
  5254. map_length = orig_bio->bi_size;
  5255. ret = btrfs_map_block(map_tree, READ, start_sector << 9,
  5256. &map_length, NULL, 0);
  5257. if (ret) {
  5258. bio_put(bio);
  5259. return -EIO;
  5260. }
  5261. while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
  5262. if (unlikely(map_length < submit_len + bvec->bv_len ||
  5263. bio_add_page(bio, bvec->bv_page, bvec->bv_len,
  5264. bvec->bv_offset) < bvec->bv_len)) {
  5265. /*
  5266. * inc the count before we submit the bio so
  5267. * we know the end IO handler won't happen before
  5268. * we inc the count. Otherwise, the dip might get freed
  5269. * before we're done setting it up
  5270. */
  5271. atomic_inc(&dip->pending_bios);
  5272. ret = __btrfs_submit_dio_bio(bio, inode, rw,
  5273. file_offset, skip_sum,
  5274. csums);
  5275. if (ret) {
  5276. bio_put(bio);
  5277. atomic_dec(&dip->pending_bios);
  5278. goto out_err;
  5279. }
  5280. /* Write's use the ordered csums */
  5281. if (!write && !skip_sum)
  5282. csums = csums + nr_pages;
  5283. start_sector += submit_len >> 9;
  5284. file_offset += submit_len;
  5285. submit_len = 0;
  5286. nr_pages = 0;
  5287. bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
  5288. start_sector, GFP_NOFS);
  5289. if (!bio)
  5290. goto out_err;
  5291. bio->bi_private = dip;
  5292. bio->bi_end_io = btrfs_end_dio_bio;
  5293. map_length = orig_bio->bi_size;
  5294. ret = btrfs_map_block(map_tree, READ, start_sector << 9,
  5295. &map_length, NULL, 0);
  5296. if (ret) {
  5297. bio_put(bio);
  5298. goto out_err;
  5299. }
  5300. } else {
  5301. submit_len += bvec->bv_len;
  5302. nr_pages ++;
  5303. bvec++;
  5304. }
  5305. }
  5306. ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
  5307. csums);
  5308. if (!ret)
  5309. return 0;
  5310. bio_put(bio);
  5311. out_err:
  5312. dip->errors = 1;
  5313. /*
  5314. * before atomic variable goto zero, we must
  5315. * make sure dip->errors is perceived to be set.
  5316. */
  5317. smp_mb__before_atomic_dec();
  5318. if (atomic_dec_and_test(&dip->pending_bios))
  5319. bio_io_error(dip->orig_bio);
  5320. /* bio_end_io() will handle error, so we needn't return it */
  5321. return 0;
  5322. }
  5323. static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
  5324. loff_t file_offset)
  5325. {
  5326. struct btrfs_root *root = BTRFS_I(inode)->root;
  5327. struct btrfs_dio_private *dip;
  5328. struct bio_vec *bvec = bio->bi_io_vec;
  5329. int skip_sum;
  5330. int write = rw & REQ_WRITE;
  5331. int ret = 0;
  5332. skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  5333. dip = kmalloc(sizeof(*dip), GFP_NOFS);
  5334. if (!dip) {
  5335. ret = -ENOMEM;
  5336. goto free_ordered;
  5337. }
  5338. dip->csums = NULL;
  5339. /* Write's use the ordered csum stuff, so we don't need dip->csums */
  5340. if (!write && !skip_sum) {
  5341. dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS);
  5342. if (!dip->csums) {
  5343. kfree(dip);
  5344. ret = -ENOMEM;
  5345. goto free_ordered;
  5346. }
  5347. }
  5348. dip->private = bio->bi_private;
  5349. dip->inode = inode;
  5350. dip->logical_offset = file_offset;
  5351. dip->bytes = 0;
  5352. do {
  5353. dip->bytes += bvec->bv_len;
  5354. bvec++;
  5355. } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
  5356. dip->disk_bytenr = (u64)bio->bi_sector << 9;
  5357. bio->bi_private = dip;
  5358. dip->errors = 0;
  5359. dip->orig_bio = bio;
  5360. atomic_set(&dip->pending_bios, 0);
  5361. if (write)
  5362. bio->bi_end_io = btrfs_endio_direct_write;
  5363. else
  5364. bio->bi_end_io = btrfs_endio_direct_read;
  5365. ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
  5366. if (!ret)
  5367. return;
  5368. free_ordered:
  5369. /*
  5370. * If this is a write, we need to clean up the reserved space and kill
  5371. * the ordered extent.
  5372. */
  5373. if (write) {
  5374. struct btrfs_ordered_extent *ordered;
  5375. ordered = btrfs_lookup_ordered_extent(inode, file_offset);
  5376. if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
  5377. !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
  5378. btrfs_free_reserved_extent(root, ordered->start,
  5379. ordered->disk_len);
  5380. btrfs_put_ordered_extent(ordered);
  5381. btrfs_put_ordered_extent(ordered);
  5382. }
  5383. bio_endio(bio, ret);
  5384. }
  5385. static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
  5386. const struct iovec *iov, loff_t offset,
  5387. unsigned long nr_segs)
  5388. {
  5389. int seg;
  5390. size_t size;
  5391. unsigned long addr;
  5392. unsigned blocksize_mask = root->sectorsize - 1;
  5393. ssize_t retval = -EINVAL;
  5394. loff_t end = offset;
  5395. if (offset & blocksize_mask)
  5396. goto out;
  5397. /* Check the memory alignment. Blocks cannot straddle pages */
  5398. for (seg = 0; seg < nr_segs; seg++) {
  5399. addr = (unsigned long)iov[seg].iov_base;
  5400. size = iov[seg].iov_len;
  5401. end += size;
  5402. if ((addr & blocksize_mask) || (size & blocksize_mask))
  5403. goto out;
  5404. }
  5405. retval = 0;
  5406. out:
  5407. return retval;
  5408. }
  5409. static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
  5410. const struct iovec *iov, loff_t offset,
  5411. unsigned long nr_segs)
  5412. {
  5413. struct file *file = iocb->ki_filp;
  5414. struct inode *inode = file->f_mapping->host;
  5415. struct btrfs_ordered_extent *ordered;
  5416. struct extent_state *cached_state = NULL;
  5417. u64 lockstart, lockend;
  5418. ssize_t ret;
  5419. int writing = rw & WRITE;
  5420. int write_bits = 0;
  5421. size_t count = iov_length(iov, nr_segs);
  5422. if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
  5423. offset, nr_segs)) {
  5424. return 0;
  5425. }
  5426. lockstart = offset;
  5427. lockend = offset + count - 1;
  5428. if (writing) {
  5429. ret = btrfs_delalloc_reserve_space(inode, count);
  5430. if (ret)
  5431. goto out;
  5432. }
  5433. while (1) {
  5434. lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  5435. 0, &cached_state, GFP_NOFS);
  5436. /*
  5437. * We're concerned with the entire range that we're going to be
  5438. * doing DIO to, so we need to make sure theres no ordered
  5439. * extents in this range.
  5440. */
  5441. ordered = btrfs_lookup_ordered_range(inode, lockstart,
  5442. lockend - lockstart + 1);
  5443. if (!ordered)
  5444. break;
  5445. unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  5446. &cached_state, GFP_NOFS);
  5447. btrfs_start_ordered_extent(inode, ordered, 1);
  5448. btrfs_put_ordered_extent(ordered);
  5449. cond_resched();
  5450. }
  5451. /*
  5452. * we don't use btrfs_set_extent_delalloc because we don't want
  5453. * the dirty or uptodate bits
  5454. */
  5455. if (writing) {
  5456. write_bits = EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING;
  5457. ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  5458. EXTENT_DELALLOC, 0, NULL, &cached_state,
  5459. GFP_NOFS);
  5460. if (ret) {
  5461. clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
  5462. lockend, EXTENT_LOCKED | write_bits,
  5463. 1, 0, &cached_state, GFP_NOFS);
  5464. goto out;
  5465. }
  5466. }
  5467. free_extent_state(cached_state);
  5468. cached_state = NULL;
  5469. ret = __blockdev_direct_IO(rw, iocb, inode,
  5470. BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
  5471. iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
  5472. btrfs_submit_direct, 0);
  5473. if (ret < 0 && ret != -EIOCBQUEUED) {
  5474. clear_extent_bit(&BTRFS_I(inode)->io_tree, offset,
  5475. offset + iov_length(iov, nr_segs) - 1,
  5476. EXTENT_LOCKED | write_bits, 1, 0,
  5477. &cached_state, GFP_NOFS);
  5478. } else if (ret >= 0 && ret < iov_length(iov, nr_segs)) {
  5479. /*
  5480. * We're falling back to buffered, unlock the section we didn't
  5481. * do IO on.
  5482. */
  5483. clear_extent_bit(&BTRFS_I(inode)->io_tree, offset + ret,
  5484. offset + iov_length(iov, nr_segs) - 1,
  5485. EXTENT_LOCKED | write_bits, 1, 0,
  5486. &cached_state, GFP_NOFS);
  5487. }
  5488. out:
  5489. free_extent_state(cached_state);
  5490. return ret;
  5491. }
  5492. static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  5493. __u64 start, __u64 len)
  5494. {
  5495. return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
  5496. }
  5497. int btrfs_readpage(struct file *file, struct page *page)
  5498. {
  5499. struct extent_io_tree *tree;
  5500. tree = &BTRFS_I(page->mapping->host)->io_tree;
  5501. return extent_read_full_page(tree, page, btrfs_get_extent);
  5502. }
  5503. static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
  5504. {
  5505. struct extent_io_tree *tree;
  5506. if (current->flags & PF_MEMALLOC) {
  5507. redirty_page_for_writepage(wbc, page);
  5508. unlock_page(page);
  5509. return 0;
  5510. }
  5511. tree = &BTRFS_I(page->mapping->host)->io_tree;
  5512. return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
  5513. }
  5514. int btrfs_writepages(struct address_space *mapping,
  5515. struct writeback_control *wbc)
  5516. {
  5517. struct extent_io_tree *tree;
  5518. tree = &BTRFS_I(mapping->host)->io_tree;
  5519. return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
  5520. }
  5521. static int
  5522. btrfs_readpages(struct file *file, struct address_space *mapping,
  5523. struct list_head *pages, unsigned nr_pages)
  5524. {
  5525. struct extent_io_tree *tree;
  5526. tree = &BTRFS_I(mapping->host)->io_tree;
  5527. return extent_readpages(tree, mapping, pages, nr_pages,
  5528. btrfs_get_extent);
  5529. }
  5530. static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  5531. {
  5532. struct extent_io_tree *tree;
  5533. struct extent_map_tree *map;
  5534. int ret;
  5535. tree = &BTRFS_I(page->mapping->host)->io_tree;
  5536. map = &BTRFS_I(page->mapping->host)->extent_tree;
  5537. ret = try_release_extent_mapping(map, tree, page, gfp_flags);
  5538. if (ret == 1) {
  5539. ClearPagePrivate(page);
  5540. set_page_private(page, 0);
  5541. page_cache_release(page);
  5542. }
  5543. return ret;
  5544. }
  5545. static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  5546. {
  5547. if (PageWriteback(page) || PageDirty(page))
  5548. return 0;
  5549. return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
  5550. }
  5551. static void btrfs_invalidatepage(struct page *page, unsigned long offset)
  5552. {
  5553. struct extent_io_tree *tree;
  5554. struct btrfs_ordered_extent *ordered;
  5555. struct extent_state *cached_state = NULL;
  5556. u64 page_start = page_offset(page);
  5557. u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
  5558. /*
  5559. * we have the page locked, so new writeback can't start,
  5560. * and the dirty bit won't be cleared while we are here.
  5561. *
  5562. * Wait for IO on this page so that we can safely clear
  5563. * the PagePrivate2 bit and do ordered accounting
  5564. */
  5565. wait_on_page_writeback(page);
  5566. tree = &BTRFS_I(page->mapping->host)->io_tree;
  5567. if (offset) {
  5568. btrfs_releasepage(page, GFP_NOFS);
  5569. return;
  5570. }
  5571. lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
  5572. GFP_NOFS);
  5573. ordered = btrfs_lookup_ordered_extent(page->mapping->host,
  5574. page_offset(page));
  5575. if (ordered) {
  5576. /*
  5577. * IO on this page will never be started, so we need
  5578. * to account for any ordered extents now
  5579. */
  5580. clear_extent_bit(tree, page_start, page_end,
  5581. EXTENT_DIRTY | EXTENT_DELALLOC |
  5582. EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
  5583. &cached_state, GFP_NOFS);
  5584. /*
  5585. * whoever cleared the private bit is responsible
  5586. * for the finish_ordered_io
  5587. */
  5588. if (TestClearPagePrivate2(page)) {
  5589. btrfs_finish_ordered_io(page->mapping->host,
  5590. page_start, page_end);
  5591. }
  5592. btrfs_put_ordered_extent(ordered);
  5593. cached_state = NULL;
  5594. lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
  5595. GFP_NOFS);
  5596. }
  5597. clear_extent_bit(tree, page_start, page_end,
  5598. EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
  5599. EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS);
  5600. __btrfs_releasepage(page, GFP_NOFS);
  5601. ClearPageChecked(page);
  5602. if (PagePrivate(page)) {
  5603. ClearPagePrivate(page);
  5604. set_page_private(page, 0);
  5605. page_cache_release(page);
  5606. }
  5607. }
  5608. /*
  5609. * btrfs_page_mkwrite() is not allowed to change the file size as it gets
  5610. * called from a page fault handler when a page is first dirtied. Hence we must
  5611. * be careful to check for EOF conditions here. We set the page up correctly
  5612. * for a written page which means we get ENOSPC checking when writing into
  5613. * holes and correct delalloc and unwritten extent mapping on filesystems that
  5614. * support these features.
  5615. *
  5616. * We are not allowed to take the i_mutex here so we have to play games to
  5617. * protect against truncate races as the page could now be beyond EOF. Because
  5618. * vmtruncate() writes the inode size before removing pages, once we have the
  5619. * page lock we can determine safely if the page is beyond EOF. If it is not
  5620. * beyond EOF, then the page is guaranteed safe against truncation until we
  5621. * unlock the page.
  5622. */
  5623. int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  5624. {
  5625. struct page *page = vmf->page;
  5626. struct inode *inode = fdentry(vma->vm_file)->d_inode;
  5627. struct btrfs_root *root = BTRFS_I(inode)->root;
  5628. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  5629. struct btrfs_ordered_extent *ordered;
  5630. struct extent_state *cached_state = NULL;
  5631. char *kaddr;
  5632. unsigned long zero_start;
  5633. loff_t size;
  5634. int ret;
  5635. u64 page_start;
  5636. u64 page_end;
  5637. ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
  5638. if (ret) {
  5639. if (ret == -ENOMEM)
  5640. ret = VM_FAULT_OOM;
  5641. else /* -ENOSPC, -EIO, etc */
  5642. ret = VM_FAULT_SIGBUS;
  5643. goto out;
  5644. }
  5645. ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
  5646. again:
  5647. lock_page(page);
  5648. size = i_size_read(inode);
  5649. page_start = page_offset(page);
  5650. page_end = page_start + PAGE_CACHE_SIZE - 1;
  5651. if ((page->mapping != inode->i_mapping) ||
  5652. (page_start >= size)) {
  5653. /* page got truncated out from underneath us */
  5654. goto out_unlock;
  5655. }
  5656. wait_on_page_writeback(page);
  5657. lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
  5658. GFP_NOFS);
  5659. set_page_extent_mapped(page);
  5660. /*
  5661. * we can't set the delalloc bits if there are pending ordered
  5662. * extents. Drop our locks and wait for them to finish
  5663. */
  5664. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  5665. if (ordered) {
  5666. unlock_extent_cached(io_tree, page_start, page_end,
  5667. &cached_state, GFP_NOFS);
  5668. unlock_page(page);
  5669. btrfs_start_ordered_extent(inode, ordered, 1);
  5670. btrfs_put_ordered_extent(ordered);
  5671. goto again;
  5672. }
  5673. /*
  5674. * XXX - page_mkwrite gets called every time the page is dirtied, even
  5675. * if it was already dirty, so for space accounting reasons we need to
  5676. * clear any delalloc bits for the range we are fixing to save. There
  5677. * is probably a better way to do this, but for now keep consistent with
  5678. * prepare_pages in the normal write path.
  5679. */
  5680. clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
  5681. EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
  5682. 0, 0, &cached_state, GFP_NOFS);
  5683. ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
  5684. &cached_state);
  5685. if (ret) {
  5686. unlock_extent_cached(io_tree, page_start, page_end,
  5687. &cached_state, GFP_NOFS);
  5688. ret = VM_FAULT_SIGBUS;
  5689. goto out_unlock;
  5690. }
  5691. ret = 0;
  5692. /* page is wholly or partially inside EOF */
  5693. if (page_start + PAGE_CACHE_SIZE > size)
  5694. zero_start = size & ~PAGE_CACHE_MASK;
  5695. else
  5696. zero_start = PAGE_CACHE_SIZE;
  5697. if (zero_start != PAGE_CACHE_SIZE) {
  5698. kaddr = kmap(page);
  5699. memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
  5700. flush_dcache_page(page);
  5701. kunmap(page);
  5702. }
  5703. ClearPageChecked(page);
  5704. set_page_dirty(page);
  5705. SetPageUptodate(page);
  5706. BTRFS_I(inode)->last_trans = root->fs_info->generation;
  5707. BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
  5708. unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
  5709. out_unlock:
  5710. if (!ret)
  5711. return VM_FAULT_LOCKED;
  5712. unlock_page(page);
  5713. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  5714. out:
  5715. return ret;
  5716. }
  5717. static int btrfs_truncate(struct inode *inode)
  5718. {
  5719. struct btrfs_root *root = BTRFS_I(inode)->root;
  5720. int ret;
  5721. int err = 0;
  5722. struct btrfs_trans_handle *trans;
  5723. unsigned long nr;
  5724. u64 mask = root->sectorsize - 1;
  5725. ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
  5726. if (ret)
  5727. return ret;
  5728. btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
  5729. btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
  5730. trans = btrfs_start_transaction(root, 5);
  5731. if (IS_ERR(trans))
  5732. return PTR_ERR(trans);
  5733. btrfs_set_trans_block_group(trans, inode);
  5734. ret = btrfs_orphan_add(trans, inode);
  5735. if (ret) {
  5736. btrfs_end_transaction(trans, root);
  5737. return ret;
  5738. }
  5739. nr = trans->blocks_used;
  5740. btrfs_end_transaction(trans, root);
  5741. btrfs_btree_balance_dirty(root, nr);
  5742. /* Now start a transaction for the truncate */
  5743. trans = btrfs_start_transaction(root, 0);
  5744. if (IS_ERR(trans))
  5745. return PTR_ERR(trans);
  5746. btrfs_set_trans_block_group(trans, inode);
  5747. trans->block_rsv = root->orphan_block_rsv;
  5748. /*
  5749. * setattr is responsible for setting the ordered_data_close flag,
  5750. * but that is only tested during the last file release. That
  5751. * could happen well after the next commit, leaving a great big
  5752. * window where new writes may get lost if someone chooses to write
  5753. * to this file after truncating to zero
  5754. *
  5755. * The inode doesn't have any dirty data here, and so if we commit
  5756. * this is a noop. If someone immediately starts writing to the inode
  5757. * it is very likely we'll catch some of their writes in this
  5758. * transaction, and the commit will find this file on the ordered
  5759. * data list with good things to send down.
  5760. *
  5761. * This is a best effort solution, there is still a window where
  5762. * using truncate to replace the contents of the file will
  5763. * end up with a zero length file after a crash.
  5764. */
  5765. if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
  5766. btrfs_add_ordered_operation(trans, root, inode);
  5767. while (1) {
  5768. if (!trans) {
  5769. trans = btrfs_start_transaction(root, 0);
  5770. if (IS_ERR(trans))
  5771. return PTR_ERR(trans);
  5772. btrfs_set_trans_block_group(trans, inode);
  5773. trans->block_rsv = root->orphan_block_rsv;
  5774. }
  5775. ret = btrfs_block_rsv_check(trans, root,
  5776. root->orphan_block_rsv, 0, 5);
  5777. if (ret == -EAGAIN) {
  5778. ret = btrfs_commit_transaction(trans, root);
  5779. if (ret)
  5780. return ret;
  5781. trans = NULL;
  5782. continue;
  5783. } else if (ret) {
  5784. err = ret;
  5785. break;
  5786. }
  5787. ret = btrfs_truncate_inode_items(trans, root, inode,
  5788. inode->i_size,
  5789. BTRFS_EXTENT_DATA_KEY);
  5790. if (ret != -EAGAIN) {
  5791. err = ret;
  5792. break;
  5793. }
  5794. ret = btrfs_update_inode(trans, root, inode);
  5795. if (ret) {
  5796. err = ret;
  5797. break;
  5798. }
  5799. nr = trans->blocks_used;
  5800. btrfs_end_transaction(trans, root);
  5801. trans = NULL;
  5802. btrfs_btree_balance_dirty(root, nr);
  5803. }
  5804. if (ret == 0 && inode->i_nlink > 0) {
  5805. ret = btrfs_orphan_del(trans, inode);
  5806. if (ret)
  5807. err = ret;
  5808. } else if (ret && inode->i_nlink > 0) {
  5809. /*
  5810. * Failed to do the truncate, remove us from the in memory
  5811. * orphan list.
  5812. */
  5813. ret = btrfs_orphan_del(NULL, inode);
  5814. }
  5815. ret = btrfs_update_inode(trans, root, inode);
  5816. if (ret && !err)
  5817. err = ret;
  5818. nr = trans->blocks_used;
  5819. ret = btrfs_end_transaction_throttle(trans, root);
  5820. if (ret && !err)
  5821. err = ret;
  5822. btrfs_btree_balance_dirty(root, nr);
  5823. return err;
  5824. }
  5825. /*
  5826. * create a new subvolume directory/inode (helper for the ioctl).
  5827. */
  5828. int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
  5829. struct btrfs_root *new_root,
  5830. u64 new_dirid, u64 alloc_hint)
  5831. {
  5832. struct inode *inode;
  5833. int err;
  5834. u64 index = 0;
  5835. inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
  5836. new_dirid, alloc_hint, S_IFDIR | 0700, &index);
  5837. if (IS_ERR(inode))
  5838. return PTR_ERR(inode);
  5839. inode->i_op = &btrfs_dir_inode_operations;
  5840. inode->i_fop = &btrfs_dir_file_operations;
  5841. inode->i_nlink = 1;
  5842. btrfs_i_size_write(inode, 0);
  5843. err = btrfs_update_inode(trans, new_root, inode);
  5844. BUG_ON(err);
  5845. iput(inode);
  5846. return 0;
  5847. }
  5848. /* helper function for file defrag and space balancing. This
  5849. * forces readahead on a given range of bytes in an inode
  5850. */
  5851. unsigned long btrfs_force_ra(struct address_space *mapping,
  5852. struct file_ra_state *ra, struct file *file,
  5853. pgoff_t offset, pgoff_t last_index)
  5854. {
  5855. pgoff_t req_size = last_index - offset + 1;
  5856. page_cache_sync_readahead(mapping, ra, file, offset, req_size);
  5857. return offset + req_size;
  5858. }
  5859. struct inode *btrfs_alloc_inode(struct super_block *sb)
  5860. {
  5861. struct btrfs_inode *ei;
  5862. struct inode *inode;
  5863. ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
  5864. if (!ei)
  5865. return NULL;
  5866. ei->root = NULL;
  5867. ei->space_info = NULL;
  5868. ei->generation = 0;
  5869. ei->sequence = 0;
  5870. ei->last_trans = 0;
  5871. ei->last_sub_trans = 0;
  5872. ei->logged_trans = 0;
  5873. ei->delalloc_bytes = 0;
  5874. ei->reserved_bytes = 0;
  5875. ei->disk_i_size = 0;
  5876. ei->flags = 0;
  5877. ei->index_cnt = (u64)-1;
  5878. ei->last_unlink_trans = 0;
  5879. atomic_set(&ei->outstanding_extents, 0);
  5880. atomic_set(&ei->reserved_extents, 0);
  5881. ei->ordered_data_close = 0;
  5882. ei->orphan_meta_reserved = 0;
  5883. ei->dummy_inode = 0;
  5884. ei->force_compress = BTRFS_COMPRESS_NONE;
  5885. inode = &ei->vfs_inode;
  5886. extent_map_tree_init(&ei->extent_tree, GFP_NOFS);
  5887. extent_io_tree_init(&ei->io_tree, &inode->i_data, GFP_NOFS);
  5888. extent_io_tree_init(&ei->io_failure_tree, &inode->i_data, GFP_NOFS);
  5889. mutex_init(&ei->log_mutex);
  5890. btrfs_ordered_inode_tree_init(&ei->ordered_tree);
  5891. INIT_LIST_HEAD(&ei->i_orphan);
  5892. INIT_LIST_HEAD(&ei->delalloc_inodes);
  5893. INIT_LIST_HEAD(&ei->ordered_operations);
  5894. RB_CLEAR_NODE(&ei->rb_node);
  5895. return inode;
  5896. }
  5897. static void btrfs_i_callback(struct rcu_head *head)
  5898. {
  5899. struct inode *inode = container_of(head, struct inode, i_rcu);
  5900. INIT_LIST_HEAD(&inode->i_dentry);
  5901. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  5902. }
  5903. void btrfs_destroy_inode(struct inode *inode)
  5904. {
  5905. struct btrfs_ordered_extent *ordered;
  5906. struct btrfs_root *root = BTRFS_I(inode)->root;
  5907. WARN_ON(!list_empty(&inode->i_dentry));
  5908. WARN_ON(inode->i_data.nrpages);
  5909. WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents));
  5910. WARN_ON(atomic_read(&BTRFS_I(inode)->reserved_extents));
  5911. /*
  5912. * This can happen where we create an inode, but somebody else also
  5913. * created the same inode and we need to destroy the one we already
  5914. * created.
  5915. */
  5916. if (!root)
  5917. goto free;
  5918. /*
  5919. * Make sure we're properly removed from the ordered operation
  5920. * lists.
  5921. */
  5922. smp_mb();
  5923. if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
  5924. spin_lock(&root->fs_info->ordered_extent_lock);
  5925. list_del_init(&BTRFS_I(inode)->ordered_operations);
  5926. spin_unlock(&root->fs_info->ordered_extent_lock);
  5927. }
  5928. if (root == root->fs_info->tree_root) {
  5929. struct btrfs_block_group_cache *block_group;
  5930. block_group = btrfs_lookup_block_group(root->fs_info,
  5931. BTRFS_I(inode)->block_group);
  5932. if (block_group && block_group->inode == inode) {
  5933. spin_lock(&block_group->lock);
  5934. block_group->inode = NULL;
  5935. spin_unlock(&block_group->lock);
  5936. btrfs_put_block_group(block_group);
  5937. } else if (block_group) {
  5938. btrfs_put_block_group(block_group);
  5939. }
  5940. }
  5941. spin_lock(&root->orphan_lock);
  5942. if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
  5943. printk(KERN_INFO "BTRFS: inode %lu still on the orphan list\n",
  5944. inode->i_ino);
  5945. list_del_init(&BTRFS_I(inode)->i_orphan);
  5946. }
  5947. spin_unlock(&root->orphan_lock);
  5948. while (1) {
  5949. ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
  5950. if (!ordered)
  5951. break;
  5952. else {
  5953. printk(KERN_ERR "btrfs found ordered "
  5954. "extent %llu %llu on inode cleanup\n",
  5955. (unsigned long long)ordered->file_offset,
  5956. (unsigned long long)ordered->len);
  5957. btrfs_remove_ordered_extent(inode, ordered);
  5958. btrfs_put_ordered_extent(ordered);
  5959. btrfs_put_ordered_extent(ordered);
  5960. }
  5961. }
  5962. inode_tree_del(inode);
  5963. btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
  5964. free:
  5965. call_rcu(&inode->i_rcu, btrfs_i_callback);
  5966. }
  5967. int btrfs_drop_inode(struct inode *inode)
  5968. {
  5969. struct btrfs_root *root = BTRFS_I(inode)->root;
  5970. if (btrfs_root_refs(&root->root_item) == 0 &&
  5971. root != root->fs_info->tree_root)
  5972. return 1;
  5973. else
  5974. return generic_drop_inode(inode);
  5975. }
  5976. static void init_once(void *foo)
  5977. {
  5978. struct btrfs_inode *ei = (struct btrfs_inode *) foo;
  5979. inode_init_once(&ei->vfs_inode);
  5980. }
  5981. void btrfs_destroy_cachep(void)
  5982. {
  5983. if (btrfs_inode_cachep)
  5984. kmem_cache_destroy(btrfs_inode_cachep);
  5985. if (btrfs_trans_handle_cachep)
  5986. kmem_cache_destroy(btrfs_trans_handle_cachep);
  5987. if (btrfs_transaction_cachep)
  5988. kmem_cache_destroy(btrfs_transaction_cachep);
  5989. if (btrfs_path_cachep)
  5990. kmem_cache_destroy(btrfs_path_cachep);
  5991. if (btrfs_free_space_cachep)
  5992. kmem_cache_destroy(btrfs_free_space_cachep);
  5993. }
  5994. int btrfs_init_cachep(void)
  5995. {
  5996. btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
  5997. sizeof(struct btrfs_inode), 0,
  5998. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
  5999. if (!btrfs_inode_cachep)
  6000. goto fail;
  6001. btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
  6002. sizeof(struct btrfs_trans_handle), 0,
  6003. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  6004. if (!btrfs_trans_handle_cachep)
  6005. goto fail;
  6006. btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
  6007. sizeof(struct btrfs_transaction), 0,
  6008. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  6009. if (!btrfs_transaction_cachep)
  6010. goto fail;
  6011. btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
  6012. sizeof(struct btrfs_path), 0,
  6013. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  6014. if (!btrfs_path_cachep)
  6015. goto fail;
  6016. btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space_cache",
  6017. sizeof(struct btrfs_free_space), 0,
  6018. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  6019. if (!btrfs_free_space_cachep)
  6020. goto fail;
  6021. return 0;
  6022. fail:
  6023. btrfs_destroy_cachep();
  6024. return -ENOMEM;
  6025. }
  6026. static int btrfs_getattr(struct vfsmount *mnt,
  6027. struct dentry *dentry, struct kstat *stat)
  6028. {
  6029. struct inode *inode = dentry->d_inode;
  6030. generic_fillattr(inode, stat);
  6031. stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
  6032. stat->blksize = PAGE_CACHE_SIZE;
  6033. stat->blocks = (inode_get_bytes(inode) +
  6034. BTRFS_I(inode)->delalloc_bytes) >> 9;
  6035. return 0;
  6036. }
  6037. static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  6038. struct inode *new_dir, struct dentry *new_dentry)
  6039. {
  6040. struct btrfs_trans_handle *trans;
  6041. struct btrfs_root *root = BTRFS_I(old_dir)->root;
  6042. struct btrfs_root *dest = BTRFS_I(new_dir)->root;
  6043. struct inode *new_inode = new_dentry->d_inode;
  6044. struct inode *old_inode = old_dentry->d_inode;
  6045. struct timespec ctime = CURRENT_TIME;
  6046. u64 index = 0;
  6047. u64 root_objectid;
  6048. int ret;
  6049. if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
  6050. return -EPERM;
  6051. /* we only allow rename subvolume link between subvolumes */
  6052. if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
  6053. return -EXDEV;
  6054. if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
  6055. (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID))
  6056. return -ENOTEMPTY;
  6057. if (S_ISDIR(old_inode->i_mode) && new_inode &&
  6058. new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
  6059. return -ENOTEMPTY;
  6060. /*
  6061. * we're using rename to replace one file with another.
  6062. * and the replacement file is large. Start IO on it now so
  6063. * we don't add too much work to the end of the transaction
  6064. */
  6065. if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
  6066. old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
  6067. filemap_flush(old_inode->i_mapping);
  6068. /* close the racy window with snapshot create/destroy ioctl */
  6069. if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
  6070. down_read(&root->fs_info->subvol_sem);
  6071. /*
  6072. * We want to reserve the absolute worst case amount of items. So if
  6073. * both inodes are subvols and we need to unlink them then that would
  6074. * require 4 item modifications, but if they are both normal inodes it
  6075. * would require 5 item modifications, so we'll assume their normal
  6076. * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
  6077. * should cover the worst case number of items we'll modify.
  6078. */
  6079. trans = btrfs_start_transaction(root, 20);
  6080. if (IS_ERR(trans))
  6081. return PTR_ERR(trans);
  6082. btrfs_set_trans_block_group(trans, new_dir);
  6083. if (dest != root)
  6084. btrfs_record_root_in_trans(trans, dest);
  6085. ret = btrfs_set_inode_index(new_dir, &index);
  6086. if (ret)
  6087. goto out_fail;
  6088. if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  6089. /* force full log commit if subvolume involved. */
  6090. root->fs_info->last_trans_log_full_commit = trans->transid;
  6091. } else {
  6092. ret = btrfs_insert_inode_ref(trans, dest,
  6093. new_dentry->d_name.name,
  6094. new_dentry->d_name.len,
  6095. old_inode->i_ino,
  6096. new_dir->i_ino, index);
  6097. if (ret)
  6098. goto out_fail;
  6099. /*
  6100. * this is an ugly little race, but the rename is required
  6101. * to make sure that if we crash, the inode is either at the
  6102. * old name or the new one. pinning the log transaction lets
  6103. * us make sure we don't allow a log commit to come in after
  6104. * we unlink the name but before we add the new name back in.
  6105. */
  6106. btrfs_pin_log_trans(root);
  6107. }
  6108. /*
  6109. * make sure the inode gets flushed if it is replacing
  6110. * something.
  6111. */
  6112. if (new_inode && new_inode->i_size &&
  6113. old_inode && S_ISREG(old_inode->i_mode)) {
  6114. btrfs_add_ordered_operation(trans, root, old_inode);
  6115. }
  6116. old_dir->i_ctime = old_dir->i_mtime = ctime;
  6117. new_dir->i_ctime = new_dir->i_mtime = ctime;
  6118. old_inode->i_ctime = ctime;
  6119. if (old_dentry->d_parent != new_dentry->d_parent)
  6120. btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
  6121. if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  6122. root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
  6123. ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
  6124. old_dentry->d_name.name,
  6125. old_dentry->d_name.len);
  6126. } else {
  6127. btrfs_inc_nlink(old_dentry->d_inode);
  6128. ret = btrfs_unlink_inode(trans, root, old_dir,
  6129. old_dentry->d_inode,
  6130. old_dentry->d_name.name,
  6131. old_dentry->d_name.len);
  6132. }
  6133. BUG_ON(ret);
  6134. if (new_inode) {
  6135. new_inode->i_ctime = CURRENT_TIME;
  6136. if (unlikely(new_inode->i_ino ==
  6137. BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
  6138. root_objectid = BTRFS_I(new_inode)->location.objectid;
  6139. ret = btrfs_unlink_subvol(trans, dest, new_dir,
  6140. root_objectid,
  6141. new_dentry->d_name.name,
  6142. new_dentry->d_name.len);
  6143. BUG_ON(new_inode->i_nlink == 0);
  6144. } else {
  6145. ret = btrfs_unlink_inode(trans, dest, new_dir,
  6146. new_dentry->d_inode,
  6147. new_dentry->d_name.name,
  6148. new_dentry->d_name.len);
  6149. }
  6150. BUG_ON(ret);
  6151. if (new_inode->i_nlink == 0) {
  6152. ret = btrfs_orphan_add(trans, new_dentry->d_inode);
  6153. BUG_ON(ret);
  6154. }
  6155. }
  6156. ret = btrfs_add_link(trans, new_dir, old_inode,
  6157. new_dentry->d_name.name,
  6158. new_dentry->d_name.len, 0, index);
  6159. BUG_ON(ret);
  6160. if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
  6161. struct dentry *parent = dget_parent(new_dentry);
  6162. btrfs_log_new_name(trans, old_inode, old_dir, parent);
  6163. dput(parent);
  6164. btrfs_end_log_trans(root);
  6165. }
  6166. out_fail:
  6167. btrfs_end_transaction_throttle(trans, root);
  6168. if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
  6169. up_read(&root->fs_info->subvol_sem);
  6170. return ret;
  6171. }
  6172. /*
  6173. * some fairly slow code that needs optimization. This walks the list
  6174. * of all the inodes with pending delalloc and forces them to disk.
  6175. */
  6176. int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
  6177. {
  6178. struct list_head *head = &root->fs_info->delalloc_inodes;
  6179. struct btrfs_inode *binode;
  6180. struct inode *inode;
  6181. if (root->fs_info->sb->s_flags & MS_RDONLY)
  6182. return -EROFS;
  6183. spin_lock(&root->fs_info->delalloc_lock);
  6184. while (!list_empty(head)) {
  6185. binode = list_entry(head->next, struct btrfs_inode,
  6186. delalloc_inodes);
  6187. inode = igrab(&binode->vfs_inode);
  6188. if (!inode)
  6189. list_del_init(&binode->delalloc_inodes);
  6190. spin_unlock(&root->fs_info->delalloc_lock);
  6191. if (inode) {
  6192. filemap_flush(inode->i_mapping);
  6193. if (delay_iput)
  6194. btrfs_add_delayed_iput(inode);
  6195. else
  6196. iput(inode);
  6197. }
  6198. cond_resched();
  6199. spin_lock(&root->fs_info->delalloc_lock);
  6200. }
  6201. spin_unlock(&root->fs_info->delalloc_lock);
  6202. /* the filemap_flush will queue IO into the worker threads, but
  6203. * we have to make sure the IO is actually started and that
  6204. * ordered extents get created before we return
  6205. */
  6206. atomic_inc(&root->fs_info->async_submit_draining);
  6207. while (atomic_read(&root->fs_info->nr_async_submits) ||
  6208. atomic_read(&root->fs_info->async_delalloc_pages)) {
  6209. wait_event(root->fs_info->async_submit_wait,
  6210. (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
  6211. atomic_read(&root->fs_info->async_delalloc_pages) == 0));
  6212. }
  6213. atomic_dec(&root->fs_info->async_submit_draining);
  6214. return 0;
  6215. }
  6216. int btrfs_start_one_delalloc_inode(struct btrfs_root *root, int delay_iput,
  6217. int sync)
  6218. {
  6219. struct btrfs_inode *binode;
  6220. struct inode *inode = NULL;
  6221. spin_lock(&root->fs_info->delalloc_lock);
  6222. while (!list_empty(&root->fs_info->delalloc_inodes)) {
  6223. binode = list_entry(root->fs_info->delalloc_inodes.next,
  6224. struct btrfs_inode, delalloc_inodes);
  6225. inode = igrab(&binode->vfs_inode);
  6226. if (inode) {
  6227. list_move_tail(&binode->delalloc_inodes,
  6228. &root->fs_info->delalloc_inodes);
  6229. break;
  6230. }
  6231. list_del_init(&binode->delalloc_inodes);
  6232. cond_resched_lock(&root->fs_info->delalloc_lock);
  6233. }
  6234. spin_unlock(&root->fs_info->delalloc_lock);
  6235. if (inode) {
  6236. if (sync) {
  6237. filemap_write_and_wait(inode->i_mapping);
  6238. /*
  6239. * We have to do this because compression doesn't
  6240. * actually set PG_writeback until it submits the pages
  6241. * for IO, which happens in an async thread, so we could
  6242. * race and not actually wait for any writeback pages
  6243. * because they've not been submitted yet. Technically
  6244. * this could still be the case for the ordered stuff
  6245. * since the async thread may not have started to do its
  6246. * work yet. If this becomes the case then we need to
  6247. * figure out a way to make sure that in writepage we
  6248. * wait for any async pages to be submitted before
  6249. * returning so that fdatawait does what its supposed to
  6250. * do.
  6251. */
  6252. btrfs_wait_ordered_range(inode, 0, (u64)-1);
  6253. } else {
  6254. filemap_flush(inode->i_mapping);
  6255. }
  6256. if (delay_iput)
  6257. btrfs_add_delayed_iput(inode);
  6258. else
  6259. iput(inode);
  6260. return 1;
  6261. }
  6262. return 0;
  6263. }
  6264. static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
  6265. const char *symname)
  6266. {
  6267. struct btrfs_trans_handle *trans;
  6268. struct btrfs_root *root = BTRFS_I(dir)->root;
  6269. struct btrfs_path *path;
  6270. struct btrfs_key key;
  6271. struct inode *inode = NULL;
  6272. int err;
  6273. int drop_inode = 0;
  6274. u64 objectid;
  6275. u64 index = 0 ;
  6276. int name_len;
  6277. int datasize;
  6278. unsigned long ptr;
  6279. struct btrfs_file_extent_item *ei;
  6280. struct extent_buffer *leaf;
  6281. unsigned long nr = 0;
  6282. name_len = strlen(symname) + 1;
  6283. if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
  6284. return -ENAMETOOLONG;
  6285. err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
  6286. if (err)
  6287. return err;
  6288. /*
  6289. * 2 items for inode item and ref
  6290. * 2 items for dir items
  6291. * 1 item for xattr if selinux is on
  6292. */
  6293. trans = btrfs_start_transaction(root, 5);
  6294. if (IS_ERR(trans))
  6295. return PTR_ERR(trans);
  6296. btrfs_set_trans_block_group(trans, dir);
  6297. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  6298. dentry->d_name.len, dir->i_ino, objectid,
  6299. BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
  6300. &index);
  6301. err = PTR_ERR(inode);
  6302. if (IS_ERR(inode))
  6303. goto out_unlock;
  6304. err = btrfs_init_inode_security(trans, inode, dir);
  6305. if (err) {
  6306. drop_inode = 1;
  6307. goto out_unlock;
  6308. }
  6309. btrfs_set_trans_block_group(trans, inode);
  6310. err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
  6311. if (err)
  6312. drop_inode = 1;
  6313. else {
  6314. inode->i_mapping->a_ops = &btrfs_aops;
  6315. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  6316. inode->i_fop = &btrfs_file_operations;
  6317. inode->i_op = &btrfs_file_inode_operations;
  6318. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  6319. }
  6320. btrfs_update_inode_block_group(trans, inode);
  6321. btrfs_update_inode_block_group(trans, dir);
  6322. if (drop_inode)
  6323. goto out_unlock;
  6324. path = btrfs_alloc_path();
  6325. BUG_ON(!path);
  6326. key.objectid = inode->i_ino;
  6327. key.offset = 0;
  6328. btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
  6329. datasize = btrfs_file_extent_calc_inline_size(name_len);
  6330. err = btrfs_insert_empty_item(trans, root, path, &key,
  6331. datasize);
  6332. if (err) {
  6333. drop_inode = 1;
  6334. goto out_unlock;
  6335. }
  6336. leaf = path->nodes[0];
  6337. ei = btrfs_item_ptr(leaf, path->slots[0],
  6338. struct btrfs_file_extent_item);
  6339. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  6340. btrfs_set_file_extent_type(leaf, ei,
  6341. BTRFS_FILE_EXTENT_INLINE);
  6342. btrfs_set_file_extent_encryption(leaf, ei, 0);
  6343. btrfs_set_file_extent_compression(leaf, ei, 0);
  6344. btrfs_set_file_extent_other_encoding(leaf, ei, 0);
  6345. btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
  6346. ptr = btrfs_file_extent_inline_start(ei);
  6347. write_extent_buffer(leaf, symname, ptr, name_len);
  6348. btrfs_mark_buffer_dirty(leaf);
  6349. btrfs_free_path(path);
  6350. inode->i_op = &btrfs_symlink_inode_operations;
  6351. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  6352. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  6353. inode_set_bytes(inode, name_len);
  6354. btrfs_i_size_write(inode, name_len - 1);
  6355. err = btrfs_update_inode(trans, root, inode);
  6356. if (err)
  6357. drop_inode = 1;
  6358. out_unlock:
  6359. nr = trans->blocks_used;
  6360. btrfs_end_transaction_throttle(trans, root);
  6361. if (drop_inode) {
  6362. inode_dec_link_count(inode);
  6363. iput(inode);
  6364. }
  6365. btrfs_btree_balance_dirty(root, nr);
  6366. return err;
  6367. }
  6368. static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
  6369. u64 start, u64 num_bytes, u64 min_size,
  6370. loff_t actual_len, u64 *alloc_hint,
  6371. struct btrfs_trans_handle *trans)
  6372. {
  6373. struct btrfs_root *root = BTRFS_I(inode)->root;
  6374. struct btrfs_key ins;
  6375. u64 cur_offset = start;
  6376. u64 i_size;
  6377. int ret = 0;
  6378. bool own_trans = true;
  6379. if (trans)
  6380. own_trans = false;
  6381. while (num_bytes > 0) {
  6382. if (own_trans) {
  6383. trans = btrfs_start_transaction(root, 3);
  6384. if (IS_ERR(trans)) {
  6385. ret = PTR_ERR(trans);
  6386. break;
  6387. }
  6388. }
  6389. ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
  6390. 0, *alloc_hint, (u64)-1, &ins, 1);
  6391. if (ret) {
  6392. if (own_trans)
  6393. btrfs_end_transaction(trans, root);
  6394. break;
  6395. }
  6396. ret = insert_reserved_file_extent(trans, inode,
  6397. cur_offset, ins.objectid,
  6398. ins.offset, ins.offset,
  6399. ins.offset, 0, 0, 0,
  6400. BTRFS_FILE_EXTENT_PREALLOC);
  6401. BUG_ON(ret);
  6402. btrfs_drop_extent_cache(inode, cur_offset,
  6403. cur_offset + ins.offset -1, 0);
  6404. num_bytes -= ins.offset;
  6405. cur_offset += ins.offset;
  6406. *alloc_hint = ins.objectid + ins.offset;
  6407. inode->i_ctime = CURRENT_TIME;
  6408. BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
  6409. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  6410. (actual_len > inode->i_size) &&
  6411. (cur_offset > inode->i_size)) {
  6412. if (cur_offset > actual_len)
  6413. i_size = actual_len;
  6414. else
  6415. i_size = cur_offset;
  6416. i_size_write(inode, i_size);
  6417. btrfs_ordered_update_i_size(inode, i_size, NULL);
  6418. }
  6419. ret = btrfs_update_inode(trans, root, inode);
  6420. BUG_ON(ret);
  6421. if (own_trans)
  6422. btrfs_end_transaction(trans, root);
  6423. }
  6424. return ret;
  6425. }
  6426. int btrfs_prealloc_file_range(struct inode *inode, int mode,
  6427. u64 start, u64 num_bytes, u64 min_size,
  6428. loff_t actual_len, u64 *alloc_hint)
  6429. {
  6430. return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
  6431. min_size, actual_len, alloc_hint,
  6432. NULL);
  6433. }
  6434. int btrfs_prealloc_file_range_trans(struct inode *inode,
  6435. struct btrfs_trans_handle *trans, int mode,
  6436. u64 start, u64 num_bytes, u64 min_size,
  6437. loff_t actual_len, u64 *alloc_hint)
  6438. {
  6439. return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
  6440. min_size, actual_len, alloc_hint, trans);
  6441. }
  6442. static int btrfs_set_page_dirty(struct page *page)
  6443. {
  6444. return __set_page_dirty_nobuffers(page);
  6445. }
  6446. static int btrfs_permission(struct inode *inode, int mask, unsigned int flags)
  6447. {
  6448. struct btrfs_root *root = BTRFS_I(inode)->root;
  6449. if (btrfs_root_readonly(root) && (mask & MAY_WRITE))
  6450. return -EROFS;
  6451. if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
  6452. return -EACCES;
  6453. return generic_permission(inode, mask, flags, btrfs_check_acl);
  6454. }
  6455. static const struct inode_operations btrfs_dir_inode_operations = {
  6456. .getattr = btrfs_getattr,
  6457. .lookup = btrfs_lookup,
  6458. .create = btrfs_create,
  6459. .unlink = btrfs_unlink,
  6460. .link = btrfs_link,
  6461. .mkdir = btrfs_mkdir,
  6462. .rmdir = btrfs_rmdir,
  6463. .rename = btrfs_rename,
  6464. .symlink = btrfs_symlink,
  6465. .setattr = btrfs_setattr,
  6466. .mknod = btrfs_mknod,
  6467. .setxattr = btrfs_setxattr,
  6468. .getxattr = btrfs_getxattr,
  6469. .listxattr = btrfs_listxattr,
  6470. .removexattr = btrfs_removexattr,
  6471. .permission = btrfs_permission,
  6472. };
  6473. static const struct inode_operations btrfs_dir_ro_inode_operations = {
  6474. .lookup = btrfs_lookup,
  6475. .permission = btrfs_permission,
  6476. };
  6477. static const struct file_operations btrfs_dir_file_operations = {
  6478. .llseek = generic_file_llseek,
  6479. .read = generic_read_dir,
  6480. .readdir = btrfs_real_readdir,
  6481. .unlocked_ioctl = btrfs_ioctl,
  6482. #ifdef CONFIG_COMPAT
  6483. .compat_ioctl = btrfs_ioctl,
  6484. #endif
  6485. .release = btrfs_release_file,
  6486. .fsync = btrfs_sync_file,
  6487. };
  6488. static struct extent_io_ops btrfs_extent_io_ops = {
  6489. .fill_delalloc = run_delalloc_range,
  6490. .submit_bio_hook = btrfs_submit_bio_hook,
  6491. .merge_bio_hook = btrfs_merge_bio_hook,
  6492. .readpage_end_io_hook = btrfs_readpage_end_io_hook,
  6493. .writepage_end_io_hook = btrfs_writepage_end_io_hook,
  6494. .writepage_start_hook = btrfs_writepage_start_hook,
  6495. .readpage_io_failed_hook = btrfs_io_failed_hook,
  6496. .set_bit_hook = btrfs_set_bit_hook,
  6497. .clear_bit_hook = btrfs_clear_bit_hook,
  6498. .merge_extent_hook = btrfs_merge_extent_hook,
  6499. .split_extent_hook = btrfs_split_extent_hook,
  6500. };
  6501. /*
  6502. * btrfs doesn't support the bmap operation because swapfiles
  6503. * use bmap to make a mapping of extents in the file. They assume
  6504. * these extents won't change over the life of the file and they
  6505. * use the bmap result to do IO directly to the drive.
  6506. *
  6507. * the btrfs bmap call would return logical addresses that aren't
  6508. * suitable for IO and they also will change frequently as COW
  6509. * operations happen. So, swapfile + btrfs == corruption.
  6510. *
  6511. * For now we're avoiding this by dropping bmap.
  6512. */
  6513. static const struct address_space_operations btrfs_aops = {
  6514. .readpage = btrfs_readpage,
  6515. .writepage = btrfs_writepage,
  6516. .writepages = btrfs_writepages,
  6517. .readpages = btrfs_readpages,
  6518. .sync_page = block_sync_page,
  6519. .direct_IO = btrfs_direct_IO,
  6520. .invalidatepage = btrfs_invalidatepage,
  6521. .releasepage = btrfs_releasepage,
  6522. .set_page_dirty = btrfs_set_page_dirty,
  6523. .error_remove_page = generic_error_remove_page,
  6524. };
  6525. static const struct address_space_operations btrfs_symlink_aops = {
  6526. .readpage = btrfs_readpage,
  6527. .writepage = btrfs_writepage,
  6528. .invalidatepage = btrfs_invalidatepage,
  6529. .releasepage = btrfs_releasepage,
  6530. };
  6531. static const struct inode_operations btrfs_file_inode_operations = {
  6532. .getattr = btrfs_getattr,
  6533. .setattr = btrfs_setattr,
  6534. .setxattr = btrfs_setxattr,
  6535. .getxattr = btrfs_getxattr,
  6536. .listxattr = btrfs_listxattr,
  6537. .removexattr = btrfs_removexattr,
  6538. .permission = btrfs_permission,
  6539. .fiemap = btrfs_fiemap,
  6540. };
  6541. static const struct inode_operations btrfs_special_inode_operations = {
  6542. .getattr = btrfs_getattr,
  6543. .setattr = btrfs_setattr,
  6544. .permission = btrfs_permission,
  6545. .setxattr = btrfs_setxattr,
  6546. .getxattr = btrfs_getxattr,
  6547. .listxattr = btrfs_listxattr,
  6548. .removexattr = btrfs_removexattr,
  6549. };
  6550. static const struct inode_operations btrfs_symlink_inode_operations = {
  6551. .readlink = generic_readlink,
  6552. .follow_link = page_follow_link_light,
  6553. .put_link = page_put_link,
  6554. .getattr = btrfs_getattr,
  6555. .permission = btrfs_permission,
  6556. .setxattr = btrfs_setxattr,
  6557. .getxattr = btrfs_getxattr,
  6558. .listxattr = btrfs_listxattr,
  6559. .removexattr = btrfs_removexattr,
  6560. };
  6561. const struct dentry_operations btrfs_dentry_operations = {
  6562. .d_delete = btrfs_dentry_delete,
  6563. };