inode.c 200 KB

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