inode.c 197 KB

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