inode.c 201 KB

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