inode.c 199 KB

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