inode.c 200 KB

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