inode.c 230 KB

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