inode.c 197 KB

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