inode.c 193 KB

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