inode.c 198 KB

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