inode.c 198 KB

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