inode.c 200 KB

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