inode.c 230 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769
  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. if (merge) {
  2054. struct btrfs_file_extent_item *fi;
  2055. u64 extent_len;
  2056. struct btrfs_key found_key;
  2057. ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
  2058. if (ret < 0)
  2059. goto out_free_path;
  2060. path->slots[0]--;
  2061. leaf = path->nodes[0];
  2062. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2063. fi = btrfs_item_ptr(leaf, path->slots[0],
  2064. struct btrfs_file_extent_item);
  2065. extent_len = btrfs_file_extent_num_bytes(leaf, fi);
  2066. if (relink_is_mergable(leaf, fi, new->bytenr) &&
  2067. extent_len + found_key.offset == start) {
  2068. btrfs_set_file_extent_num_bytes(leaf, fi,
  2069. extent_len + len);
  2070. btrfs_mark_buffer_dirty(leaf);
  2071. inode_add_bytes(inode, len);
  2072. ret = 1;
  2073. goto out_free_path;
  2074. } else {
  2075. merge = false;
  2076. btrfs_release_path(path);
  2077. goto again;
  2078. }
  2079. }
  2080. ret = btrfs_insert_empty_item(trans, root, path, &key,
  2081. sizeof(*extent));
  2082. if (ret) {
  2083. btrfs_abort_transaction(trans, root, ret);
  2084. goto out_free_path;
  2085. }
  2086. leaf = path->nodes[0];
  2087. item = btrfs_item_ptr(leaf, path->slots[0],
  2088. struct btrfs_file_extent_item);
  2089. btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
  2090. btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
  2091. btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
  2092. btrfs_set_file_extent_num_bytes(leaf, item, len);
  2093. btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
  2094. btrfs_set_file_extent_generation(leaf, item, trans->transid);
  2095. btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
  2096. btrfs_set_file_extent_compression(leaf, item, new->compress_type);
  2097. btrfs_set_file_extent_encryption(leaf, item, 0);
  2098. btrfs_set_file_extent_other_encoding(leaf, item, 0);
  2099. btrfs_mark_buffer_dirty(leaf);
  2100. inode_add_bytes(inode, len);
  2101. ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
  2102. new->disk_len, 0,
  2103. backref->root_id, backref->inum,
  2104. new->file_pos, 0); /* start - extent_offset */
  2105. if (ret) {
  2106. btrfs_abort_transaction(trans, root, ret);
  2107. goto out_free_path;
  2108. }
  2109. ret = 1;
  2110. out_free_path:
  2111. btrfs_release_path(path);
  2112. btrfs_end_transaction(trans, root);
  2113. out_unlock:
  2114. unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
  2115. &cached, GFP_NOFS);
  2116. iput(inode);
  2117. return ret;
  2118. }
  2119. static void relink_file_extents(struct new_sa_defrag_extent *new)
  2120. {
  2121. struct btrfs_path *path;
  2122. struct old_sa_defrag_extent *old, *tmp;
  2123. struct sa_defrag_extent_backref *backref;
  2124. struct sa_defrag_extent_backref *prev = NULL;
  2125. struct inode *inode;
  2126. struct btrfs_root *root;
  2127. struct rb_node *node;
  2128. int ret;
  2129. inode = new->inode;
  2130. root = BTRFS_I(inode)->root;
  2131. path = btrfs_alloc_path();
  2132. if (!path)
  2133. return;
  2134. if (!record_extent_backrefs(path, new)) {
  2135. btrfs_free_path(path);
  2136. goto out;
  2137. }
  2138. btrfs_release_path(path);
  2139. while (1) {
  2140. node = rb_first(&new->root);
  2141. if (!node)
  2142. break;
  2143. rb_erase(node, &new->root);
  2144. backref = rb_entry(node, struct sa_defrag_extent_backref, node);
  2145. ret = relink_extent_backref(path, prev, backref);
  2146. WARN_ON(ret < 0);
  2147. kfree(prev);
  2148. if (ret == 1)
  2149. prev = backref;
  2150. else
  2151. prev = NULL;
  2152. cond_resched();
  2153. }
  2154. kfree(prev);
  2155. btrfs_free_path(path);
  2156. list_for_each_entry_safe(old, tmp, &new->head, list) {
  2157. list_del(&old->list);
  2158. kfree(old);
  2159. }
  2160. out:
  2161. atomic_dec(&root->fs_info->defrag_running);
  2162. wake_up(&root->fs_info->transaction_wait);
  2163. kfree(new);
  2164. }
  2165. static struct new_sa_defrag_extent *
  2166. record_old_file_extents(struct inode *inode,
  2167. struct btrfs_ordered_extent *ordered)
  2168. {
  2169. struct btrfs_root *root = BTRFS_I(inode)->root;
  2170. struct btrfs_path *path;
  2171. struct btrfs_key key;
  2172. struct old_sa_defrag_extent *old, *tmp;
  2173. struct new_sa_defrag_extent *new;
  2174. int ret;
  2175. new = kmalloc(sizeof(*new), GFP_NOFS);
  2176. if (!new)
  2177. return NULL;
  2178. new->inode = inode;
  2179. new->file_pos = ordered->file_offset;
  2180. new->len = ordered->len;
  2181. new->bytenr = ordered->start;
  2182. new->disk_len = ordered->disk_len;
  2183. new->compress_type = ordered->compress_type;
  2184. new->root = RB_ROOT;
  2185. INIT_LIST_HEAD(&new->head);
  2186. path = btrfs_alloc_path();
  2187. if (!path)
  2188. goto out_kfree;
  2189. key.objectid = btrfs_ino(inode);
  2190. key.type = BTRFS_EXTENT_DATA_KEY;
  2191. key.offset = new->file_pos;
  2192. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2193. if (ret < 0)
  2194. goto out_free_path;
  2195. if (ret > 0 && path->slots[0] > 0)
  2196. path->slots[0]--;
  2197. /* find out all the old extents for the file range */
  2198. while (1) {
  2199. struct btrfs_file_extent_item *extent;
  2200. struct extent_buffer *l;
  2201. int slot;
  2202. u64 num_bytes;
  2203. u64 offset;
  2204. u64 end;
  2205. u64 disk_bytenr;
  2206. u64 extent_offset;
  2207. l = path->nodes[0];
  2208. slot = path->slots[0];
  2209. if (slot >= btrfs_header_nritems(l)) {
  2210. ret = btrfs_next_leaf(root, path);
  2211. if (ret < 0)
  2212. goto out_free_list;
  2213. else if (ret > 0)
  2214. break;
  2215. continue;
  2216. }
  2217. btrfs_item_key_to_cpu(l, &key, slot);
  2218. if (key.objectid != btrfs_ino(inode))
  2219. break;
  2220. if (key.type != BTRFS_EXTENT_DATA_KEY)
  2221. break;
  2222. if (key.offset >= new->file_pos + new->len)
  2223. break;
  2224. extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
  2225. num_bytes = btrfs_file_extent_num_bytes(l, extent);
  2226. if (key.offset + num_bytes < new->file_pos)
  2227. goto next;
  2228. disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
  2229. if (!disk_bytenr)
  2230. goto next;
  2231. extent_offset = btrfs_file_extent_offset(l, extent);
  2232. old = kmalloc(sizeof(*old), GFP_NOFS);
  2233. if (!old)
  2234. goto out_free_list;
  2235. offset = max(new->file_pos, key.offset);
  2236. end = min(new->file_pos + new->len, key.offset + num_bytes);
  2237. old->bytenr = disk_bytenr;
  2238. old->extent_offset = extent_offset;
  2239. old->offset = offset - key.offset;
  2240. old->len = end - offset;
  2241. old->new = new;
  2242. old->count = 0;
  2243. list_add_tail(&old->list, &new->head);
  2244. next:
  2245. path->slots[0]++;
  2246. cond_resched();
  2247. }
  2248. btrfs_free_path(path);
  2249. atomic_inc(&root->fs_info->defrag_running);
  2250. return new;
  2251. out_free_list:
  2252. list_for_each_entry_safe(old, tmp, &new->head, list) {
  2253. list_del(&old->list);
  2254. kfree(old);
  2255. }
  2256. out_free_path:
  2257. btrfs_free_path(path);
  2258. out_kfree:
  2259. kfree(new);
  2260. return NULL;
  2261. }
  2262. /*
  2263. * helper function for btrfs_finish_ordered_io, this
  2264. * just reads in some of the csum leaves to prime them into ram
  2265. * before we start the transaction. It limits the amount of btree
  2266. * reads required while inside the transaction.
  2267. */
  2268. /* as ordered data IO finishes, this gets called so we can finish
  2269. * an ordered extent if the range of bytes in the file it covers are
  2270. * fully written.
  2271. */
  2272. static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
  2273. {
  2274. struct inode *inode = ordered_extent->inode;
  2275. struct btrfs_root *root = BTRFS_I(inode)->root;
  2276. struct btrfs_trans_handle *trans = NULL;
  2277. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  2278. struct extent_state *cached_state = NULL;
  2279. struct new_sa_defrag_extent *new = NULL;
  2280. int compress_type = 0;
  2281. int ret;
  2282. bool nolock;
  2283. nolock = btrfs_is_free_space_inode(inode);
  2284. if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
  2285. ret = -EIO;
  2286. goto out;
  2287. }
  2288. if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
  2289. BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
  2290. btrfs_ordered_update_i_size(inode, 0, ordered_extent);
  2291. if (nolock)
  2292. trans = btrfs_join_transaction_nolock(root);
  2293. else
  2294. trans = btrfs_join_transaction(root);
  2295. if (IS_ERR(trans)) {
  2296. ret = PTR_ERR(trans);
  2297. trans = NULL;
  2298. goto out;
  2299. }
  2300. trans->block_rsv = &root->fs_info->delalloc_block_rsv;
  2301. ret = btrfs_update_inode_fallback(trans, root, inode);
  2302. if (ret) /* -ENOMEM or corruption */
  2303. btrfs_abort_transaction(trans, root, ret);
  2304. goto out;
  2305. }
  2306. lock_extent_bits(io_tree, ordered_extent->file_offset,
  2307. ordered_extent->file_offset + ordered_extent->len - 1,
  2308. 0, &cached_state);
  2309. ret = test_range_bit(io_tree, ordered_extent->file_offset,
  2310. ordered_extent->file_offset + ordered_extent->len - 1,
  2311. EXTENT_DEFRAG, 1, cached_state);
  2312. if (ret) {
  2313. u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
  2314. if (last_snapshot >= BTRFS_I(inode)->generation)
  2315. /* the inode is shared */
  2316. new = record_old_file_extents(inode, ordered_extent);
  2317. clear_extent_bit(io_tree, ordered_extent->file_offset,
  2318. ordered_extent->file_offset + ordered_extent->len - 1,
  2319. EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
  2320. }
  2321. if (nolock)
  2322. trans = btrfs_join_transaction_nolock(root);
  2323. else
  2324. trans = btrfs_join_transaction(root);
  2325. if (IS_ERR(trans)) {
  2326. ret = PTR_ERR(trans);
  2327. trans = NULL;
  2328. goto out_unlock;
  2329. }
  2330. trans->block_rsv = &root->fs_info->delalloc_block_rsv;
  2331. if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
  2332. compress_type = ordered_extent->compress_type;
  2333. if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
  2334. BUG_ON(compress_type);
  2335. ret = btrfs_mark_extent_written(trans, inode,
  2336. ordered_extent->file_offset,
  2337. ordered_extent->file_offset +
  2338. ordered_extent->len);
  2339. } else {
  2340. BUG_ON(root == root->fs_info->tree_root);
  2341. ret = insert_reserved_file_extent(trans, inode,
  2342. ordered_extent->file_offset,
  2343. ordered_extent->start,
  2344. ordered_extent->disk_len,
  2345. ordered_extent->len,
  2346. ordered_extent->len,
  2347. compress_type, 0, 0,
  2348. BTRFS_FILE_EXTENT_REG);
  2349. }
  2350. unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
  2351. ordered_extent->file_offset, ordered_extent->len,
  2352. trans->transid);
  2353. if (ret < 0) {
  2354. btrfs_abort_transaction(trans, root, ret);
  2355. goto out_unlock;
  2356. }
  2357. add_pending_csums(trans, inode, ordered_extent->file_offset,
  2358. &ordered_extent->list);
  2359. btrfs_ordered_update_i_size(inode, 0, ordered_extent);
  2360. ret = btrfs_update_inode_fallback(trans, root, inode);
  2361. if (ret) { /* -ENOMEM or corruption */
  2362. btrfs_abort_transaction(trans, root, ret);
  2363. goto out_unlock;
  2364. }
  2365. ret = 0;
  2366. out_unlock:
  2367. unlock_extent_cached(io_tree, ordered_extent->file_offset,
  2368. ordered_extent->file_offset +
  2369. ordered_extent->len - 1, &cached_state, GFP_NOFS);
  2370. out:
  2371. if (root != root->fs_info->tree_root)
  2372. btrfs_delalloc_release_metadata(inode, ordered_extent->len);
  2373. if (trans)
  2374. btrfs_end_transaction(trans, root);
  2375. if (ret) {
  2376. clear_extent_uptodate(io_tree, ordered_extent->file_offset,
  2377. ordered_extent->file_offset +
  2378. ordered_extent->len - 1, NULL, GFP_NOFS);
  2379. /*
  2380. * If the ordered extent had an IOERR or something else went
  2381. * wrong we need to return the space for this ordered extent
  2382. * back to the allocator.
  2383. */
  2384. if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
  2385. !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
  2386. btrfs_free_reserved_extent(root, ordered_extent->start,
  2387. ordered_extent->disk_len);
  2388. }
  2389. /*
  2390. * This needs to be done to make sure anybody waiting knows we are done
  2391. * updating everything for this ordered extent.
  2392. */
  2393. btrfs_remove_ordered_extent(inode, ordered_extent);
  2394. /* for snapshot-aware defrag */
  2395. if (new)
  2396. relink_file_extents(new);
  2397. /* once for us */
  2398. btrfs_put_ordered_extent(ordered_extent);
  2399. /* once for the tree */
  2400. btrfs_put_ordered_extent(ordered_extent);
  2401. return ret;
  2402. }
  2403. static void finish_ordered_fn(struct btrfs_work *work)
  2404. {
  2405. struct btrfs_ordered_extent *ordered_extent;
  2406. ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
  2407. btrfs_finish_ordered_io(ordered_extent);
  2408. }
  2409. static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
  2410. struct extent_state *state, int uptodate)
  2411. {
  2412. struct inode *inode = page->mapping->host;
  2413. struct btrfs_root *root = BTRFS_I(inode)->root;
  2414. struct btrfs_ordered_extent *ordered_extent = NULL;
  2415. struct btrfs_workers *workers;
  2416. trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
  2417. ClearPagePrivate2(page);
  2418. if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
  2419. end - start + 1, uptodate))
  2420. return 0;
  2421. ordered_extent->work.func = finish_ordered_fn;
  2422. ordered_extent->work.flags = 0;
  2423. if (btrfs_is_free_space_inode(inode))
  2424. workers = &root->fs_info->endio_freespace_worker;
  2425. else
  2426. workers = &root->fs_info->endio_write_workers;
  2427. btrfs_queue_worker(workers, &ordered_extent->work);
  2428. return 0;
  2429. }
  2430. /*
  2431. * when reads are done, we need to check csums to verify the data is correct
  2432. * if there's a match, we allow the bio to finish. If not, the code in
  2433. * extent_io.c will try to find good copies for us.
  2434. */
  2435. static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
  2436. struct extent_state *state, int mirror)
  2437. {
  2438. size_t offset = start - page_offset(page);
  2439. struct inode *inode = page->mapping->host;
  2440. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  2441. char *kaddr;
  2442. u64 private = ~(u32)0;
  2443. int ret;
  2444. struct btrfs_root *root = BTRFS_I(inode)->root;
  2445. u32 csum = ~(u32)0;
  2446. if (PageChecked(page)) {
  2447. ClearPageChecked(page);
  2448. goto good;
  2449. }
  2450. if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
  2451. goto good;
  2452. if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
  2453. test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
  2454. clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
  2455. GFP_NOFS);
  2456. return 0;
  2457. }
  2458. if (state && state->start == start) {
  2459. private = state->private;
  2460. ret = 0;
  2461. } else {
  2462. ret = get_state_private(io_tree, start, &private);
  2463. }
  2464. kaddr = kmap_atomic(page);
  2465. if (ret)
  2466. goto zeroit;
  2467. csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
  2468. btrfs_csum_final(csum, (char *)&csum);
  2469. if (csum != private)
  2470. goto zeroit;
  2471. kunmap_atomic(kaddr);
  2472. good:
  2473. return 0;
  2474. zeroit:
  2475. printk_ratelimited(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
  2476. "private %llu\n",
  2477. (unsigned long long)btrfs_ino(page->mapping->host),
  2478. (unsigned long long)start, csum,
  2479. (unsigned long long)private);
  2480. memset(kaddr + offset, 1, end - start + 1);
  2481. flush_dcache_page(page);
  2482. kunmap_atomic(kaddr);
  2483. if (private == 0)
  2484. return 0;
  2485. return -EIO;
  2486. }
  2487. struct delayed_iput {
  2488. struct list_head list;
  2489. struct inode *inode;
  2490. };
  2491. /* JDM: If this is fs-wide, why can't we add a pointer to
  2492. * btrfs_inode instead and avoid the allocation? */
  2493. void btrfs_add_delayed_iput(struct inode *inode)
  2494. {
  2495. struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
  2496. struct delayed_iput *delayed;
  2497. if (atomic_add_unless(&inode->i_count, -1, 1))
  2498. return;
  2499. delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
  2500. delayed->inode = inode;
  2501. spin_lock(&fs_info->delayed_iput_lock);
  2502. list_add_tail(&delayed->list, &fs_info->delayed_iputs);
  2503. spin_unlock(&fs_info->delayed_iput_lock);
  2504. }
  2505. void btrfs_run_delayed_iputs(struct btrfs_root *root)
  2506. {
  2507. LIST_HEAD(list);
  2508. struct btrfs_fs_info *fs_info = root->fs_info;
  2509. struct delayed_iput *delayed;
  2510. int empty;
  2511. spin_lock(&fs_info->delayed_iput_lock);
  2512. empty = list_empty(&fs_info->delayed_iputs);
  2513. spin_unlock(&fs_info->delayed_iput_lock);
  2514. if (empty)
  2515. return;
  2516. spin_lock(&fs_info->delayed_iput_lock);
  2517. list_splice_init(&fs_info->delayed_iputs, &list);
  2518. spin_unlock(&fs_info->delayed_iput_lock);
  2519. while (!list_empty(&list)) {
  2520. delayed = list_entry(list.next, struct delayed_iput, list);
  2521. list_del(&delayed->list);
  2522. iput(delayed->inode);
  2523. kfree(delayed);
  2524. }
  2525. }
  2526. /*
  2527. * This is called in transaction commit time. If there are no orphan
  2528. * files in the subvolume, it removes orphan item and frees block_rsv
  2529. * structure.
  2530. */
  2531. void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
  2532. struct btrfs_root *root)
  2533. {
  2534. struct btrfs_block_rsv *block_rsv;
  2535. int ret;
  2536. if (atomic_read(&root->orphan_inodes) ||
  2537. root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
  2538. return;
  2539. spin_lock(&root->orphan_lock);
  2540. if (atomic_read(&root->orphan_inodes)) {
  2541. spin_unlock(&root->orphan_lock);
  2542. return;
  2543. }
  2544. if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
  2545. spin_unlock(&root->orphan_lock);
  2546. return;
  2547. }
  2548. block_rsv = root->orphan_block_rsv;
  2549. root->orphan_block_rsv = NULL;
  2550. spin_unlock(&root->orphan_lock);
  2551. if (root->orphan_item_inserted &&
  2552. btrfs_root_refs(&root->root_item) > 0) {
  2553. ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
  2554. root->root_key.objectid);
  2555. BUG_ON(ret);
  2556. root->orphan_item_inserted = 0;
  2557. }
  2558. if (block_rsv) {
  2559. WARN_ON(block_rsv->size > 0);
  2560. btrfs_free_block_rsv(root, block_rsv);
  2561. }
  2562. }
  2563. /*
  2564. * This creates an orphan entry for the given inode in case something goes
  2565. * wrong in the middle of an unlink/truncate.
  2566. *
  2567. * NOTE: caller of this function should reserve 5 units of metadata for
  2568. * this function.
  2569. */
  2570. int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
  2571. {
  2572. struct btrfs_root *root = BTRFS_I(inode)->root;
  2573. struct btrfs_block_rsv *block_rsv = NULL;
  2574. int reserve = 0;
  2575. int insert = 0;
  2576. int ret;
  2577. if (!root->orphan_block_rsv) {
  2578. block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
  2579. if (!block_rsv)
  2580. return -ENOMEM;
  2581. }
  2582. spin_lock(&root->orphan_lock);
  2583. if (!root->orphan_block_rsv) {
  2584. root->orphan_block_rsv = block_rsv;
  2585. } else if (block_rsv) {
  2586. btrfs_free_block_rsv(root, block_rsv);
  2587. block_rsv = NULL;
  2588. }
  2589. if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
  2590. &BTRFS_I(inode)->runtime_flags)) {
  2591. #if 0
  2592. /*
  2593. * For proper ENOSPC handling, we should do orphan
  2594. * cleanup when mounting. But this introduces backward
  2595. * compatibility issue.
  2596. */
  2597. if (!xchg(&root->orphan_item_inserted, 1))
  2598. insert = 2;
  2599. else
  2600. insert = 1;
  2601. #endif
  2602. insert = 1;
  2603. atomic_inc(&root->orphan_inodes);
  2604. }
  2605. if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
  2606. &BTRFS_I(inode)->runtime_flags))
  2607. reserve = 1;
  2608. spin_unlock(&root->orphan_lock);
  2609. /* grab metadata reservation from transaction handle */
  2610. if (reserve) {
  2611. ret = btrfs_orphan_reserve_metadata(trans, inode);
  2612. BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
  2613. }
  2614. /* insert an orphan item to track this unlinked/truncated file */
  2615. if (insert >= 1) {
  2616. ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
  2617. if (ret && ret != -EEXIST) {
  2618. clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
  2619. &BTRFS_I(inode)->runtime_flags);
  2620. btrfs_abort_transaction(trans, root, ret);
  2621. return ret;
  2622. }
  2623. ret = 0;
  2624. }
  2625. /* insert an orphan item to track subvolume contains orphan files */
  2626. if (insert >= 2) {
  2627. ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
  2628. root->root_key.objectid);
  2629. if (ret && ret != -EEXIST) {
  2630. btrfs_abort_transaction(trans, root, ret);
  2631. return ret;
  2632. }
  2633. }
  2634. return 0;
  2635. }
  2636. /*
  2637. * We have done the truncate/delete so we can go ahead and remove the orphan
  2638. * item for this particular inode.
  2639. */
  2640. int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
  2641. {
  2642. struct btrfs_root *root = BTRFS_I(inode)->root;
  2643. int delete_item = 0;
  2644. int release_rsv = 0;
  2645. int ret = 0;
  2646. spin_lock(&root->orphan_lock);
  2647. if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
  2648. &BTRFS_I(inode)->runtime_flags))
  2649. delete_item = 1;
  2650. if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
  2651. &BTRFS_I(inode)->runtime_flags))
  2652. release_rsv = 1;
  2653. spin_unlock(&root->orphan_lock);
  2654. if (trans && delete_item) {
  2655. ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
  2656. BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
  2657. }
  2658. if (release_rsv) {
  2659. btrfs_orphan_release_metadata(inode);
  2660. atomic_dec(&root->orphan_inodes);
  2661. }
  2662. return 0;
  2663. }
  2664. /*
  2665. * this cleans up any orphans that may be left on the list from the last use
  2666. * of this root.
  2667. */
  2668. int btrfs_orphan_cleanup(struct btrfs_root *root)
  2669. {
  2670. struct btrfs_path *path;
  2671. struct extent_buffer *leaf;
  2672. struct btrfs_key key, found_key;
  2673. struct btrfs_trans_handle *trans;
  2674. struct inode *inode;
  2675. u64 last_objectid = 0;
  2676. int ret = 0, nr_unlink = 0, nr_truncate = 0;
  2677. if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
  2678. return 0;
  2679. path = btrfs_alloc_path();
  2680. if (!path) {
  2681. ret = -ENOMEM;
  2682. goto out;
  2683. }
  2684. path->reada = -1;
  2685. key.objectid = BTRFS_ORPHAN_OBJECTID;
  2686. btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
  2687. key.offset = (u64)-1;
  2688. while (1) {
  2689. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2690. if (ret < 0)
  2691. goto out;
  2692. /*
  2693. * if ret == 0 means we found what we were searching for, which
  2694. * is weird, but possible, so only screw with path if we didn't
  2695. * find the key and see if we have stuff that matches
  2696. */
  2697. if (ret > 0) {
  2698. ret = 0;
  2699. if (path->slots[0] == 0)
  2700. break;
  2701. path->slots[0]--;
  2702. }
  2703. /* pull out the item */
  2704. leaf = path->nodes[0];
  2705. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2706. /* make sure the item matches what we want */
  2707. if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
  2708. break;
  2709. if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
  2710. break;
  2711. /* release the path since we're done with it */
  2712. btrfs_release_path(path);
  2713. /*
  2714. * this is where we are basically btrfs_lookup, without the
  2715. * crossing root thing. we store the inode number in the
  2716. * offset of the orphan item.
  2717. */
  2718. if (found_key.offset == last_objectid) {
  2719. printk(KERN_ERR "btrfs: Error removing orphan entry, "
  2720. "stopping orphan cleanup\n");
  2721. ret = -EINVAL;
  2722. goto out;
  2723. }
  2724. last_objectid = found_key.offset;
  2725. found_key.objectid = found_key.offset;
  2726. found_key.type = BTRFS_INODE_ITEM_KEY;
  2727. found_key.offset = 0;
  2728. inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
  2729. ret = PTR_RET(inode);
  2730. if (ret && ret != -ESTALE)
  2731. goto out;
  2732. if (ret == -ESTALE && root == root->fs_info->tree_root) {
  2733. struct btrfs_root *dead_root;
  2734. struct btrfs_fs_info *fs_info = root->fs_info;
  2735. int is_dead_root = 0;
  2736. /*
  2737. * this is an orphan in the tree root. Currently these
  2738. * could come from 2 sources:
  2739. * a) a snapshot deletion in progress
  2740. * b) a free space cache inode
  2741. * We need to distinguish those two, as the snapshot
  2742. * orphan must not get deleted.
  2743. * find_dead_roots already ran before us, so if this
  2744. * is a snapshot deletion, we should find the root
  2745. * in the dead_roots list
  2746. */
  2747. spin_lock(&fs_info->trans_lock);
  2748. list_for_each_entry(dead_root, &fs_info->dead_roots,
  2749. root_list) {
  2750. if (dead_root->root_key.objectid ==
  2751. found_key.objectid) {
  2752. is_dead_root = 1;
  2753. break;
  2754. }
  2755. }
  2756. spin_unlock(&fs_info->trans_lock);
  2757. if (is_dead_root) {
  2758. /* prevent this orphan from being found again */
  2759. key.offset = found_key.objectid - 1;
  2760. continue;
  2761. }
  2762. }
  2763. /*
  2764. * Inode is already gone but the orphan item is still there,
  2765. * kill the orphan item.
  2766. */
  2767. if (ret == -ESTALE) {
  2768. trans = btrfs_start_transaction(root, 1);
  2769. if (IS_ERR(trans)) {
  2770. ret = PTR_ERR(trans);
  2771. goto out;
  2772. }
  2773. printk(KERN_ERR "auto deleting %Lu\n",
  2774. found_key.objectid);
  2775. ret = btrfs_del_orphan_item(trans, root,
  2776. found_key.objectid);
  2777. BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
  2778. btrfs_end_transaction(trans, root);
  2779. continue;
  2780. }
  2781. /*
  2782. * add this inode to the orphan list so btrfs_orphan_del does
  2783. * the proper thing when we hit it
  2784. */
  2785. set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
  2786. &BTRFS_I(inode)->runtime_flags);
  2787. atomic_inc(&root->orphan_inodes);
  2788. /* if we have links, this was a truncate, lets do that */
  2789. if (inode->i_nlink) {
  2790. if (!S_ISREG(inode->i_mode)) {
  2791. WARN_ON(1);
  2792. iput(inode);
  2793. continue;
  2794. }
  2795. nr_truncate++;
  2796. /* 1 for the orphan item deletion. */
  2797. trans = btrfs_start_transaction(root, 1);
  2798. if (IS_ERR(trans)) {
  2799. ret = PTR_ERR(trans);
  2800. goto out;
  2801. }
  2802. ret = btrfs_orphan_add(trans, inode);
  2803. btrfs_end_transaction(trans, root);
  2804. if (ret)
  2805. goto out;
  2806. ret = btrfs_truncate(inode);
  2807. if (ret)
  2808. btrfs_orphan_del(NULL, inode);
  2809. } else {
  2810. nr_unlink++;
  2811. }
  2812. /* this will do delete_inode and everything for us */
  2813. iput(inode);
  2814. if (ret)
  2815. goto out;
  2816. }
  2817. /* release the path since we're done with it */
  2818. btrfs_release_path(path);
  2819. root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
  2820. if (root->orphan_block_rsv)
  2821. btrfs_block_rsv_release(root, root->orphan_block_rsv,
  2822. (u64)-1);
  2823. if (root->orphan_block_rsv || root->orphan_item_inserted) {
  2824. trans = btrfs_join_transaction(root);
  2825. if (!IS_ERR(trans))
  2826. btrfs_end_transaction(trans, root);
  2827. }
  2828. if (nr_unlink)
  2829. printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
  2830. if (nr_truncate)
  2831. printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
  2832. out:
  2833. if (ret)
  2834. printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
  2835. btrfs_free_path(path);
  2836. return ret;
  2837. }
  2838. /*
  2839. * very simple check to peek ahead in the leaf looking for xattrs. If we
  2840. * don't find any xattrs, we know there can't be any acls.
  2841. *
  2842. * slot is the slot the inode is in, objectid is the objectid of the inode
  2843. */
  2844. static noinline int acls_after_inode_item(struct extent_buffer *leaf,
  2845. int slot, u64 objectid)
  2846. {
  2847. u32 nritems = btrfs_header_nritems(leaf);
  2848. struct btrfs_key found_key;
  2849. int scanned = 0;
  2850. slot++;
  2851. while (slot < nritems) {
  2852. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  2853. /* we found a different objectid, there must not be acls */
  2854. if (found_key.objectid != objectid)
  2855. return 0;
  2856. /* we found an xattr, assume we've got an acl */
  2857. if (found_key.type == BTRFS_XATTR_ITEM_KEY)
  2858. return 1;
  2859. /*
  2860. * we found a key greater than an xattr key, there can't
  2861. * be any acls later on
  2862. */
  2863. if (found_key.type > BTRFS_XATTR_ITEM_KEY)
  2864. return 0;
  2865. slot++;
  2866. scanned++;
  2867. /*
  2868. * it goes inode, inode backrefs, xattrs, extents,
  2869. * so if there are a ton of hard links to an inode there can
  2870. * be a lot of backrefs. Don't waste time searching too hard,
  2871. * this is just an optimization
  2872. */
  2873. if (scanned >= 8)
  2874. break;
  2875. }
  2876. /* we hit the end of the leaf before we found an xattr or
  2877. * something larger than an xattr. We have to assume the inode
  2878. * has acls
  2879. */
  2880. return 1;
  2881. }
  2882. /*
  2883. * read an inode from the btree into the in-memory inode
  2884. */
  2885. static void btrfs_read_locked_inode(struct inode *inode)
  2886. {
  2887. struct btrfs_path *path;
  2888. struct extent_buffer *leaf;
  2889. struct btrfs_inode_item *inode_item;
  2890. struct btrfs_timespec *tspec;
  2891. struct btrfs_root *root = BTRFS_I(inode)->root;
  2892. struct btrfs_key location;
  2893. int maybe_acls;
  2894. u32 rdev;
  2895. int ret;
  2896. bool filled = false;
  2897. ret = btrfs_fill_inode(inode, &rdev);
  2898. if (!ret)
  2899. filled = true;
  2900. path = btrfs_alloc_path();
  2901. if (!path)
  2902. goto make_bad;
  2903. path->leave_spinning = 1;
  2904. memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
  2905. ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
  2906. if (ret)
  2907. goto make_bad;
  2908. leaf = path->nodes[0];
  2909. if (filled)
  2910. goto cache_acl;
  2911. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  2912. struct btrfs_inode_item);
  2913. inode->i_mode = btrfs_inode_mode(leaf, inode_item);
  2914. set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
  2915. i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
  2916. i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
  2917. btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
  2918. tspec = btrfs_inode_atime(inode_item);
  2919. inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  2920. inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  2921. tspec = btrfs_inode_mtime(inode_item);
  2922. inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  2923. inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  2924. tspec = btrfs_inode_ctime(inode_item);
  2925. inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  2926. inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  2927. inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
  2928. BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
  2929. BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
  2930. /*
  2931. * If we were modified in the current generation and evicted from memory
  2932. * and then re-read we need to do a full sync since we don't have any
  2933. * idea about which extents were modified before we were evicted from
  2934. * cache.
  2935. */
  2936. if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
  2937. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  2938. &BTRFS_I(inode)->runtime_flags);
  2939. inode->i_version = btrfs_inode_sequence(leaf, inode_item);
  2940. inode->i_generation = BTRFS_I(inode)->generation;
  2941. inode->i_rdev = 0;
  2942. rdev = btrfs_inode_rdev(leaf, inode_item);
  2943. BTRFS_I(inode)->index_cnt = (u64)-1;
  2944. BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
  2945. cache_acl:
  2946. /*
  2947. * try to precache a NULL acl entry for files that don't have
  2948. * any xattrs or acls
  2949. */
  2950. maybe_acls = acls_after_inode_item(leaf, path->slots[0],
  2951. btrfs_ino(inode));
  2952. if (!maybe_acls)
  2953. cache_no_acl(inode);
  2954. btrfs_free_path(path);
  2955. switch (inode->i_mode & S_IFMT) {
  2956. case S_IFREG:
  2957. inode->i_mapping->a_ops = &btrfs_aops;
  2958. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  2959. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  2960. inode->i_fop = &btrfs_file_operations;
  2961. inode->i_op = &btrfs_file_inode_operations;
  2962. break;
  2963. case S_IFDIR:
  2964. inode->i_fop = &btrfs_dir_file_operations;
  2965. if (root == root->fs_info->tree_root)
  2966. inode->i_op = &btrfs_dir_ro_inode_operations;
  2967. else
  2968. inode->i_op = &btrfs_dir_inode_operations;
  2969. break;
  2970. case S_IFLNK:
  2971. inode->i_op = &btrfs_symlink_inode_operations;
  2972. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  2973. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  2974. break;
  2975. default:
  2976. inode->i_op = &btrfs_special_inode_operations;
  2977. init_special_inode(inode, inode->i_mode, rdev);
  2978. break;
  2979. }
  2980. btrfs_update_iflags(inode);
  2981. return;
  2982. make_bad:
  2983. btrfs_free_path(path);
  2984. make_bad_inode(inode);
  2985. }
  2986. /*
  2987. * given a leaf and an inode, copy the inode fields into the leaf
  2988. */
  2989. static void fill_inode_item(struct btrfs_trans_handle *trans,
  2990. struct extent_buffer *leaf,
  2991. struct btrfs_inode_item *item,
  2992. struct inode *inode)
  2993. {
  2994. struct btrfs_map_token token;
  2995. btrfs_init_map_token(&token);
  2996. btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
  2997. btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
  2998. btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
  2999. &token);
  3000. btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
  3001. btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
  3002. btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
  3003. inode->i_atime.tv_sec, &token);
  3004. btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
  3005. inode->i_atime.tv_nsec, &token);
  3006. btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
  3007. inode->i_mtime.tv_sec, &token);
  3008. btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
  3009. inode->i_mtime.tv_nsec, &token);
  3010. btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
  3011. inode->i_ctime.tv_sec, &token);
  3012. btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
  3013. inode->i_ctime.tv_nsec, &token);
  3014. btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
  3015. &token);
  3016. btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
  3017. &token);
  3018. btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
  3019. btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
  3020. btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
  3021. btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
  3022. btrfs_set_token_inode_block_group(leaf, item, 0, &token);
  3023. }
  3024. /*
  3025. * copy everything in the in-memory inode into the btree.
  3026. */
  3027. static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
  3028. struct btrfs_root *root, struct inode *inode)
  3029. {
  3030. struct btrfs_inode_item *inode_item;
  3031. struct btrfs_path *path;
  3032. struct extent_buffer *leaf;
  3033. int ret;
  3034. path = btrfs_alloc_path();
  3035. if (!path)
  3036. return -ENOMEM;
  3037. path->leave_spinning = 1;
  3038. ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
  3039. 1);
  3040. if (ret) {
  3041. if (ret > 0)
  3042. ret = -ENOENT;
  3043. goto failed;
  3044. }
  3045. btrfs_unlock_up_safe(path, 1);
  3046. leaf = path->nodes[0];
  3047. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  3048. struct btrfs_inode_item);
  3049. fill_inode_item(trans, leaf, inode_item, inode);
  3050. btrfs_mark_buffer_dirty(leaf);
  3051. btrfs_set_inode_last_trans(trans, inode);
  3052. ret = 0;
  3053. failed:
  3054. btrfs_free_path(path);
  3055. return ret;
  3056. }
  3057. /*
  3058. * copy everything in the in-memory inode into the btree.
  3059. */
  3060. noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
  3061. struct btrfs_root *root, struct inode *inode)
  3062. {
  3063. int ret;
  3064. /*
  3065. * If the inode is a free space inode, we can deadlock during commit
  3066. * if we put it into the delayed code.
  3067. *
  3068. * The data relocation inode should also be directly updated
  3069. * without delay
  3070. */
  3071. if (!btrfs_is_free_space_inode(inode)
  3072. && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
  3073. btrfs_update_root_times(trans, root);
  3074. ret = btrfs_delayed_update_inode(trans, root, inode);
  3075. if (!ret)
  3076. btrfs_set_inode_last_trans(trans, inode);
  3077. return ret;
  3078. }
  3079. return btrfs_update_inode_item(trans, root, inode);
  3080. }
  3081. noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
  3082. struct btrfs_root *root,
  3083. struct inode *inode)
  3084. {
  3085. int ret;
  3086. ret = btrfs_update_inode(trans, root, inode);
  3087. if (ret == -ENOSPC)
  3088. return btrfs_update_inode_item(trans, root, inode);
  3089. return ret;
  3090. }
  3091. /*
  3092. * unlink helper that gets used here in inode.c and in the tree logging
  3093. * recovery code. It remove a link in a directory with a given name, and
  3094. * also drops the back refs in the inode to the directory
  3095. */
  3096. static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
  3097. struct btrfs_root *root,
  3098. struct inode *dir, struct inode *inode,
  3099. const char *name, int name_len)
  3100. {
  3101. struct btrfs_path *path;
  3102. int ret = 0;
  3103. struct extent_buffer *leaf;
  3104. struct btrfs_dir_item *di;
  3105. struct btrfs_key key;
  3106. u64 index;
  3107. u64 ino = btrfs_ino(inode);
  3108. u64 dir_ino = btrfs_ino(dir);
  3109. path = btrfs_alloc_path();
  3110. if (!path) {
  3111. ret = -ENOMEM;
  3112. goto out;
  3113. }
  3114. path->leave_spinning = 1;
  3115. di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
  3116. name, name_len, -1);
  3117. if (IS_ERR(di)) {
  3118. ret = PTR_ERR(di);
  3119. goto err;
  3120. }
  3121. if (!di) {
  3122. ret = -ENOENT;
  3123. goto err;
  3124. }
  3125. leaf = path->nodes[0];
  3126. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  3127. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  3128. if (ret)
  3129. goto err;
  3130. btrfs_release_path(path);
  3131. ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
  3132. dir_ino, &index);
  3133. if (ret) {
  3134. printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
  3135. "inode %llu parent %llu\n", name_len, name,
  3136. (unsigned long long)ino, (unsigned long long)dir_ino);
  3137. btrfs_abort_transaction(trans, root, ret);
  3138. goto err;
  3139. }
  3140. ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
  3141. if (ret) {
  3142. btrfs_abort_transaction(trans, root, ret);
  3143. goto err;
  3144. }
  3145. ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
  3146. inode, dir_ino);
  3147. if (ret != 0 && ret != -ENOENT) {
  3148. btrfs_abort_transaction(trans, root, ret);
  3149. goto err;
  3150. }
  3151. ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
  3152. dir, index);
  3153. if (ret == -ENOENT)
  3154. ret = 0;
  3155. err:
  3156. btrfs_free_path(path);
  3157. if (ret)
  3158. goto out;
  3159. btrfs_i_size_write(dir, dir->i_size - name_len * 2);
  3160. inode_inc_iversion(inode);
  3161. inode_inc_iversion(dir);
  3162. inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  3163. ret = btrfs_update_inode(trans, root, dir);
  3164. out:
  3165. return ret;
  3166. }
  3167. int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
  3168. struct btrfs_root *root,
  3169. struct inode *dir, struct inode *inode,
  3170. const char *name, int name_len)
  3171. {
  3172. int ret;
  3173. ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
  3174. if (!ret) {
  3175. btrfs_drop_nlink(inode);
  3176. ret = btrfs_update_inode(trans, root, inode);
  3177. }
  3178. return ret;
  3179. }
  3180. /* helper to check if there is any shared block in the path */
  3181. static int check_path_shared(struct btrfs_root *root,
  3182. struct btrfs_path *path)
  3183. {
  3184. struct extent_buffer *eb;
  3185. int level;
  3186. u64 refs = 1;
  3187. for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
  3188. int ret;
  3189. if (!path->nodes[level])
  3190. break;
  3191. eb = path->nodes[level];
  3192. if (!btrfs_block_can_be_shared(root, eb))
  3193. continue;
  3194. ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
  3195. &refs, NULL);
  3196. if (refs > 1)
  3197. return 1;
  3198. }
  3199. return 0;
  3200. }
  3201. /*
  3202. * helper to start transaction for unlink and rmdir.
  3203. *
  3204. * unlink and rmdir are special in btrfs, they do not always free space.
  3205. * so in enospc case, we should make sure they will free space before
  3206. * allowing them to use the global metadata reservation.
  3207. */
  3208. static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
  3209. struct dentry *dentry)
  3210. {
  3211. struct btrfs_trans_handle *trans;
  3212. struct btrfs_root *root = BTRFS_I(dir)->root;
  3213. struct btrfs_path *path;
  3214. struct btrfs_dir_item *di;
  3215. struct inode *inode = dentry->d_inode;
  3216. u64 index;
  3217. int check_link = 1;
  3218. int err = -ENOSPC;
  3219. int ret;
  3220. u64 ino = btrfs_ino(inode);
  3221. u64 dir_ino = btrfs_ino(dir);
  3222. /*
  3223. * 1 for the possible orphan item
  3224. * 1 for the dir item
  3225. * 1 for the dir index
  3226. * 1 for the inode ref
  3227. * 1 for the inode ref in the tree log
  3228. * 2 for the dir entries in the log
  3229. * 1 for the inode
  3230. */
  3231. trans = btrfs_start_transaction(root, 8);
  3232. if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
  3233. return trans;
  3234. if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
  3235. return ERR_PTR(-ENOSPC);
  3236. /* check if there is someone else holds reference */
  3237. if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
  3238. return ERR_PTR(-ENOSPC);
  3239. if (atomic_read(&inode->i_count) > 2)
  3240. return ERR_PTR(-ENOSPC);
  3241. if (xchg(&root->fs_info->enospc_unlink, 1))
  3242. return ERR_PTR(-ENOSPC);
  3243. path = btrfs_alloc_path();
  3244. if (!path) {
  3245. root->fs_info->enospc_unlink = 0;
  3246. return ERR_PTR(-ENOMEM);
  3247. }
  3248. /* 1 for the orphan item */
  3249. trans = btrfs_start_transaction(root, 1);
  3250. if (IS_ERR(trans)) {
  3251. btrfs_free_path(path);
  3252. root->fs_info->enospc_unlink = 0;
  3253. return trans;
  3254. }
  3255. path->skip_locking = 1;
  3256. path->search_commit_root = 1;
  3257. ret = btrfs_lookup_inode(trans, root, path,
  3258. &BTRFS_I(dir)->location, 0);
  3259. if (ret < 0) {
  3260. err = ret;
  3261. goto out;
  3262. }
  3263. if (ret == 0) {
  3264. if (check_path_shared(root, path))
  3265. goto out;
  3266. } else {
  3267. check_link = 0;
  3268. }
  3269. btrfs_release_path(path);
  3270. ret = btrfs_lookup_inode(trans, root, path,
  3271. &BTRFS_I(inode)->location, 0);
  3272. if (ret < 0) {
  3273. err = ret;
  3274. goto out;
  3275. }
  3276. if (ret == 0) {
  3277. if (check_path_shared(root, path))
  3278. goto out;
  3279. } else {
  3280. check_link = 0;
  3281. }
  3282. btrfs_release_path(path);
  3283. if (ret == 0 && S_ISREG(inode->i_mode)) {
  3284. ret = btrfs_lookup_file_extent(trans, root, path,
  3285. ino, (u64)-1, 0);
  3286. if (ret < 0) {
  3287. err = ret;
  3288. goto out;
  3289. }
  3290. BUG_ON(ret == 0); /* Corruption */
  3291. if (check_path_shared(root, path))
  3292. goto out;
  3293. btrfs_release_path(path);
  3294. }
  3295. if (!check_link) {
  3296. err = 0;
  3297. goto out;
  3298. }
  3299. di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
  3300. dentry->d_name.name, dentry->d_name.len, 0);
  3301. if (IS_ERR(di)) {
  3302. err = PTR_ERR(di);
  3303. goto out;
  3304. }
  3305. if (di) {
  3306. if (check_path_shared(root, path))
  3307. goto out;
  3308. } else {
  3309. err = 0;
  3310. goto out;
  3311. }
  3312. btrfs_release_path(path);
  3313. ret = btrfs_get_inode_ref_index(trans, root, path, dentry->d_name.name,
  3314. dentry->d_name.len, ino, dir_ino, 0,
  3315. &index);
  3316. if (ret) {
  3317. err = ret;
  3318. goto out;
  3319. }
  3320. if (check_path_shared(root, path))
  3321. goto out;
  3322. btrfs_release_path(path);
  3323. /*
  3324. * This is a commit root search, if we can lookup inode item and other
  3325. * relative items in the commit root, it means the transaction of
  3326. * dir/file creation has been committed, and the dir index item that we
  3327. * delay to insert has also been inserted into the commit root. So
  3328. * we needn't worry about the delayed insertion of the dir index item
  3329. * here.
  3330. */
  3331. di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
  3332. dentry->d_name.name, dentry->d_name.len, 0);
  3333. if (IS_ERR(di)) {
  3334. err = PTR_ERR(di);
  3335. goto out;
  3336. }
  3337. BUG_ON(ret == -ENOENT);
  3338. if (check_path_shared(root, path))
  3339. goto out;
  3340. err = 0;
  3341. out:
  3342. btrfs_free_path(path);
  3343. /* Migrate the orphan reservation over */
  3344. if (!err)
  3345. err = btrfs_block_rsv_migrate(trans->block_rsv,
  3346. &root->fs_info->global_block_rsv,
  3347. trans->bytes_reserved);
  3348. if (err) {
  3349. btrfs_end_transaction(trans, root);
  3350. root->fs_info->enospc_unlink = 0;
  3351. return ERR_PTR(err);
  3352. }
  3353. trans->block_rsv = &root->fs_info->global_block_rsv;
  3354. return trans;
  3355. }
  3356. static void __unlink_end_trans(struct btrfs_trans_handle *trans,
  3357. struct btrfs_root *root)
  3358. {
  3359. if (trans->block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL) {
  3360. btrfs_block_rsv_release(root, trans->block_rsv,
  3361. trans->bytes_reserved);
  3362. trans->block_rsv = &root->fs_info->trans_block_rsv;
  3363. BUG_ON(!root->fs_info->enospc_unlink);
  3364. root->fs_info->enospc_unlink = 0;
  3365. }
  3366. btrfs_end_transaction(trans, root);
  3367. }
  3368. static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
  3369. {
  3370. struct btrfs_root *root = BTRFS_I(dir)->root;
  3371. struct btrfs_trans_handle *trans;
  3372. struct inode *inode = dentry->d_inode;
  3373. int ret;
  3374. trans = __unlink_start_trans(dir, dentry);
  3375. if (IS_ERR(trans))
  3376. return PTR_ERR(trans);
  3377. btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
  3378. ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
  3379. dentry->d_name.name, dentry->d_name.len);
  3380. if (ret)
  3381. goto out;
  3382. if (inode->i_nlink == 0) {
  3383. ret = btrfs_orphan_add(trans, inode);
  3384. if (ret)
  3385. goto out;
  3386. }
  3387. out:
  3388. __unlink_end_trans(trans, root);
  3389. btrfs_btree_balance_dirty(root);
  3390. return ret;
  3391. }
  3392. int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
  3393. struct btrfs_root *root,
  3394. struct inode *dir, u64 objectid,
  3395. const char *name, int name_len)
  3396. {
  3397. struct btrfs_path *path;
  3398. struct extent_buffer *leaf;
  3399. struct btrfs_dir_item *di;
  3400. struct btrfs_key key;
  3401. u64 index;
  3402. int ret;
  3403. u64 dir_ino = btrfs_ino(dir);
  3404. path = btrfs_alloc_path();
  3405. if (!path)
  3406. return -ENOMEM;
  3407. di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
  3408. name, name_len, -1);
  3409. if (IS_ERR_OR_NULL(di)) {
  3410. if (!di)
  3411. ret = -ENOENT;
  3412. else
  3413. ret = PTR_ERR(di);
  3414. goto out;
  3415. }
  3416. leaf = path->nodes[0];
  3417. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  3418. WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
  3419. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  3420. if (ret) {
  3421. btrfs_abort_transaction(trans, root, ret);
  3422. goto out;
  3423. }
  3424. btrfs_release_path(path);
  3425. ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
  3426. objectid, root->root_key.objectid,
  3427. dir_ino, &index, name, name_len);
  3428. if (ret < 0) {
  3429. if (ret != -ENOENT) {
  3430. btrfs_abort_transaction(trans, root, ret);
  3431. goto out;
  3432. }
  3433. di = btrfs_search_dir_index_item(root, path, dir_ino,
  3434. name, name_len);
  3435. if (IS_ERR_OR_NULL(di)) {
  3436. if (!di)
  3437. ret = -ENOENT;
  3438. else
  3439. ret = PTR_ERR(di);
  3440. btrfs_abort_transaction(trans, root, ret);
  3441. goto out;
  3442. }
  3443. leaf = path->nodes[0];
  3444. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  3445. btrfs_release_path(path);
  3446. index = key.offset;
  3447. }
  3448. btrfs_release_path(path);
  3449. ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
  3450. if (ret) {
  3451. btrfs_abort_transaction(trans, root, ret);
  3452. goto out;
  3453. }
  3454. btrfs_i_size_write(dir, dir->i_size - name_len * 2);
  3455. inode_inc_iversion(dir);
  3456. dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  3457. ret = btrfs_update_inode_fallback(trans, root, dir);
  3458. if (ret)
  3459. btrfs_abort_transaction(trans, root, ret);
  3460. out:
  3461. btrfs_free_path(path);
  3462. return ret;
  3463. }
  3464. static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
  3465. {
  3466. struct inode *inode = dentry->d_inode;
  3467. int err = 0;
  3468. struct btrfs_root *root = BTRFS_I(dir)->root;
  3469. struct btrfs_trans_handle *trans;
  3470. if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
  3471. return -ENOTEMPTY;
  3472. if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
  3473. return -EPERM;
  3474. trans = __unlink_start_trans(dir, dentry);
  3475. if (IS_ERR(trans))
  3476. return PTR_ERR(trans);
  3477. if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
  3478. err = btrfs_unlink_subvol(trans, root, dir,
  3479. BTRFS_I(inode)->location.objectid,
  3480. dentry->d_name.name,
  3481. dentry->d_name.len);
  3482. goto out;
  3483. }
  3484. err = btrfs_orphan_add(trans, inode);
  3485. if (err)
  3486. goto out;
  3487. /* now the directory is empty */
  3488. err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
  3489. dentry->d_name.name, dentry->d_name.len);
  3490. if (!err)
  3491. btrfs_i_size_write(inode, 0);
  3492. out:
  3493. __unlink_end_trans(trans, root);
  3494. btrfs_btree_balance_dirty(root);
  3495. return err;
  3496. }
  3497. /*
  3498. * this can truncate away extent items, csum items and directory items.
  3499. * It starts at a high offset and removes keys until it can't find
  3500. * any higher than new_size
  3501. *
  3502. * csum items that cross the new i_size are truncated to the new size
  3503. * as well.
  3504. *
  3505. * min_type is the minimum key type to truncate down to. If set to 0, this
  3506. * will kill all the items on this inode, including the INODE_ITEM_KEY.
  3507. */
  3508. int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
  3509. struct btrfs_root *root,
  3510. struct inode *inode,
  3511. u64 new_size, u32 min_type)
  3512. {
  3513. struct btrfs_path *path;
  3514. struct extent_buffer *leaf;
  3515. struct btrfs_file_extent_item *fi;
  3516. struct btrfs_key key;
  3517. struct btrfs_key found_key;
  3518. u64 extent_start = 0;
  3519. u64 extent_num_bytes = 0;
  3520. u64 extent_offset = 0;
  3521. u64 item_end = 0;
  3522. u32 found_type = (u8)-1;
  3523. int found_extent;
  3524. int del_item;
  3525. int pending_del_nr = 0;
  3526. int pending_del_slot = 0;
  3527. int extent_type = -1;
  3528. int ret;
  3529. int err = 0;
  3530. u64 ino = btrfs_ino(inode);
  3531. BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
  3532. path = btrfs_alloc_path();
  3533. if (!path)
  3534. return -ENOMEM;
  3535. path->reada = -1;
  3536. /*
  3537. * We want to drop from the next block forward in case this new size is
  3538. * not block aligned since we will be keeping the last block of the
  3539. * extent just the way it is.
  3540. */
  3541. if (root->ref_cows || root == root->fs_info->tree_root)
  3542. btrfs_drop_extent_cache(inode, ALIGN(new_size,
  3543. root->sectorsize), (u64)-1, 0);
  3544. /*
  3545. * This function is also used to drop the items in the log tree before
  3546. * we relog the inode, so if root != BTRFS_I(inode)->root, it means
  3547. * it is used to drop the loged items. So we shouldn't kill the delayed
  3548. * items.
  3549. */
  3550. if (min_type == 0 && root == BTRFS_I(inode)->root)
  3551. btrfs_kill_delayed_inode_items(inode);
  3552. key.objectid = ino;
  3553. key.offset = (u64)-1;
  3554. key.type = (u8)-1;
  3555. search_again:
  3556. path->leave_spinning = 1;
  3557. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  3558. if (ret < 0) {
  3559. err = ret;
  3560. goto out;
  3561. }
  3562. if (ret > 0) {
  3563. /* there are no items in the tree for us to truncate, we're
  3564. * done
  3565. */
  3566. if (path->slots[0] == 0)
  3567. goto out;
  3568. path->slots[0]--;
  3569. }
  3570. while (1) {
  3571. fi = NULL;
  3572. leaf = path->nodes[0];
  3573. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3574. found_type = btrfs_key_type(&found_key);
  3575. if (found_key.objectid != ino)
  3576. break;
  3577. if (found_type < min_type)
  3578. break;
  3579. item_end = found_key.offset;
  3580. if (found_type == BTRFS_EXTENT_DATA_KEY) {
  3581. fi = btrfs_item_ptr(leaf, path->slots[0],
  3582. struct btrfs_file_extent_item);
  3583. extent_type = btrfs_file_extent_type(leaf, fi);
  3584. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  3585. item_end +=
  3586. btrfs_file_extent_num_bytes(leaf, fi);
  3587. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  3588. item_end += btrfs_file_extent_inline_len(leaf,
  3589. fi);
  3590. }
  3591. item_end--;
  3592. }
  3593. if (found_type > min_type) {
  3594. del_item = 1;
  3595. } else {
  3596. if (item_end < new_size)
  3597. break;
  3598. if (found_key.offset >= new_size)
  3599. del_item = 1;
  3600. else
  3601. del_item = 0;
  3602. }
  3603. found_extent = 0;
  3604. /* FIXME, shrink the extent if the ref count is only 1 */
  3605. if (found_type != BTRFS_EXTENT_DATA_KEY)
  3606. goto delete;
  3607. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  3608. u64 num_dec;
  3609. extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
  3610. if (!del_item) {
  3611. u64 orig_num_bytes =
  3612. btrfs_file_extent_num_bytes(leaf, fi);
  3613. extent_num_bytes = ALIGN(new_size -
  3614. found_key.offset,
  3615. root->sectorsize);
  3616. btrfs_set_file_extent_num_bytes(leaf, fi,
  3617. extent_num_bytes);
  3618. num_dec = (orig_num_bytes -
  3619. extent_num_bytes);
  3620. if (root->ref_cows && extent_start != 0)
  3621. inode_sub_bytes(inode, num_dec);
  3622. btrfs_mark_buffer_dirty(leaf);
  3623. } else {
  3624. extent_num_bytes =
  3625. btrfs_file_extent_disk_num_bytes(leaf,
  3626. fi);
  3627. extent_offset = found_key.offset -
  3628. btrfs_file_extent_offset(leaf, fi);
  3629. /* FIXME blocksize != 4096 */
  3630. num_dec = btrfs_file_extent_num_bytes(leaf, fi);
  3631. if (extent_start != 0) {
  3632. found_extent = 1;
  3633. if (root->ref_cows)
  3634. inode_sub_bytes(inode, num_dec);
  3635. }
  3636. }
  3637. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  3638. /*
  3639. * we can't truncate inline items that have had
  3640. * special encodings
  3641. */
  3642. if (!del_item &&
  3643. btrfs_file_extent_compression(leaf, fi) == 0 &&
  3644. btrfs_file_extent_encryption(leaf, fi) == 0 &&
  3645. btrfs_file_extent_other_encoding(leaf, fi) == 0) {
  3646. u32 size = new_size - found_key.offset;
  3647. if (root->ref_cows) {
  3648. inode_sub_bytes(inode, item_end + 1 -
  3649. new_size);
  3650. }
  3651. size =
  3652. btrfs_file_extent_calc_inline_size(size);
  3653. btrfs_truncate_item(trans, root, path,
  3654. size, 1);
  3655. } else if (root->ref_cows) {
  3656. inode_sub_bytes(inode, item_end + 1 -
  3657. found_key.offset);
  3658. }
  3659. }
  3660. delete:
  3661. if (del_item) {
  3662. if (!pending_del_nr) {
  3663. /* no pending yet, add ourselves */
  3664. pending_del_slot = path->slots[0];
  3665. pending_del_nr = 1;
  3666. } else if (pending_del_nr &&
  3667. path->slots[0] + 1 == pending_del_slot) {
  3668. /* hop on the pending chunk */
  3669. pending_del_nr++;
  3670. pending_del_slot = path->slots[0];
  3671. } else {
  3672. BUG();
  3673. }
  3674. } else {
  3675. break;
  3676. }
  3677. if (found_extent && (root->ref_cows ||
  3678. root == root->fs_info->tree_root)) {
  3679. btrfs_set_path_blocking(path);
  3680. ret = btrfs_free_extent(trans, root, extent_start,
  3681. extent_num_bytes, 0,
  3682. btrfs_header_owner(leaf),
  3683. ino, extent_offset, 0);
  3684. BUG_ON(ret);
  3685. }
  3686. if (found_type == BTRFS_INODE_ITEM_KEY)
  3687. break;
  3688. if (path->slots[0] == 0 ||
  3689. path->slots[0] != pending_del_slot) {
  3690. if (pending_del_nr) {
  3691. ret = btrfs_del_items(trans, root, path,
  3692. pending_del_slot,
  3693. pending_del_nr);
  3694. if (ret) {
  3695. btrfs_abort_transaction(trans,
  3696. root, ret);
  3697. goto error;
  3698. }
  3699. pending_del_nr = 0;
  3700. }
  3701. btrfs_release_path(path);
  3702. goto search_again;
  3703. } else {
  3704. path->slots[0]--;
  3705. }
  3706. }
  3707. out:
  3708. if (pending_del_nr) {
  3709. ret = btrfs_del_items(trans, root, path, pending_del_slot,
  3710. pending_del_nr);
  3711. if (ret)
  3712. btrfs_abort_transaction(trans, root, ret);
  3713. }
  3714. error:
  3715. btrfs_free_path(path);
  3716. return err;
  3717. }
  3718. /*
  3719. * btrfs_truncate_page - read, zero a chunk and write a page
  3720. * @inode - inode that we're zeroing
  3721. * @from - the offset to start zeroing
  3722. * @len - the length to zero, 0 to zero the entire range respective to the
  3723. * offset
  3724. * @front - zero up to the offset instead of from the offset on
  3725. *
  3726. * This will find the page for the "from" offset and cow the page and zero the
  3727. * part we want to zero. This is used with truncate and hole punching.
  3728. */
  3729. int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
  3730. int front)
  3731. {
  3732. struct address_space *mapping = inode->i_mapping;
  3733. struct btrfs_root *root = BTRFS_I(inode)->root;
  3734. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  3735. struct btrfs_ordered_extent *ordered;
  3736. struct extent_state *cached_state = NULL;
  3737. char *kaddr;
  3738. u32 blocksize = root->sectorsize;
  3739. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  3740. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  3741. struct page *page;
  3742. gfp_t mask = btrfs_alloc_write_mask(mapping);
  3743. int ret = 0;
  3744. u64 page_start;
  3745. u64 page_end;
  3746. if ((offset & (blocksize - 1)) == 0 &&
  3747. (!len || ((len & (blocksize - 1)) == 0)))
  3748. goto out;
  3749. ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
  3750. if (ret)
  3751. goto out;
  3752. again:
  3753. page = find_or_create_page(mapping, index, mask);
  3754. if (!page) {
  3755. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  3756. ret = -ENOMEM;
  3757. goto out;
  3758. }
  3759. page_start = page_offset(page);
  3760. page_end = page_start + PAGE_CACHE_SIZE - 1;
  3761. if (!PageUptodate(page)) {
  3762. ret = btrfs_readpage(NULL, page);
  3763. lock_page(page);
  3764. if (page->mapping != mapping) {
  3765. unlock_page(page);
  3766. page_cache_release(page);
  3767. goto again;
  3768. }
  3769. if (!PageUptodate(page)) {
  3770. ret = -EIO;
  3771. goto out_unlock;
  3772. }
  3773. }
  3774. wait_on_page_writeback(page);
  3775. lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
  3776. set_page_extent_mapped(page);
  3777. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  3778. if (ordered) {
  3779. unlock_extent_cached(io_tree, page_start, page_end,
  3780. &cached_state, GFP_NOFS);
  3781. unlock_page(page);
  3782. page_cache_release(page);
  3783. btrfs_start_ordered_extent(inode, ordered, 1);
  3784. btrfs_put_ordered_extent(ordered);
  3785. goto again;
  3786. }
  3787. clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
  3788. EXTENT_DIRTY | EXTENT_DELALLOC |
  3789. EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
  3790. 0, 0, &cached_state, GFP_NOFS);
  3791. ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
  3792. &cached_state);
  3793. if (ret) {
  3794. unlock_extent_cached(io_tree, page_start, page_end,
  3795. &cached_state, GFP_NOFS);
  3796. goto out_unlock;
  3797. }
  3798. if (offset != PAGE_CACHE_SIZE) {
  3799. if (!len)
  3800. len = PAGE_CACHE_SIZE - offset;
  3801. kaddr = kmap(page);
  3802. if (front)
  3803. memset(kaddr, 0, offset);
  3804. else
  3805. memset(kaddr + offset, 0, len);
  3806. flush_dcache_page(page);
  3807. kunmap(page);
  3808. }
  3809. ClearPageChecked(page);
  3810. set_page_dirty(page);
  3811. unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
  3812. GFP_NOFS);
  3813. out_unlock:
  3814. if (ret)
  3815. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  3816. unlock_page(page);
  3817. page_cache_release(page);
  3818. out:
  3819. return ret;
  3820. }
  3821. /*
  3822. * This function puts in dummy file extents for the area we're creating a hole
  3823. * for. So if we are truncating this file to a larger size we need to insert
  3824. * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
  3825. * the range between oldsize and size
  3826. */
  3827. int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
  3828. {
  3829. struct btrfs_trans_handle *trans;
  3830. struct btrfs_root *root = BTRFS_I(inode)->root;
  3831. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  3832. struct extent_map *em = NULL;
  3833. struct extent_state *cached_state = NULL;
  3834. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  3835. u64 hole_start = ALIGN(oldsize, root->sectorsize);
  3836. u64 block_end = ALIGN(size, root->sectorsize);
  3837. u64 last_byte;
  3838. u64 cur_offset;
  3839. u64 hole_size;
  3840. int err = 0;
  3841. if (size <= hole_start)
  3842. return 0;
  3843. while (1) {
  3844. struct btrfs_ordered_extent *ordered;
  3845. btrfs_wait_ordered_range(inode, hole_start,
  3846. block_end - hole_start);
  3847. lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
  3848. &cached_state);
  3849. ordered = btrfs_lookup_ordered_extent(inode, hole_start);
  3850. if (!ordered)
  3851. break;
  3852. unlock_extent_cached(io_tree, hole_start, block_end - 1,
  3853. &cached_state, GFP_NOFS);
  3854. btrfs_put_ordered_extent(ordered);
  3855. }
  3856. cur_offset = hole_start;
  3857. while (1) {
  3858. em = btrfs_get_extent(inode, NULL, 0, cur_offset,
  3859. block_end - cur_offset, 0);
  3860. if (IS_ERR(em)) {
  3861. err = PTR_ERR(em);
  3862. em = NULL;
  3863. break;
  3864. }
  3865. last_byte = min(extent_map_end(em), block_end);
  3866. last_byte = ALIGN(last_byte , root->sectorsize);
  3867. if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
  3868. struct extent_map *hole_em;
  3869. hole_size = last_byte - cur_offset;
  3870. trans = btrfs_start_transaction(root, 3);
  3871. if (IS_ERR(trans)) {
  3872. err = PTR_ERR(trans);
  3873. break;
  3874. }
  3875. err = btrfs_drop_extents(trans, root, inode,
  3876. cur_offset,
  3877. cur_offset + hole_size, 1);
  3878. if (err) {
  3879. btrfs_abort_transaction(trans, root, err);
  3880. btrfs_end_transaction(trans, root);
  3881. break;
  3882. }
  3883. err = btrfs_insert_file_extent(trans, root,
  3884. btrfs_ino(inode), cur_offset, 0,
  3885. 0, hole_size, 0, hole_size,
  3886. 0, 0, 0);
  3887. if (err) {
  3888. btrfs_abort_transaction(trans, root, err);
  3889. btrfs_end_transaction(trans, root);
  3890. break;
  3891. }
  3892. btrfs_drop_extent_cache(inode, cur_offset,
  3893. cur_offset + hole_size - 1, 0);
  3894. hole_em = alloc_extent_map();
  3895. if (!hole_em) {
  3896. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  3897. &BTRFS_I(inode)->runtime_flags);
  3898. goto next;
  3899. }
  3900. hole_em->start = cur_offset;
  3901. hole_em->len = hole_size;
  3902. hole_em->orig_start = cur_offset;
  3903. hole_em->block_start = EXTENT_MAP_HOLE;
  3904. hole_em->block_len = 0;
  3905. hole_em->orig_block_len = 0;
  3906. hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
  3907. hole_em->compress_type = BTRFS_COMPRESS_NONE;
  3908. hole_em->generation = trans->transid;
  3909. while (1) {
  3910. write_lock(&em_tree->lock);
  3911. err = add_extent_mapping(em_tree, hole_em);
  3912. if (!err)
  3913. list_move(&hole_em->list,
  3914. &em_tree->modified_extents);
  3915. write_unlock(&em_tree->lock);
  3916. if (err != -EEXIST)
  3917. break;
  3918. btrfs_drop_extent_cache(inode, cur_offset,
  3919. cur_offset +
  3920. hole_size - 1, 0);
  3921. }
  3922. free_extent_map(hole_em);
  3923. next:
  3924. btrfs_update_inode(trans, root, inode);
  3925. btrfs_end_transaction(trans, root);
  3926. }
  3927. free_extent_map(em);
  3928. em = NULL;
  3929. cur_offset = last_byte;
  3930. if (cur_offset >= block_end)
  3931. break;
  3932. }
  3933. free_extent_map(em);
  3934. unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
  3935. GFP_NOFS);
  3936. return err;
  3937. }
  3938. static int btrfs_setsize(struct inode *inode, struct iattr *attr)
  3939. {
  3940. struct btrfs_root *root = BTRFS_I(inode)->root;
  3941. struct btrfs_trans_handle *trans;
  3942. loff_t oldsize = i_size_read(inode);
  3943. loff_t newsize = attr->ia_size;
  3944. int mask = attr->ia_valid;
  3945. int ret;
  3946. if (newsize == oldsize)
  3947. return 0;
  3948. /*
  3949. * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
  3950. * special case where we need to update the times despite not having
  3951. * these flags set. For all other operations the VFS set these flags
  3952. * explicitly if it wants a timestamp update.
  3953. */
  3954. if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
  3955. inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
  3956. if (newsize > oldsize) {
  3957. truncate_pagecache(inode, oldsize, newsize);
  3958. ret = btrfs_cont_expand(inode, oldsize, newsize);
  3959. if (ret)
  3960. return ret;
  3961. trans = btrfs_start_transaction(root, 1);
  3962. if (IS_ERR(trans))
  3963. return PTR_ERR(trans);
  3964. i_size_write(inode, newsize);
  3965. btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
  3966. ret = btrfs_update_inode(trans, root, inode);
  3967. btrfs_end_transaction(trans, root);
  3968. } else {
  3969. /*
  3970. * We're truncating a file that used to have good data down to
  3971. * zero. Make sure it gets into the ordered flush list so that
  3972. * any new writes get down to disk quickly.
  3973. */
  3974. if (newsize == 0)
  3975. set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
  3976. &BTRFS_I(inode)->runtime_flags);
  3977. /*
  3978. * 1 for the orphan item we're going to add
  3979. * 1 for the orphan item deletion.
  3980. */
  3981. trans = btrfs_start_transaction(root, 2);
  3982. if (IS_ERR(trans))
  3983. return PTR_ERR(trans);
  3984. /*
  3985. * We need to do this in case we fail at _any_ point during the
  3986. * actual truncate. Once we do the truncate_setsize we could
  3987. * invalidate pages which forces any outstanding ordered io to
  3988. * be instantly completed which will give us extents that need
  3989. * to be truncated. If we fail to get an orphan inode down we
  3990. * could have left over extents that were never meant to live,
  3991. * so we need to garuntee from this point on that everything
  3992. * will be consistent.
  3993. */
  3994. ret = btrfs_orphan_add(trans, inode);
  3995. btrfs_end_transaction(trans, root);
  3996. if (ret)
  3997. return ret;
  3998. /* we don't support swapfiles, so vmtruncate shouldn't fail */
  3999. truncate_setsize(inode, newsize);
  4000. /* Disable nonlocked read DIO to avoid the end less truncate */
  4001. btrfs_inode_block_unlocked_dio(inode);
  4002. inode_dio_wait(inode);
  4003. btrfs_inode_resume_unlocked_dio(inode);
  4004. ret = btrfs_truncate(inode);
  4005. if (ret && inode->i_nlink)
  4006. btrfs_orphan_del(NULL, inode);
  4007. }
  4008. return ret;
  4009. }
  4010. static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
  4011. {
  4012. struct inode *inode = dentry->d_inode;
  4013. struct btrfs_root *root = BTRFS_I(inode)->root;
  4014. int err;
  4015. if (btrfs_root_readonly(root))
  4016. return -EROFS;
  4017. err = inode_change_ok(inode, attr);
  4018. if (err)
  4019. return err;
  4020. if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
  4021. err = btrfs_setsize(inode, attr);
  4022. if (err)
  4023. return err;
  4024. }
  4025. if (attr->ia_valid) {
  4026. setattr_copy(inode, attr);
  4027. inode_inc_iversion(inode);
  4028. err = btrfs_dirty_inode(inode);
  4029. if (!err && attr->ia_valid & ATTR_MODE)
  4030. err = btrfs_acl_chmod(inode);
  4031. }
  4032. return err;
  4033. }
  4034. void btrfs_evict_inode(struct inode *inode)
  4035. {
  4036. struct btrfs_trans_handle *trans;
  4037. struct btrfs_root *root = BTRFS_I(inode)->root;
  4038. struct btrfs_block_rsv *rsv, *global_rsv;
  4039. u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
  4040. int ret;
  4041. trace_btrfs_inode_evict(inode);
  4042. truncate_inode_pages(&inode->i_data, 0);
  4043. if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
  4044. btrfs_is_free_space_inode(inode)))
  4045. goto no_delete;
  4046. if (is_bad_inode(inode)) {
  4047. btrfs_orphan_del(NULL, inode);
  4048. goto no_delete;
  4049. }
  4050. /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
  4051. btrfs_wait_ordered_range(inode, 0, (u64)-1);
  4052. if (root->fs_info->log_root_recovering) {
  4053. BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
  4054. &BTRFS_I(inode)->runtime_flags));
  4055. goto no_delete;
  4056. }
  4057. if (inode->i_nlink > 0) {
  4058. BUG_ON(btrfs_root_refs(&root->root_item) != 0);
  4059. goto no_delete;
  4060. }
  4061. ret = btrfs_commit_inode_delayed_inode(inode);
  4062. if (ret) {
  4063. btrfs_orphan_del(NULL, inode);
  4064. goto no_delete;
  4065. }
  4066. rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
  4067. if (!rsv) {
  4068. btrfs_orphan_del(NULL, inode);
  4069. goto no_delete;
  4070. }
  4071. rsv->size = min_size;
  4072. rsv->failfast = 1;
  4073. global_rsv = &root->fs_info->global_block_rsv;
  4074. btrfs_i_size_write(inode, 0);
  4075. /*
  4076. * This is a bit simpler than btrfs_truncate since we've already
  4077. * reserved our space for our orphan item in the unlink, so we just
  4078. * need to reserve some slack space in case we add bytes and update
  4079. * inode item when doing the truncate.
  4080. */
  4081. while (1) {
  4082. ret = btrfs_block_rsv_refill(root, rsv, min_size,
  4083. BTRFS_RESERVE_FLUSH_LIMIT);
  4084. /*
  4085. * Try and steal from the global reserve since we will
  4086. * likely not use this space anyway, we want to try as
  4087. * hard as possible to get this to work.
  4088. */
  4089. if (ret)
  4090. ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
  4091. if (ret) {
  4092. printk(KERN_WARNING "Could not get space for a "
  4093. "delete, will truncate on mount %d\n", ret);
  4094. btrfs_orphan_del(NULL, inode);
  4095. btrfs_free_block_rsv(root, rsv);
  4096. goto no_delete;
  4097. }
  4098. trans = btrfs_join_transaction(root);
  4099. if (IS_ERR(trans)) {
  4100. btrfs_orphan_del(NULL, inode);
  4101. btrfs_free_block_rsv(root, rsv);
  4102. goto no_delete;
  4103. }
  4104. trans->block_rsv = rsv;
  4105. ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
  4106. if (ret != -ENOSPC)
  4107. break;
  4108. trans->block_rsv = &root->fs_info->trans_block_rsv;
  4109. btrfs_end_transaction(trans, root);
  4110. trans = NULL;
  4111. btrfs_btree_balance_dirty(root);
  4112. }
  4113. btrfs_free_block_rsv(root, rsv);
  4114. if (ret == 0) {
  4115. trans->block_rsv = root->orphan_block_rsv;
  4116. ret = btrfs_orphan_del(trans, inode);
  4117. BUG_ON(ret);
  4118. }
  4119. trans->block_rsv = &root->fs_info->trans_block_rsv;
  4120. if (!(root == root->fs_info->tree_root ||
  4121. root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
  4122. btrfs_return_ino(root, btrfs_ino(inode));
  4123. btrfs_end_transaction(trans, root);
  4124. btrfs_btree_balance_dirty(root);
  4125. no_delete:
  4126. clear_inode(inode);
  4127. return;
  4128. }
  4129. /*
  4130. * this returns the key found in the dir entry in the location pointer.
  4131. * If no dir entries were found, location->objectid is 0.
  4132. */
  4133. static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
  4134. struct btrfs_key *location)
  4135. {
  4136. const char *name = dentry->d_name.name;
  4137. int namelen = dentry->d_name.len;
  4138. struct btrfs_dir_item *di;
  4139. struct btrfs_path *path;
  4140. struct btrfs_root *root = BTRFS_I(dir)->root;
  4141. int ret = 0;
  4142. path = btrfs_alloc_path();
  4143. if (!path)
  4144. return -ENOMEM;
  4145. di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
  4146. namelen, 0);
  4147. if (IS_ERR(di))
  4148. ret = PTR_ERR(di);
  4149. if (IS_ERR_OR_NULL(di))
  4150. goto out_err;
  4151. btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
  4152. out:
  4153. btrfs_free_path(path);
  4154. return ret;
  4155. out_err:
  4156. location->objectid = 0;
  4157. goto out;
  4158. }
  4159. /*
  4160. * when we hit a tree root in a directory, the btrfs part of the inode
  4161. * needs to be changed to reflect the root directory of the tree root. This
  4162. * is kind of like crossing a mount point.
  4163. */
  4164. static int fixup_tree_root_location(struct btrfs_root *root,
  4165. struct inode *dir,
  4166. struct dentry *dentry,
  4167. struct btrfs_key *location,
  4168. struct btrfs_root **sub_root)
  4169. {
  4170. struct btrfs_path *path;
  4171. struct btrfs_root *new_root;
  4172. struct btrfs_root_ref *ref;
  4173. struct extent_buffer *leaf;
  4174. int ret;
  4175. int err = 0;
  4176. path = btrfs_alloc_path();
  4177. if (!path) {
  4178. err = -ENOMEM;
  4179. goto out;
  4180. }
  4181. err = -ENOENT;
  4182. ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
  4183. BTRFS_I(dir)->root->root_key.objectid,
  4184. location->objectid);
  4185. if (ret) {
  4186. if (ret < 0)
  4187. err = ret;
  4188. goto out;
  4189. }
  4190. leaf = path->nodes[0];
  4191. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
  4192. if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
  4193. btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
  4194. goto out;
  4195. ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
  4196. (unsigned long)(ref + 1),
  4197. dentry->d_name.len);
  4198. if (ret)
  4199. goto out;
  4200. btrfs_release_path(path);
  4201. new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
  4202. if (IS_ERR(new_root)) {
  4203. err = PTR_ERR(new_root);
  4204. goto out;
  4205. }
  4206. if (btrfs_root_refs(&new_root->root_item) == 0) {
  4207. err = -ENOENT;
  4208. goto out;
  4209. }
  4210. *sub_root = new_root;
  4211. location->objectid = btrfs_root_dirid(&new_root->root_item);
  4212. location->type = BTRFS_INODE_ITEM_KEY;
  4213. location->offset = 0;
  4214. err = 0;
  4215. out:
  4216. btrfs_free_path(path);
  4217. return err;
  4218. }
  4219. static void inode_tree_add(struct inode *inode)
  4220. {
  4221. struct btrfs_root *root = BTRFS_I(inode)->root;
  4222. struct btrfs_inode *entry;
  4223. struct rb_node **p;
  4224. struct rb_node *parent;
  4225. u64 ino = btrfs_ino(inode);
  4226. again:
  4227. p = &root->inode_tree.rb_node;
  4228. parent = NULL;
  4229. if (inode_unhashed(inode))
  4230. return;
  4231. spin_lock(&root->inode_lock);
  4232. while (*p) {
  4233. parent = *p;
  4234. entry = rb_entry(parent, struct btrfs_inode, rb_node);
  4235. if (ino < btrfs_ino(&entry->vfs_inode))
  4236. p = &parent->rb_left;
  4237. else if (ino > btrfs_ino(&entry->vfs_inode))
  4238. p = &parent->rb_right;
  4239. else {
  4240. WARN_ON(!(entry->vfs_inode.i_state &
  4241. (I_WILL_FREE | I_FREEING)));
  4242. rb_erase(parent, &root->inode_tree);
  4243. RB_CLEAR_NODE(parent);
  4244. spin_unlock(&root->inode_lock);
  4245. goto again;
  4246. }
  4247. }
  4248. rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
  4249. rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
  4250. spin_unlock(&root->inode_lock);
  4251. }
  4252. static void inode_tree_del(struct inode *inode)
  4253. {
  4254. struct btrfs_root *root = BTRFS_I(inode)->root;
  4255. int empty = 0;
  4256. spin_lock(&root->inode_lock);
  4257. if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
  4258. rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
  4259. RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
  4260. empty = RB_EMPTY_ROOT(&root->inode_tree);
  4261. }
  4262. spin_unlock(&root->inode_lock);
  4263. /*
  4264. * Free space cache has inodes in the tree root, but the tree root has a
  4265. * root_refs of 0, so this could end up dropping the tree root as a
  4266. * snapshot, so we need the extra !root->fs_info->tree_root check to
  4267. * make sure we don't drop it.
  4268. */
  4269. if (empty && btrfs_root_refs(&root->root_item) == 0 &&
  4270. root != root->fs_info->tree_root) {
  4271. synchronize_srcu(&root->fs_info->subvol_srcu);
  4272. spin_lock(&root->inode_lock);
  4273. empty = RB_EMPTY_ROOT(&root->inode_tree);
  4274. spin_unlock(&root->inode_lock);
  4275. if (empty)
  4276. btrfs_add_dead_root(root);
  4277. }
  4278. }
  4279. void btrfs_invalidate_inodes(struct btrfs_root *root)
  4280. {
  4281. struct rb_node *node;
  4282. struct rb_node *prev;
  4283. struct btrfs_inode *entry;
  4284. struct inode *inode;
  4285. u64 objectid = 0;
  4286. WARN_ON(btrfs_root_refs(&root->root_item) != 0);
  4287. spin_lock(&root->inode_lock);
  4288. again:
  4289. node = root->inode_tree.rb_node;
  4290. prev = NULL;
  4291. while (node) {
  4292. prev = node;
  4293. entry = rb_entry(node, struct btrfs_inode, rb_node);
  4294. if (objectid < btrfs_ino(&entry->vfs_inode))
  4295. node = node->rb_left;
  4296. else if (objectid > btrfs_ino(&entry->vfs_inode))
  4297. node = node->rb_right;
  4298. else
  4299. break;
  4300. }
  4301. if (!node) {
  4302. while (prev) {
  4303. entry = rb_entry(prev, struct btrfs_inode, rb_node);
  4304. if (objectid <= btrfs_ino(&entry->vfs_inode)) {
  4305. node = prev;
  4306. break;
  4307. }
  4308. prev = rb_next(prev);
  4309. }
  4310. }
  4311. while (node) {
  4312. entry = rb_entry(node, struct btrfs_inode, rb_node);
  4313. objectid = btrfs_ino(&entry->vfs_inode) + 1;
  4314. inode = igrab(&entry->vfs_inode);
  4315. if (inode) {
  4316. spin_unlock(&root->inode_lock);
  4317. if (atomic_read(&inode->i_count) > 1)
  4318. d_prune_aliases(inode);
  4319. /*
  4320. * btrfs_drop_inode will have it removed from
  4321. * the inode cache when its usage count
  4322. * hits zero.
  4323. */
  4324. iput(inode);
  4325. cond_resched();
  4326. spin_lock(&root->inode_lock);
  4327. goto again;
  4328. }
  4329. if (cond_resched_lock(&root->inode_lock))
  4330. goto again;
  4331. node = rb_next(node);
  4332. }
  4333. spin_unlock(&root->inode_lock);
  4334. }
  4335. static int btrfs_init_locked_inode(struct inode *inode, void *p)
  4336. {
  4337. struct btrfs_iget_args *args = p;
  4338. inode->i_ino = args->ino;
  4339. BTRFS_I(inode)->root = args->root;
  4340. return 0;
  4341. }
  4342. static int btrfs_find_actor(struct inode *inode, void *opaque)
  4343. {
  4344. struct btrfs_iget_args *args = opaque;
  4345. return args->ino == btrfs_ino(inode) &&
  4346. args->root == BTRFS_I(inode)->root;
  4347. }
  4348. static struct inode *btrfs_iget_locked(struct super_block *s,
  4349. u64 objectid,
  4350. struct btrfs_root *root)
  4351. {
  4352. struct inode *inode;
  4353. struct btrfs_iget_args args;
  4354. args.ino = objectid;
  4355. args.root = root;
  4356. inode = iget5_locked(s, objectid, btrfs_find_actor,
  4357. btrfs_init_locked_inode,
  4358. (void *)&args);
  4359. return inode;
  4360. }
  4361. /* Get an inode object given its location and corresponding root.
  4362. * Returns in *is_new if the inode was read from disk
  4363. */
  4364. struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
  4365. struct btrfs_root *root, int *new)
  4366. {
  4367. struct inode *inode;
  4368. inode = btrfs_iget_locked(s, location->objectid, root);
  4369. if (!inode)
  4370. return ERR_PTR(-ENOMEM);
  4371. if (inode->i_state & I_NEW) {
  4372. BTRFS_I(inode)->root = root;
  4373. memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
  4374. btrfs_read_locked_inode(inode);
  4375. if (!is_bad_inode(inode)) {
  4376. inode_tree_add(inode);
  4377. unlock_new_inode(inode);
  4378. if (new)
  4379. *new = 1;
  4380. } else {
  4381. unlock_new_inode(inode);
  4382. iput(inode);
  4383. inode = ERR_PTR(-ESTALE);
  4384. }
  4385. }
  4386. return inode;
  4387. }
  4388. static struct inode *new_simple_dir(struct super_block *s,
  4389. struct btrfs_key *key,
  4390. struct btrfs_root *root)
  4391. {
  4392. struct inode *inode = new_inode(s);
  4393. if (!inode)
  4394. return ERR_PTR(-ENOMEM);
  4395. BTRFS_I(inode)->root = root;
  4396. memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
  4397. set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
  4398. inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
  4399. inode->i_op = &btrfs_dir_ro_inode_operations;
  4400. inode->i_fop = &simple_dir_operations;
  4401. inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
  4402. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  4403. return inode;
  4404. }
  4405. struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
  4406. {
  4407. struct inode *inode;
  4408. struct btrfs_root *root = BTRFS_I(dir)->root;
  4409. struct btrfs_root *sub_root = root;
  4410. struct btrfs_key location;
  4411. int index;
  4412. int ret = 0;
  4413. if (dentry->d_name.len > BTRFS_NAME_LEN)
  4414. return ERR_PTR(-ENAMETOOLONG);
  4415. ret = btrfs_inode_by_name(dir, dentry, &location);
  4416. if (ret < 0)
  4417. return ERR_PTR(ret);
  4418. if (location.objectid == 0)
  4419. return NULL;
  4420. if (location.type == BTRFS_INODE_ITEM_KEY) {
  4421. inode = btrfs_iget(dir->i_sb, &location, root, NULL);
  4422. return inode;
  4423. }
  4424. BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
  4425. index = srcu_read_lock(&root->fs_info->subvol_srcu);
  4426. ret = fixup_tree_root_location(root, dir, dentry,
  4427. &location, &sub_root);
  4428. if (ret < 0) {
  4429. if (ret != -ENOENT)
  4430. inode = ERR_PTR(ret);
  4431. else
  4432. inode = new_simple_dir(dir->i_sb, &location, sub_root);
  4433. } else {
  4434. inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
  4435. }
  4436. srcu_read_unlock(&root->fs_info->subvol_srcu, index);
  4437. if (!IS_ERR(inode) && root != sub_root) {
  4438. down_read(&root->fs_info->cleanup_work_sem);
  4439. if (!(inode->i_sb->s_flags & MS_RDONLY))
  4440. ret = btrfs_orphan_cleanup(sub_root);
  4441. up_read(&root->fs_info->cleanup_work_sem);
  4442. if (ret)
  4443. inode = ERR_PTR(ret);
  4444. }
  4445. return inode;
  4446. }
  4447. static int btrfs_dentry_delete(const struct dentry *dentry)
  4448. {
  4449. struct btrfs_root *root;
  4450. struct inode *inode = dentry->d_inode;
  4451. if (!inode && !IS_ROOT(dentry))
  4452. inode = dentry->d_parent->d_inode;
  4453. if (inode) {
  4454. root = BTRFS_I(inode)->root;
  4455. if (btrfs_root_refs(&root->root_item) == 0)
  4456. return 1;
  4457. if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
  4458. return 1;
  4459. }
  4460. return 0;
  4461. }
  4462. static void btrfs_dentry_release(struct dentry *dentry)
  4463. {
  4464. if (dentry->d_fsdata)
  4465. kfree(dentry->d_fsdata);
  4466. }
  4467. static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
  4468. unsigned int flags)
  4469. {
  4470. struct dentry *ret;
  4471. ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
  4472. return ret;
  4473. }
  4474. unsigned char btrfs_filetype_table[] = {
  4475. DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
  4476. };
  4477. static int btrfs_real_readdir(struct file *filp, void *dirent,
  4478. filldir_t filldir)
  4479. {
  4480. struct inode *inode = file_inode(filp);
  4481. struct btrfs_root *root = BTRFS_I(inode)->root;
  4482. struct btrfs_item *item;
  4483. struct btrfs_dir_item *di;
  4484. struct btrfs_key key;
  4485. struct btrfs_key found_key;
  4486. struct btrfs_path *path;
  4487. struct list_head ins_list;
  4488. struct list_head del_list;
  4489. int ret;
  4490. struct extent_buffer *leaf;
  4491. int slot;
  4492. unsigned char d_type;
  4493. int over = 0;
  4494. u32 di_cur;
  4495. u32 di_total;
  4496. u32 di_len;
  4497. int key_type = BTRFS_DIR_INDEX_KEY;
  4498. char tmp_name[32];
  4499. char *name_ptr;
  4500. int name_len;
  4501. int is_curr = 0; /* filp->f_pos points to the current index? */
  4502. /* FIXME, use a real flag for deciding about the key type */
  4503. if (root->fs_info->tree_root == root)
  4504. key_type = BTRFS_DIR_ITEM_KEY;
  4505. /* special case for "." */
  4506. if (filp->f_pos == 0) {
  4507. over = filldir(dirent, ".", 1,
  4508. filp->f_pos, btrfs_ino(inode), DT_DIR);
  4509. if (over)
  4510. return 0;
  4511. filp->f_pos = 1;
  4512. }
  4513. /* special case for .., just use the back ref */
  4514. if (filp->f_pos == 1) {
  4515. u64 pino = parent_ino(filp->f_path.dentry);
  4516. over = filldir(dirent, "..", 2,
  4517. filp->f_pos, pino, DT_DIR);
  4518. if (over)
  4519. return 0;
  4520. filp->f_pos = 2;
  4521. }
  4522. path = btrfs_alloc_path();
  4523. if (!path)
  4524. return -ENOMEM;
  4525. path->reada = 1;
  4526. if (key_type == BTRFS_DIR_INDEX_KEY) {
  4527. INIT_LIST_HEAD(&ins_list);
  4528. INIT_LIST_HEAD(&del_list);
  4529. btrfs_get_delayed_items(inode, &ins_list, &del_list);
  4530. }
  4531. btrfs_set_key_type(&key, key_type);
  4532. key.offset = filp->f_pos;
  4533. key.objectid = btrfs_ino(inode);
  4534. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4535. if (ret < 0)
  4536. goto err;
  4537. while (1) {
  4538. leaf = path->nodes[0];
  4539. slot = path->slots[0];
  4540. if (slot >= btrfs_header_nritems(leaf)) {
  4541. ret = btrfs_next_leaf(root, path);
  4542. if (ret < 0)
  4543. goto err;
  4544. else if (ret > 0)
  4545. break;
  4546. continue;
  4547. }
  4548. item = btrfs_item_nr(leaf, slot);
  4549. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  4550. if (found_key.objectid != key.objectid)
  4551. break;
  4552. if (btrfs_key_type(&found_key) != key_type)
  4553. break;
  4554. if (found_key.offset < filp->f_pos)
  4555. goto next;
  4556. if (key_type == BTRFS_DIR_INDEX_KEY &&
  4557. btrfs_should_delete_dir_index(&del_list,
  4558. found_key.offset))
  4559. goto next;
  4560. filp->f_pos = found_key.offset;
  4561. is_curr = 1;
  4562. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  4563. di_cur = 0;
  4564. di_total = btrfs_item_size(leaf, item);
  4565. while (di_cur < di_total) {
  4566. struct btrfs_key location;
  4567. if (verify_dir_item(root, leaf, di))
  4568. break;
  4569. name_len = btrfs_dir_name_len(leaf, di);
  4570. if (name_len <= sizeof(tmp_name)) {
  4571. name_ptr = tmp_name;
  4572. } else {
  4573. name_ptr = kmalloc(name_len, GFP_NOFS);
  4574. if (!name_ptr) {
  4575. ret = -ENOMEM;
  4576. goto err;
  4577. }
  4578. }
  4579. read_extent_buffer(leaf, name_ptr,
  4580. (unsigned long)(di + 1), name_len);
  4581. d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
  4582. btrfs_dir_item_key_to_cpu(leaf, di, &location);
  4583. /* is this a reference to our own snapshot? If so
  4584. * skip it.
  4585. *
  4586. * In contrast to old kernels, we insert the snapshot's
  4587. * dir item and dir index after it has been created, so
  4588. * we won't find a reference to our own snapshot. We
  4589. * still keep the following code for backward
  4590. * compatibility.
  4591. */
  4592. if (location.type == BTRFS_ROOT_ITEM_KEY &&
  4593. location.objectid == root->root_key.objectid) {
  4594. over = 0;
  4595. goto skip;
  4596. }
  4597. over = filldir(dirent, name_ptr, name_len,
  4598. found_key.offset, location.objectid,
  4599. d_type);
  4600. skip:
  4601. if (name_ptr != tmp_name)
  4602. kfree(name_ptr);
  4603. if (over)
  4604. goto nopos;
  4605. di_len = btrfs_dir_name_len(leaf, di) +
  4606. btrfs_dir_data_len(leaf, di) + sizeof(*di);
  4607. di_cur += di_len;
  4608. di = (struct btrfs_dir_item *)((char *)di + di_len);
  4609. }
  4610. next:
  4611. path->slots[0]++;
  4612. }
  4613. if (key_type == BTRFS_DIR_INDEX_KEY) {
  4614. if (is_curr)
  4615. filp->f_pos++;
  4616. ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
  4617. &ins_list);
  4618. if (ret)
  4619. goto nopos;
  4620. }
  4621. /* Reached end of directory/root. Bump pos past the last item. */
  4622. if (key_type == BTRFS_DIR_INDEX_KEY)
  4623. /*
  4624. * 32-bit glibc will use getdents64, but then strtol -
  4625. * so the last number we can serve is this.
  4626. */
  4627. filp->f_pos = 0x7fffffff;
  4628. else
  4629. filp->f_pos++;
  4630. nopos:
  4631. ret = 0;
  4632. err:
  4633. if (key_type == BTRFS_DIR_INDEX_KEY)
  4634. btrfs_put_delayed_items(&ins_list, &del_list);
  4635. btrfs_free_path(path);
  4636. return ret;
  4637. }
  4638. int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
  4639. {
  4640. struct btrfs_root *root = BTRFS_I(inode)->root;
  4641. struct btrfs_trans_handle *trans;
  4642. int ret = 0;
  4643. bool nolock = false;
  4644. if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
  4645. return 0;
  4646. if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
  4647. nolock = true;
  4648. if (wbc->sync_mode == WB_SYNC_ALL) {
  4649. if (nolock)
  4650. trans = btrfs_join_transaction_nolock(root);
  4651. else
  4652. trans = btrfs_join_transaction(root);
  4653. if (IS_ERR(trans))
  4654. return PTR_ERR(trans);
  4655. ret = btrfs_commit_transaction(trans, root);
  4656. }
  4657. return ret;
  4658. }
  4659. /*
  4660. * This is somewhat expensive, updating the tree every time the
  4661. * inode changes. But, it is most likely to find the inode in cache.
  4662. * FIXME, needs more benchmarking...there are no reasons other than performance
  4663. * to keep or drop this code.
  4664. */
  4665. int btrfs_dirty_inode(struct inode *inode)
  4666. {
  4667. struct btrfs_root *root = BTRFS_I(inode)->root;
  4668. struct btrfs_trans_handle *trans;
  4669. int ret;
  4670. if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
  4671. return 0;
  4672. trans = btrfs_join_transaction(root);
  4673. if (IS_ERR(trans))
  4674. return PTR_ERR(trans);
  4675. ret = btrfs_update_inode(trans, root, inode);
  4676. if (ret && ret == -ENOSPC) {
  4677. /* whoops, lets try again with the full transaction */
  4678. btrfs_end_transaction(trans, root);
  4679. trans = btrfs_start_transaction(root, 1);
  4680. if (IS_ERR(trans))
  4681. return PTR_ERR(trans);
  4682. ret = btrfs_update_inode(trans, root, inode);
  4683. }
  4684. btrfs_end_transaction(trans, root);
  4685. if (BTRFS_I(inode)->delayed_node)
  4686. btrfs_balance_delayed_items(root);
  4687. return ret;
  4688. }
  4689. /*
  4690. * This is a copy of file_update_time. We need this so we can return error on
  4691. * ENOSPC for updating the inode in the case of file write and mmap writes.
  4692. */
  4693. static int btrfs_update_time(struct inode *inode, struct timespec *now,
  4694. int flags)
  4695. {
  4696. struct btrfs_root *root = BTRFS_I(inode)->root;
  4697. if (btrfs_root_readonly(root))
  4698. return -EROFS;
  4699. if (flags & S_VERSION)
  4700. inode_inc_iversion(inode);
  4701. if (flags & S_CTIME)
  4702. inode->i_ctime = *now;
  4703. if (flags & S_MTIME)
  4704. inode->i_mtime = *now;
  4705. if (flags & S_ATIME)
  4706. inode->i_atime = *now;
  4707. return btrfs_dirty_inode(inode);
  4708. }
  4709. /*
  4710. * find the highest existing sequence number in a directory
  4711. * and then set the in-memory index_cnt variable to reflect
  4712. * free sequence numbers
  4713. */
  4714. static int btrfs_set_inode_index_count(struct inode *inode)
  4715. {
  4716. struct btrfs_root *root = BTRFS_I(inode)->root;
  4717. struct btrfs_key key, found_key;
  4718. struct btrfs_path *path;
  4719. struct extent_buffer *leaf;
  4720. int ret;
  4721. key.objectid = btrfs_ino(inode);
  4722. btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
  4723. key.offset = (u64)-1;
  4724. path = btrfs_alloc_path();
  4725. if (!path)
  4726. return -ENOMEM;
  4727. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4728. if (ret < 0)
  4729. goto out;
  4730. /* FIXME: we should be able to handle this */
  4731. if (ret == 0)
  4732. goto out;
  4733. ret = 0;
  4734. /*
  4735. * MAGIC NUMBER EXPLANATION:
  4736. * since we search a directory based on f_pos we have to start at 2
  4737. * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
  4738. * else has to start at 2
  4739. */
  4740. if (path->slots[0] == 0) {
  4741. BTRFS_I(inode)->index_cnt = 2;
  4742. goto out;
  4743. }
  4744. path->slots[0]--;
  4745. leaf = path->nodes[0];
  4746. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  4747. if (found_key.objectid != btrfs_ino(inode) ||
  4748. btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
  4749. BTRFS_I(inode)->index_cnt = 2;
  4750. goto out;
  4751. }
  4752. BTRFS_I(inode)->index_cnt = found_key.offset + 1;
  4753. out:
  4754. btrfs_free_path(path);
  4755. return ret;
  4756. }
  4757. /*
  4758. * helper to find a free sequence number in a given directory. This current
  4759. * code is very simple, later versions will do smarter things in the btree
  4760. */
  4761. int btrfs_set_inode_index(struct inode *dir, u64 *index)
  4762. {
  4763. int ret = 0;
  4764. if (BTRFS_I(dir)->index_cnt == (u64)-1) {
  4765. ret = btrfs_inode_delayed_dir_index_count(dir);
  4766. if (ret) {
  4767. ret = btrfs_set_inode_index_count(dir);
  4768. if (ret)
  4769. return ret;
  4770. }
  4771. }
  4772. *index = BTRFS_I(dir)->index_cnt;
  4773. BTRFS_I(dir)->index_cnt++;
  4774. return ret;
  4775. }
  4776. static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
  4777. struct btrfs_root *root,
  4778. struct inode *dir,
  4779. const char *name, int name_len,
  4780. u64 ref_objectid, u64 objectid,
  4781. umode_t mode, u64 *index)
  4782. {
  4783. struct inode *inode;
  4784. struct btrfs_inode_item *inode_item;
  4785. struct btrfs_key *location;
  4786. struct btrfs_path *path;
  4787. struct btrfs_inode_ref *ref;
  4788. struct btrfs_key key[2];
  4789. u32 sizes[2];
  4790. unsigned long ptr;
  4791. int ret;
  4792. int owner;
  4793. path = btrfs_alloc_path();
  4794. if (!path)
  4795. return ERR_PTR(-ENOMEM);
  4796. inode = new_inode(root->fs_info->sb);
  4797. if (!inode) {
  4798. btrfs_free_path(path);
  4799. return ERR_PTR(-ENOMEM);
  4800. }
  4801. /*
  4802. * we have to initialize this early, so we can reclaim the inode
  4803. * number if we fail afterwards in this function.
  4804. */
  4805. inode->i_ino = objectid;
  4806. if (dir) {
  4807. trace_btrfs_inode_request(dir);
  4808. ret = btrfs_set_inode_index(dir, index);
  4809. if (ret) {
  4810. btrfs_free_path(path);
  4811. iput(inode);
  4812. return ERR_PTR(ret);
  4813. }
  4814. }
  4815. /*
  4816. * index_cnt is ignored for everything but a dir,
  4817. * btrfs_get_inode_index_count has an explanation for the magic
  4818. * number
  4819. */
  4820. BTRFS_I(inode)->index_cnt = 2;
  4821. BTRFS_I(inode)->root = root;
  4822. BTRFS_I(inode)->generation = trans->transid;
  4823. inode->i_generation = BTRFS_I(inode)->generation;
  4824. /*
  4825. * We could have gotten an inode number from somebody who was fsynced
  4826. * and then removed in this same transaction, so let's just set full
  4827. * sync since it will be a full sync anyway and this will blow away the
  4828. * old info in the log.
  4829. */
  4830. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
  4831. if (S_ISDIR(mode))
  4832. owner = 0;
  4833. else
  4834. owner = 1;
  4835. key[0].objectid = objectid;
  4836. btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
  4837. key[0].offset = 0;
  4838. /*
  4839. * Start new inodes with an inode_ref. This is slightly more
  4840. * efficient for small numbers of hard links since they will
  4841. * be packed into one item. Extended refs will kick in if we
  4842. * add more hard links than can fit in the ref item.
  4843. */
  4844. key[1].objectid = objectid;
  4845. btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
  4846. key[1].offset = ref_objectid;
  4847. sizes[0] = sizeof(struct btrfs_inode_item);
  4848. sizes[1] = name_len + sizeof(*ref);
  4849. path->leave_spinning = 1;
  4850. ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
  4851. if (ret != 0)
  4852. goto fail;
  4853. inode_init_owner(inode, dir, mode);
  4854. inode_set_bytes(inode, 0);
  4855. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  4856. inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  4857. struct btrfs_inode_item);
  4858. memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
  4859. sizeof(*inode_item));
  4860. fill_inode_item(trans, path->nodes[0], inode_item, inode);
  4861. ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
  4862. struct btrfs_inode_ref);
  4863. btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
  4864. btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
  4865. ptr = (unsigned long)(ref + 1);
  4866. write_extent_buffer(path->nodes[0], name, ptr, name_len);
  4867. btrfs_mark_buffer_dirty(path->nodes[0]);
  4868. btrfs_free_path(path);
  4869. location = &BTRFS_I(inode)->location;
  4870. location->objectid = objectid;
  4871. location->offset = 0;
  4872. btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
  4873. btrfs_inherit_iflags(inode, dir);
  4874. if (S_ISREG(mode)) {
  4875. if (btrfs_test_opt(root, NODATASUM))
  4876. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
  4877. if (btrfs_test_opt(root, NODATACOW))
  4878. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
  4879. BTRFS_INODE_NODATASUM;
  4880. }
  4881. insert_inode_hash(inode);
  4882. inode_tree_add(inode);
  4883. trace_btrfs_inode_new(inode);
  4884. btrfs_set_inode_last_trans(trans, inode);
  4885. btrfs_update_root_times(trans, root);
  4886. return inode;
  4887. fail:
  4888. if (dir)
  4889. BTRFS_I(dir)->index_cnt--;
  4890. btrfs_free_path(path);
  4891. iput(inode);
  4892. return ERR_PTR(ret);
  4893. }
  4894. static inline u8 btrfs_inode_type(struct inode *inode)
  4895. {
  4896. return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
  4897. }
  4898. /*
  4899. * utility function to add 'inode' into 'parent_inode' with
  4900. * a give name and a given sequence number.
  4901. * if 'add_backref' is true, also insert a backref from the
  4902. * inode to the parent directory.
  4903. */
  4904. int btrfs_add_link(struct btrfs_trans_handle *trans,
  4905. struct inode *parent_inode, struct inode *inode,
  4906. const char *name, int name_len, int add_backref, u64 index)
  4907. {
  4908. int ret = 0;
  4909. struct btrfs_key key;
  4910. struct btrfs_root *root = BTRFS_I(parent_inode)->root;
  4911. u64 ino = btrfs_ino(inode);
  4912. u64 parent_ino = btrfs_ino(parent_inode);
  4913. if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
  4914. memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
  4915. } else {
  4916. key.objectid = ino;
  4917. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  4918. key.offset = 0;
  4919. }
  4920. if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
  4921. ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
  4922. key.objectid, root->root_key.objectid,
  4923. parent_ino, index, name, name_len);
  4924. } else if (add_backref) {
  4925. ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
  4926. parent_ino, index);
  4927. }
  4928. /* Nothing to clean up yet */
  4929. if (ret)
  4930. return ret;
  4931. ret = btrfs_insert_dir_item(trans, root, name, name_len,
  4932. parent_inode, &key,
  4933. btrfs_inode_type(inode), index);
  4934. if (ret == -EEXIST || ret == -EOVERFLOW)
  4935. goto fail_dir_item;
  4936. else if (ret) {
  4937. btrfs_abort_transaction(trans, root, ret);
  4938. return ret;
  4939. }
  4940. btrfs_i_size_write(parent_inode, parent_inode->i_size +
  4941. name_len * 2);
  4942. inode_inc_iversion(parent_inode);
  4943. parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
  4944. ret = btrfs_update_inode(trans, root, parent_inode);
  4945. if (ret)
  4946. btrfs_abort_transaction(trans, root, ret);
  4947. return ret;
  4948. fail_dir_item:
  4949. if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
  4950. u64 local_index;
  4951. int err;
  4952. err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
  4953. key.objectid, root->root_key.objectid,
  4954. parent_ino, &local_index, name, name_len);
  4955. } else if (add_backref) {
  4956. u64 local_index;
  4957. int err;
  4958. err = btrfs_del_inode_ref(trans, root, name, name_len,
  4959. ino, parent_ino, &local_index);
  4960. }
  4961. return ret;
  4962. }
  4963. static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
  4964. struct inode *dir, struct dentry *dentry,
  4965. struct inode *inode, int backref, u64 index)
  4966. {
  4967. int err = btrfs_add_link(trans, dir, inode,
  4968. dentry->d_name.name, dentry->d_name.len,
  4969. backref, index);
  4970. if (err > 0)
  4971. err = -EEXIST;
  4972. return err;
  4973. }
  4974. static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
  4975. umode_t mode, dev_t rdev)
  4976. {
  4977. struct btrfs_trans_handle *trans;
  4978. struct btrfs_root *root = BTRFS_I(dir)->root;
  4979. struct inode *inode = NULL;
  4980. int err;
  4981. int drop_inode = 0;
  4982. u64 objectid;
  4983. u64 index = 0;
  4984. if (!new_valid_dev(rdev))
  4985. return -EINVAL;
  4986. /*
  4987. * 2 for inode item and ref
  4988. * 2 for dir items
  4989. * 1 for xattr if selinux is on
  4990. */
  4991. trans = btrfs_start_transaction(root, 5);
  4992. if (IS_ERR(trans))
  4993. return PTR_ERR(trans);
  4994. err = btrfs_find_free_ino(root, &objectid);
  4995. if (err)
  4996. goto out_unlock;
  4997. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  4998. dentry->d_name.len, btrfs_ino(dir), objectid,
  4999. mode, &index);
  5000. if (IS_ERR(inode)) {
  5001. err = PTR_ERR(inode);
  5002. goto out_unlock;
  5003. }
  5004. err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
  5005. if (err) {
  5006. drop_inode = 1;
  5007. goto out_unlock;
  5008. }
  5009. /*
  5010. * If the active LSM wants to access the inode during
  5011. * d_instantiate it needs these. Smack checks to see
  5012. * if the filesystem supports xattrs by looking at the
  5013. * ops vector.
  5014. */
  5015. inode->i_op = &btrfs_special_inode_operations;
  5016. err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
  5017. if (err)
  5018. drop_inode = 1;
  5019. else {
  5020. init_special_inode(inode, inode->i_mode, rdev);
  5021. btrfs_update_inode(trans, root, inode);
  5022. d_instantiate(dentry, inode);
  5023. }
  5024. out_unlock:
  5025. btrfs_end_transaction(trans, root);
  5026. btrfs_btree_balance_dirty(root);
  5027. if (drop_inode) {
  5028. inode_dec_link_count(inode);
  5029. iput(inode);
  5030. }
  5031. return err;
  5032. }
  5033. static int btrfs_create(struct inode *dir, struct dentry *dentry,
  5034. umode_t mode, bool excl)
  5035. {
  5036. struct btrfs_trans_handle *trans;
  5037. struct btrfs_root *root = BTRFS_I(dir)->root;
  5038. struct inode *inode = NULL;
  5039. int drop_inode_on_err = 0;
  5040. int err;
  5041. u64 objectid;
  5042. u64 index = 0;
  5043. /*
  5044. * 2 for inode item and ref
  5045. * 2 for dir items
  5046. * 1 for xattr if selinux is on
  5047. */
  5048. trans = btrfs_start_transaction(root, 5);
  5049. if (IS_ERR(trans))
  5050. return PTR_ERR(trans);
  5051. err = btrfs_find_free_ino(root, &objectid);
  5052. if (err)
  5053. goto out_unlock;
  5054. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  5055. dentry->d_name.len, btrfs_ino(dir), objectid,
  5056. mode, &index);
  5057. if (IS_ERR(inode)) {
  5058. err = PTR_ERR(inode);
  5059. goto out_unlock;
  5060. }
  5061. drop_inode_on_err = 1;
  5062. err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
  5063. if (err)
  5064. goto out_unlock;
  5065. err = btrfs_update_inode(trans, root, inode);
  5066. if (err)
  5067. goto out_unlock;
  5068. /*
  5069. * If the active LSM wants to access the inode during
  5070. * d_instantiate it needs these. Smack checks to see
  5071. * if the filesystem supports xattrs by looking at the
  5072. * ops vector.
  5073. */
  5074. inode->i_fop = &btrfs_file_operations;
  5075. inode->i_op = &btrfs_file_inode_operations;
  5076. err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
  5077. if (err)
  5078. goto out_unlock;
  5079. inode->i_mapping->a_ops = &btrfs_aops;
  5080. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  5081. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  5082. d_instantiate(dentry, inode);
  5083. out_unlock:
  5084. btrfs_end_transaction(trans, root);
  5085. if (err && drop_inode_on_err) {
  5086. inode_dec_link_count(inode);
  5087. iput(inode);
  5088. }
  5089. btrfs_btree_balance_dirty(root);
  5090. return err;
  5091. }
  5092. static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
  5093. struct dentry *dentry)
  5094. {
  5095. struct btrfs_trans_handle *trans;
  5096. struct btrfs_root *root = BTRFS_I(dir)->root;
  5097. struct inode *inode = old_dentry->d_inode;
  5098. u64 index;
  5099. int err;
  5100. int drop_inode = 0;
  5101. /* do not allow sys_link's with other subvols of the same device */
  5102. if (root->objectid != BTRFS_I(inode)->root->objectid)
  5103. return -EXDEV;
  5104. if (inode->i_nlink >= BTRFS_LINK_MAX)
  5105. return -EMLINK;
  5106. err = btrfs_set_inode_index(dir, &index);
  5107. if (err)
  5108. goto fail;
  5109. /*
  5110. * 2 items for inode and inode ref
  5111. * 2 items for dir items
  5112. * 1 item for parent inode
  5113. */
  5114. trans = btrfs_start_transaction(root, 5);
  5115. if (IS_ERR(trans)) {
  5116. err = PTR_ERR(trans);
  5117. goto fail;
  5118. }
  5119. btrfs_inc_nlink(inode);
  5120. inode_inc_iversion(inode);
  5121. inode->i_ctime = CURRENT_TIME;
  5122. ihold(inode);
  5123. set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
  5124. err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
  5125. if (err) {
  5126. drop_inode = 1;
  5127. } else {
  5128. struct dentry *parent = dentry->d_parent;
  5129. err = btrfs_update_inode(trans, root, inode);
  5130. if (err)
  5131. goto fail;
  5132. d_instantiate(dentry, inode);
  5133. btrfs_log_new_name(trans, inode, NULL, parent);
  5134. }
  5135. btrfs_end_transaction(trans, root);
  5136. fail:
  5137. if (drop_inode) {
  5138. inode_dec_link_count(inode);
  5139. iput(inode);
  5140. }
  5141. btrfs_btree_balance_dirty(root);
  5142. return err;
  5143. }
  5144. static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  5145. {
  5146. struct inode *inode = NULL;
  5147. struct btrfs_trans_handle *trans;
  5148. struct btrfs_root *root = BTRFS_I(dir)->root;
  5149. int err = 0;
  5150. int drop_on_err = 0;
  5151. u64 objectid = 0;
  5152. u64 index = 0;
  5153. /*
  5154. * 2 items for inode and ref
  5155. * 2 items for dir items
  5156. * 1 for xattr if selinux is on
  5157. */
  5158. trans = btrfs_start_transaction(root, 5);
  5159. if (IS_ERR(trans))
  5160. return PTR_ERR(trans);
  5161. err = btrfs_find_free_ino(root, &objectid);
  5162. if (err)
  5163. goto out_fail;
  5164. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  5165. dentry->d_name.len, btrfs_ino(dir), objectid,
  5166. S_IFDIR | mode, &index);
  5167. if (IS_ERR(inode)) {
  5168. err = PTR_ERR(inode);
  5169. goto out_fail;
  5170. }
  5171. drop_on_err = 1;
  5172. err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
  5173. if (err)
  5174. goto out_fail;
  5175. inode->i_op = &btrfs_dir_inode_operations;
  5176. inode->i_fop = &btrfs_dir_file_operations;
  5177. btrfs_i_size_write(inode, 0);
  5178. err = btrfs_update_inode(trans, root, inode);
  5179. if (err)
  5180. goto out_fail;
  5181. err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
  5182. dentry->d_name.len, 0, index);
  5183. if (err)
  5184. goto out_fail;
  5185. d_instantiate(dentry, inode);
  5186. drop_on_err = 0;
  5187. out_fail:
  5188. btrfs_end_transaction(trans, root);
  5189. if (drop_on_err)
  5190. iput(inode);
  5191. btrfs_btree_balance_dirty(root);
  5192. return err;
  5193. }
  5194. /* helper for btfs_get_extent. Given an existing extent in the tree,
  5195. * and an extent that you want to insert, deal with overlap and insert
  5196. * the new extent into the tree.
  5197. */
  5198. static int merge_extent_mapping(struct extent_map_tree *em_tree,
  5199. struct extent_map *existing,
  5200. struct extent_map *em,
  5201. u64 map_start, u64 map_len)
  5202. {
  5203. u64 start_diff;
  5204. BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
  5205. start_diff = map_start - em->start;
  5206. em->start = map_start;
  5207. em->len = map_len;
  5208. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  5209. !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  5210. em->block_start += start_diff;
  5211. em->block_len -= start_diff;
  5212. }
  5213. return add_extent_mapping(em_tree, em);
  5214. }
  5215. static noinline int uncompress_inline(struct btrfs_path *path,
  5216. struct inode *inode, struct page *page,
  5217. size_t pg_offset, u64 extent_offset,
  5218. struct btrfs_file_extent_item *item)
  5219. {
  5220. int ret;
  5221. struct extent_buffer *leaf = path->nodes[0];
  5222. char *tmp;
  5223. size_t max_size;
  5224. unsigned long inline_size;
  5225. unsigned long ptr;
  5226. int compress_type;
  5227. WARN_ON(pg_offset != 0);
  5228. compress_type = btrfs_file_extent_compression(leaf, item);
  5229. max_size = btrfs_file_extent_ram_bytes(leaf, item);
  5230. inline_size = btrfs_file_extent_inline_item_len(leaf,
  5231. btrfs_item_nr(leaf, path->slots[0]));
  5232. tmp = kmalloc(inline_size, GFP_NOFS);
  5233. if (!tmp)
  5234. return -ENOMEM;
  5235. ptr = btrfs_file_extent_inline_start(item);
  5236. read_extent_buffer(leaf, tmp, ptr, inline_size);
  5237. max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
  5238. ret = btrfs_decompress(compress_type, tmp, page,
  5239. extent_offset, inline_size, max_size);
  5240. if (ret) {
  5241. char *kaddr = kmap_atomic(page);
  5242. unsigned long copy_size = min_t(u64,
  5243. PAGE_CACHE_SIZE - pg_offset,
  5244. max_size - extent_offset);
  5245. memset(kaddr + pg_offset, 0, copy_size);
  5246. kunmap_atomic(kaddr);
  5247. }
  5248. kfree(tmp);
  5249. return 0;
  5250. }
  5251. /*
  5252. * a bit scary, this does extent mapping from logical file offset to the disk.
  5253. * the ugly parts come from merging extents from the disk with the in-ram
  5254. * representation. This gets more complex because of the data=ordered code,
  5255. * where the in-ram extents might be locked pending data=ordered completion.
  5256. *
  5257. * This also copies inline extents directly into the page.
  5258. */
  5259. struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
  5260. size_t pg_offset, u64 start, u64 len,
  5261. int create)
  5262. {
  5263. int ret;
  5264. int err = 0;
  5265. u64 bytenr;
  5266. u64 extent_start = 0;
  5267. u64 extent_end = 0;
  5268. u64 objectid = btrfs_ino(inode);
  5269. u32 found_type;
  5270. struct btrfs_path *path = NULL;
  5271. struct btrfs_root *root = BTRFS_I(inode)->root;
  5272. struct btrfs_file_extent_item *item;
  5273. struct extent_buffer *leaf;
  5274. struct btrfs_key found_key;
  5275. struct extent_map *em = NULL;
  5276. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  5277. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  5278. struct btrfs_trans_handle *trans = NULL;
  5279. int compress_type;
  5280. again:
  5281. read_lock(&em_tree->lock);
  5282. em = lookup_extent_mapping(em_tree, start, len);
  5283. if (em)
  5284. em->bdev = root->fs_info->fs_devices->latest_bdev;
  5285. read_unlock(&em_tree->lock);
  5286. if (em) {
  5287. if (em->start > start || em->start + em->len <= start)
  5288. free_extent_map(em);
  5289. else if (em->block_start == EXTENT_MAP_INLINE && page)
  5290. free_extent_map(em);
  5291. else
  5292. goto out;
  5293. }
  5294. em = alloc_extent_map();
  5295. if (!em) {
  5296. err = -ENOMEM;
  5297. goto out;
  5298. }
  5299. em->bdev = root->fs_info->fs_devices->latest_bdev;
  5300. em->start = EXTENT_MAP_HOLE;
  5301. em->orig_start = EXTENT_MAP_HOLE;
  5302. em->len = (u64)-1;
  5303. em->block_len = (u64)-1;
  5304. if (!path) {
  5305. path = btrfs_alloc_path();
  5306. if (!path) {
  5307. err = -ENOMEM;
  5308. goto out;
  5309. }
  5310. /*
  5311. * Chances are we'll be called again, so go ahead and do
  5312. * readahead
  5313. */
  5314. path->reada = 1;
  5315. }
  5316. ret = btrfs_lookup_file_extent(trans, root, path,
  5317. objectid, start, trans != NULL);
  5318. if (ret < 0) {
  5319. err = ret;
  5320. goto out;
  5321. }
  5322. if (ret != 0) {
  5323. if (path->slots[0] == 0)
  5324. goto not_found;
  5325. path->slots[0]--;
  5326. }
  5327. leaf = path->nodes[0];
  5328. item = btrfs_item_ptr(leaf, path->slots[0],
  5329. struct btrfs_file_extent_item);
  5330. /* are we inside the extent that was found? */
  5331. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  5332. found_type = btrfs_key_type(&found_key);
  5333. if (found_key.objectid != objectid ||
  5334. found_type != BTRFS_EXTENT_DATA_KEY) {
  5335. goto not_found;
  5336. }
  5337. found_type = btrfs_file_extent_type(leaf, item);
  5338. extent_start = found_key.offset;
  5339. compress_type = btrfs_file_extent_compression(leaf, item);
  5340. if (found_type == BTRFS_FILE_EXTENT_REG ||
  5341. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  5342. extent_end = extent_start +
  5343. btrfs_file_extent_num_bytes(leaf, item);
  5344. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  5345. size_t size;
  5346. size = btrfs_file_extent_inline_len(leaf, item);
  5347. extent_end = ALIGN(extent_start + size, root->sectorsize);
  5348. }
  5349. if (start >= extent_end) {
  5350. path->slots[0]++;
  5351. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  5352. ret = btrfs_next_leaf(root, path);
  5353. if (ret < 0) {
  5354. err = ret;
  5355. goto out;
  5356. }
  5357. if (ret > 0)
  5358. goto not_found;
  5359. leaf = path->nodes[0];
  5360. }
  5361. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  5362. if (found_key.objectid != objectid ||
  5363. found_key.type != BTRFS_EXTENT_DATA_KEY)
  5364. goto not_found;
  5365. if (start + len <= found_key.offset)
  5366. goto not_found;
  5367. em->start = start;
  5368. em->orig_start = start;
  5369. em->len = found_key.offset - start;
  5370. goto not_found_em;
  5371. }
  5372. if (found_type == BTRFS_FILE_EXTENT_REG ||
  5373. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  5374. em->start = extent_start;
  5375. em->len = extent_end - extent_start;
  5376. em->orig_start = extent_start -
  5377. btrfs_file_extent_offset(leaf, item);
  5378. em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
  5379. item);
  5380. bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
  5381. if (bytenr == 0) {
  5382. em->block_start = EXTENT_MAP_HOLE;
  5383. goto insert;
  5384. }
  5385. if (compress_type != BTRFS_COMPRESS_NONE) {
  5386. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  5387. em->compress_type = compress_type;
  5388. em->block_start = bytenr;
  5389. em->block_len = em->orig_block_len;
  5390. } else {
  5391. bytenr += btrfs_file_extent_offset(leaf, item);
  5392. em->block_start = bytenr;
  5393. em->block_len = em->len;
  5394. if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
  5395. set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
  5396. }
  5397. goto insert;
  5398. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  5399. unsigned long ptr;
  5400. char *map;
  5401. size_t size;
  5402. size_t extent_offset;
  5403. size_t copy_size;
  5404. em->block_start = EXTENT_MAP_INLINE;
  5405. if (!page || create) {
  5406. em->start = extent_start;
  5407. em->len = extent_end - extent_start;
  5408. goto out;
  5409. }
  5410. size = btrfs_file_extent_inline_len(leaf, item);
  5411. extent_offset = page_offset(page) + pg_offset - extent_start;
  5412. copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
  5413. size - extent_offset);
  5414. em->start = extent_start + extent_offset;
  5415. em->len = ALIGN(copy_size, root->sectorsize);
  5416. em->orig_block_len = em->len;
  5417. em->orig_start = em->start;
  5418. if (compress_type) {
  5419. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  5420. em->compress_type = compress_type;
  5421. }
  5422. ptr = btrfs_file_extent_inline_start(item) + extent_offset;
  5423. if (create == 0 && !PageUptodate(page)) {
  5424. if (btrfs_file_extent_compression(leaf, item) !=
  5425. BTRFS_COMPRESS_NONE) {
  5426. ret = uncompress_inline(path, inode, page,
  5427. pg_offset,
  5428. extent_offset, item);
  5429. BUG_ON(ret); /* -ENOMEM */
  5430. } else {
  5431. map = kmap(page);
  5432. read_extent_buffer(leaf, map + pg_offset, ptr,
  5433. copy_size);
  5434. if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
  5435. memset(map + pg_offset + copy_size, 0,
  5436. PAGE_CACHE_SIZE - pg_offset -
  5437. copy_size);
  5438. }
  5439. kunmap(page);
  5440. }
  5441. flush_dcache_page(page);
  5442. } else if (create && PageUptodate(page)) {
  5443. BUG();
  5444. if (!trans) {
  5445. kunmap(page);
  5446. free_extent_map(em);
  5447. em = NULL;
  5448. btrfs_release_path(path);
  5449. trans = btrfs_join_transaction(root);
  5450. if (IS_ERR(trans))
  5451. return ERR_CAST(trans);
  5452. goto again;
  5453. }
  5454. map = kmap(page);
  5455. write_extent_buffer(leaf, map + pg_offset, ptr,
  5456. copy_size);
  5457. kunmap(page);
  5458. btrfs_mark_buffer_dirty(leaf);
  5459. }
  5460. set_extent_uptodate(io_tree, em->start,
  5461. extent_map_end(em) - 1, NULL, GFP_NOFS);
  5462. goto insert;
  5463. } else {
  5464. WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
  5465. }
  5466. not_found:
  5467. em->start = start;
  5468. em->orig_start = start;
  5469. em->len = len;
  5470. not_found_em:
  5471. em->block_start = EXTENT_MAP_HOLE;
  5472. set_bit(EXTENT_FLAG_VACANCY, &em->flags);
  5473. insert:
  5474. btrfs_release_path(path);
  5475. if (em->start > start || extent_map_end(em) <= start) {
  5476. printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
  5477. "[%llu %llu]\n", (unsigned long long)em->start,
  5478. (unsigned long long)em->len,
  5479. (unsigned long long)start,
  5480. (unsigned long long)len);
  5481. err = -EIO;
  5482. goto out;
  5483. }
  5484. err = 0;
  5485. write_lock(&em_tree->lock);
  5486. ret = add_extent_mapping(em_tree, em);
  5487. /* it is possible that someone inserted the extent into the tree
  5488. * while we had the lock dropped. It is also possible that
  5489. * an overlapping map exists in the tree
  5490. */
  5491. if (ret == -EEXIST) {
  5492. struct extent_map *existing;
  5493. ret = 0;
  5494. existing = lookup_extent_mapping(em_tree, start, len);
  5495. if (existing && (existing->start > start ||
  5496. existing->start + existing->len <= start)) {
  5497. free_extent_map(existing);
  5498. existing = NULL;
  5499. }
  5500. if (!existing) {
  5501. existing = lookup_extent_mapping(em_tree, em->start,
  5502. em->len);
  5503. if (existing) {
  5504. err = merge_extent_mapping(em_tree, existing,
  5505. em, start,
  5506. root->sectorsize);
  5507. free_extent_map(existing);
  5508. if (err) {
  5509. free_extent_map(em);
  5510. em = NULL;
  5511. }
  5512. } else {
  5513. err = -EIO;
  5514. free_extent_map(em);
  5515. em = NULL;
  5516. }
  5517. } else {
  5518. free_extent_map(em);
  5519. em = existing;
  5520. err = 0;
  5521. }
  5522. }
  5523. write_unlock(&em_tree->lock);
  5524. out:
  5525. if (em)
  5526. trace_btrfs_get_extent(root, em);
  5527. if (path)
  5528. btrfs_free_path(path);
  5529. if (trans) {
  5530. ret = btrfs_end_transaction(trans, root);
  5531. if (!err)
  5532. err = ret;
  5533. }
  5534. if (err) {
  5535. free_extent_map(em);
  5536. return ERR_PTR(err);
  5537. }
  5538. BUG_ON(!em); /* Error is always set */
  5539. return em;
  5540. }
  5541. struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
  5542. size_t pg_offset, u64 start, u64 len,
  5543. int create)
  5544. {
  5545. struct extent_map *em;
  5546. struct extent_map *hole_em = NULL;
  5547. u64 range_start = start;
  5548. u64 end;
  5549. u64 found;
  5550. u64 found_end;
  5551. int err = 0;
  5552. em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
  5553. if (IS_ERR(em))
  5554. return em;
  5555. if (em) {
  5556. /*
  5557. * if our em maps to
  5558. * - a hole or
  5559. * - a pre-alloc extent,
  5560. * there might actually be delalloc bytes behind it.
  5561. */
  5562. if (em->block_start != EXTENT_MAP_HOLE &&
  5563. !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  5564. return em;
  5565. else
  5566. hole_em = em;
  5567. }
  5568. /* check to see if we've wrapped (len == -1 or similar) */
  5569. end = start + len;
  5570. if (end < start)
  5571. end = (u64)-1;
  5572. else
  5573. end -= 1;
  5574. em = NULL;
  5575. /* ok, we didn't find anything, lets look for delalloc */
  5576. found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
  5577. end, len, EXTENT_DELALLOC, 1);
  5578. found_end = range_start + found;
  5579. if (found_end < range_start)
  5580. found_end = (u64)-1;
  5581. /*
  5582. * we didn't find anything useful, return
  5583. * the original results from get_extent()
  5584. */
  5585. if (range_start > end || found_end <= start) {
  5586. em = hole_em;
  5587. hole_em = NULL;
  5588. goto out;
  5589. }
  5590. /* adjust the range_start to make sure it doesn't
  5591. * go backwards from the start they passed in
  5592. */
  5593. range_start = max(start,range_start);
  5594. found = found_end - range_start;
  5595. if (found > 0) {
  5596. u64 hole_start = start;
  5597. u64 hole_len = len;
  5598. em = alloc_extent_map();
  5599. if (!em) {
  5600. err = -ENOMEM;
  5601. goto out;
  5602. }
  5603. /*
  5604. * when btrfs_get_extent can't find anything it
  5605. * returns one huge hole
  5606. *
  5607. * make sure what it found really fits our range, and
  5608. * adjust to make sure it is based on the start from
  5609. * the caller
  5610. */
  5611. if (hole_em) {
  5612. u64 calc_end = extent_map_end(hole_em);
  5613. if (calc_end <= start || (hole_em->start > end)) {
  5614. free_extent_map(hole_em);
  5615. hole_em = NULL;
  5616. } else {
  5617. hole_start = max(hole_em->start, start);
  5618. hole_len = calc_end - hole_start;
  5619. }
  5620. }
  5621. em->bdev = NULL;
  5622. if (hole_em && range_start > hole_start) {
  5623. /* our hole starts before our delalloc, so we
  5624. * have to return just the parts of the hole
  5625. * that go until the delalloc starts
  5626. */
  5627. em->len = min(hole_len,
  5628. range_start - hole_start);
  5629. em->start = hole_start;
  5630. em->orig_start = hole_start;
  5631. /*
  5632. * don't adjust block start at all,
  5633. * it is fixed at EXTENT_MAP_HOLE
  5634. */
  5635. em->block_start = hole_em->block_start;
  5636. em->block_len = hole_len;
  5637. if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
  5638. set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
  5639. } else {
  5640. em->start = range_start;
  5641. em->len = found;
  5642. em->orig_start = range_start;
  5643. em->block_start = EXTENT_MAP_DELALLOC;
  5644. em->block_len = found;
  5645. }
  5646. } else if (hole_em) {
  5647. return hole_em;
  5648. }
  5649. out:
  5650. free_extent_map(hole_em);
  5651. if (err) {
  5652. free_extent_map(em);
  5653. return ERR_PTR(err);
  5654. }
  5655. return em;
  5656. }
  5657. static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
  5658. u64 start, u64 len)
  5659. {
  5660. struct btrfs_root *root = BTRFS_I(inode)->root;
  5661. struct btrfs_trans_handle *trans;
  5662. struct extent_map *em;
  5663. struct btrfs_key ins;
  5664. u64 alloc_hint;
  5665. int ret;
  5666. trans = btrfs_join_transaction(root);
  5667. if (IS_ERR(trans))
  5668. return ERR_CAST(trans);
  5669. trans->block_rsv = &root->fs_info->delalloc_block_rsv;
  5670. alloc_hint = get_extent_allocation_hint(inode, start, len);
  5671. ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
  5672. alloc_hint, &ins, 1);
  5673. if (ret) {
  5674. em = ERR_PTR(ret);
  5675. goto out;
  5676. }
  5677. em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
  5678. ins.offset, ins.offset, 0);
  5679. if (IS_ERR(em))
  5680. goto out;
  5681. ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
  5682. ins.offset, ins.offset, 0);
  5683. if (ret) {
  5684. btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
  5685. em = ERR_PTR(ret);
  5686. }
  5687. out:
  5688. btrfs_end_transaction(trans, root);
  5689. return em;
  5690. }
  5691. /*
  5692. * returns 1 when the nocow is safe, < 1 on error, 0 if the
  5693. * block must be cow'd
  5694. */
  5695. static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
  5696. struct inode *inode, u64 offset, u64 len)
  5697. {
  5698. struct btrfs_path *path;
  5699. int ret;
  5700. struct extent_buffer *leaf;
  5701. struct btrfs_root *root = BTRFS_I(inode)->root;
  5702. struct btrfs_file_extent_item *fi;
  5703. struct btrfs_key key;
  5704. u64 disk_bytenr;
  5705. u64 backref_offset;
  5706. u64 extent_end;
  5707. u64 num_bytes;
  5708. int slot;
  5709. int found_type;
  5710. path = btrfs_alloc_path();
  5711. if (!path)
  5712. return -ENOMEM;
  5713. ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
  5714. offset, 0);
  5715. if (ret < 0)
  5716. goto out;
  5717. slot = path->slots[0];
  5718. if (ret == 1) {
  5719. if (slot == 0) {
  5720. /* can't find the item, must cow */
  5721. ret = 0;
  5722. goto out;
  5723. }
  5724. slot--;
  5725. }
  5726. ret = 0;
  5727. leaf = path->nodes[0];
  5728. btrfs_item_key_to_cpu(leaf, &key, slot);
  5729. if (key.objectid != btrfs_ino(inode) ||
  5730. key.type != BTRFS_EXTENT_DATA_KEY) {
  5731. /* not our file or wrong item type, must cow */
  5732. goto out;
  5733. }
  5734. if (key.offset > offset) {
  5735. /* Wrong offset, must cow */
  5736. goto out;
  5737. }
  5738. fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  5739. found_type = btrfs_file_extent_type(leaf, fi);
  5740. if (found_type != BTRFS_FILE_EXTENT_REG &&
  5741. found_type != BTRFS_FILE_EXTENT_PREALLOC) {
  5742. /* not a regular extent, must cow */
  5743. goto out;
  5744. }
  5745. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  5746. backref_offset = btrfs_file_extent_offset(leaf, fi);
  5747. extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
  5748. if (extent_end < offset + len) {
  5749. /* extent doesn't include our full range, must cow */
  5750. goto out;
  5751. }
  5752. if (btrfs_extent_readonly(root, disk_bytenr))
  5753. goto out;
  5754. /*
  5755. * look for other files referencing this extent, if we
  5756. * find any we must cow
  5757. */
  5758. if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
  5759. key.offset - backref_offset, disk_bytenr))
  5760. goto out;
  5761. /*
  5762. * adjust disk_bytenr and num_bytes to cover just the bytes
  5763. * in this extent we are about to write. If there
  5764. * are any csums in that range we have to cow in order
  5765. * to keep the csums correct
  5766. */
  5767. disk_bytenr += backref_offset;
  5768. disk_bytenr += offset - key.offset;
  5769. num_bytes = min(offset + len, extent_end) - offset;
  5770. if (csum_exist_in_range(root, disk_bytenr, num_bytes))
  5771. goto out;
  5772. /*
  5773. * all of the above have passed, it is safe to overwrite this extent
  5774. * without cow
  5775. */
  5776. ret = 1;
  5777. out:
  5778. btrfs_free_path(path);
  5779. return ret;
  5780. }
  5781. static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
  5782. struct extent_state **cached_state, int writing)
  5783. {
  5784. struct btrfs_ordered_extent *ordered;
  5785. int ret = 0;
  5786. while (1) {
  5787. lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  5788. 0, cached_state);
  5789. /*
  5790. * We're concerned with the entire range that we're going to be
  5791. * doing DIO to, so we need to make sure theres no ordered
  5792. * extents in this range.
  5793. */
  5794. ordered = btrfs_lookup_ordered_range(inode, lockstart,
  5795. lockend - lockstart + 1);
  5796. /*
  5797. * We need to make sure there are no buffered pages in this
  5798. * range either, we could have raced between the invalidate in
  5799. * generic_file_direct_write and locking the extent. The
  5800. * invalidate needs to happen so that reads after a write do not
  5801. * get stale data.
  5802. */
  5803. if (!ordered && (!writing ||
  5804. !test_range_bit(&BTRFS_I(inode)->io_tree,
  5805. lockstart, lockend, EXTENT_UPTODATE, 0,
  5806. *cached_state)))
  5807. break;
  5808. unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  5809. cached_state, GFP_NOFS);
  5810. if (ordered) {
  5811. btrfs_start_ordered_extent(inode, ordered, 1);
  5812. btrfs_put_ordered_extent(ordered);
  5813. } else {
  5814. /* Screw you mmap */
  5815. ret = filemap_write_and_wait_range(inode->i_mapping,
  5816. lockstart,
  5817. lockend);
  5818. if (ret)
  5819. break;
  5820. /*
  5821. * If we found a page that couldn't be invalidated just
  5822. * fall back to buffered.
  5823. */
  5824. ret = invalidate_inode_pages2_range(inode->i_mapping,
  5825. lockstart >> PAGE_CACHE_SHIFT,
  5826. lockend >> PAGE_CACHE_SHIFT);
  5827. if (ret)
  5828. break;
  5829. }
  5830. cond_resched();
  5831. }
  5832. return ret;
  5833. }
  5834. static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
  5835. u64 len, u64 orig_start,
  5836. u64 block_start, u64 block_len,
  5837. u64 orig_block_len, int type)
  5838. {
  5839. struct extent_map_tree *em_tree;
  5840. struct extent_map *em;
  5841. struct btrfs_root *root = BTRFS_I(inode)->root;
  5842. int ret;
  5843. em_tree = &BTRFS_I(inode)->extent_tree;
  5844. em = alloc_extent_map();
  5845. if (!em)
  5846. return ERR_PTR(-ENOMEM);
  5847. em->start = start;
  5848. em->orig_start = orig_start;
  5849. em->mod_start = start;
  5850. em->mod_len = len;
  5851. em->len = len;
  5852. em->block_len = block_len;
  5853. em->block_start = block_start;
  5854. em->bdev = root->fs_info->fs_devices->latest_bdev;
  5855. em->orig_block_len = orig_block_len;
  5856. em->generation = -1;
  5857. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  5858. if (type == BTRFS_ORDERED_PREALLOC)
  5859. set_bit(EXTENT_FLAG_FILLING, &em->flags);
  5860. do {
  5861. btrfs_drop_extent_cache(inode, em->start,
  5862. em->start + em->len - 1, 0);
  5863. write_lock(&em_tree->lock);
  5864. ret = add_extent_mapping(em_tree, em);
  5865. if (!ret)
  5866. list_move(&em->list,
  5867. &em_tree->modified_extents);
  5868. write_unlock(&em_tree->lock);
  5869. } while (ret == -EEXIST);
  5870. if (ret) {
  5871. free_extent_map(em);
  5872. return ERR_PTR(ret);
  5873. }
  5874. return em;
  5875. }
  5876. static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
  5877. struct buffer_head *bh_result, int create)
  5878. {
  5879. struct extent_map *em;
  5880. struct btrfs_root *root = BTRFS_I(inode)->root;
  5881. struct extent_state *cached_state = NULL;
  5882. u64 start = iblock << inode->i_blkbits;
  5883. u64 lockstart, lockend;
  5884. u64 len = bh_result->b_size;
  5885. struct btrfs_trans_handle *trans;
  5886. int unlock_bits = EXTENT_LOCKED;
  5887. int ret = 0;
  5888. if (create)
  5889. unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
  5890. else
  5891. len = min_t(u64, len, root->sectorsize);
  5892. lockstart = start;
  5893. lockend = start + len - 1;
  5894. /*
  5895. * If this errors out it's because we couldn't invalidate pagecache for
  5896. * this range and we need to fallback to buffered.
  5897. */
  5898. if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
  5899. return -ENOTBLK;
  5900. em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
  5901. if (IS_ERR(em)) {
  5902. ret = PTR_ERR(em);
  5903. goto unlock_err;
  5904. }
  5905. /*
  5906. * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
  5907. * io. INLINE is special, and we could probably kludge it in here, but
  5908. * it's still buffered so for safety lets just fall back to the generic
  5909. * buffered path.
  5910. *
  5911. * For COMPRESSED we _have_ to read the entire extent in so we can
  5912. * decompress it, so there will be buffering required no matter what we
  5913. * do, so go ahead and fallback to buffered.
  5914. *
  5915. * We return -ENOTBLK because thats what makes DIO go ahead and go back
  5916. * to buffered IO. Don't blame me, this is the price we pay for using
  5917. * the generic code.
  5918. */
  5919. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
  5920. em->block_start == EXTENT_MAP_INLINE) {
  5921. free_extent_map(em);
  5922. ret = -ENOTBLK;
  5923. goto unlock_err;
  5924. }
  5925. /* Just a good old fashioned hole, return */
  5926. if (!create && (em->block_start == EXTENT_MAP_HOLE ||
  5927. test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
  5928. free_extent_map(em);
  5929. goto unlock_err;
  5930. }
  5931. /*
  5932. * We don't allocate a new extent in the following cases
  5933. *
  5934. * 1) The inode is marked as NODATACOW. In this case we'll just use the
  5935. * existing extent.
  5936. * 2) The extent is marked as PREALLOC. We're good to go here and can
  5937. * just use the extent.
  5938. *
  5939. */
  5940. if (!create) {
  5941. len = min(len, em->len - (start - em->start));
  5942. lockstart = start + len;
  5943. goto unlock;
  5944. }
  5945. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
  5946. ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
  5947. em->block_start != EXTENT_MAP_HOLE)) {
  5948. int type;
  5949. int ret;
  5950. u64 block_start;
  5951. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  5952. type = BTRFS_ORDERED_PREALLOC;
  5953. else
  5954. type = BTRFS_ORDERED_NOCOW;
  5955. len = min(len, em->len - (start - em->start));
  5956. block_start = em->block_start + (start - em->start);
  5957. /*
  5958. * we're not going to log anything, but we do need
  5959. * to make sure the current transaction stays open
  5960. * while we look for nocow cross refs
  5961. */
  5962. trans = btrfs_join_transaction(root);
  5963. if (IS_ERR(trans))
  5964. goto must_cow;
  5965. if (can_nocow_odirect(trans, inode, start, len) == 1) {
  5966. u64 orig_start = em->orig_start;
  5967. u64 orig_block_len = em->orig_block_len;
  5968. if (type == BTRFS_ORDERED_PREALLOC) {
  5969. free_extent_map(em);
  5970. em = create_pinned_em(inode, start, len,
  5971. orig_start,
  5972. block_start, len,
  5973. orig_block_len, type);
  5974. if (IS_ERR(em)) {
  5975. btrfs_end_transaction(trans, root);
  5976. goto unlock_err;
  5977. }
  5978. }
  5979. ret = btrfs_add_ordered_extent_dio(inode, start,
  5980. block_start, len, len, type);
  5981. btrfs_end_transaction(trans, root);
  5982. if (ret) {
  5983. free_extent_map(em);
  5984. goto unlock_err;
  5985. }
  5986. goto unlock;
  5987. }
  5988. btrfs_end_transaction(trans, root);
  5989. }
  5990. must_cow:
  5991. /*
  5992. * this will cow the extent, reset the len in case we changed
  5993. * it above
  5994. */
  5995. len = bh_result->b_size;
  5996. free_extent_map(em);
  5997. em = btrfs_new_extent_direct(inode, start, len);
  5998. if (IS_ERR(em)) {
  5999. ret = PTR_ERR(em);
  6000. goto unlock_err;
  6001. }
  6002. len = min(len, em->len - (start - em->start));
  6003. unlock:
  6004. bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
  6005. inode->i_blkbits;
  6006. bh_result->b_size = len;
  6007. bh_result->b_bdev = em->bdev;
  6008. set_buffer_mapped(bh_result);
  6009. if (create) {
  6010. if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  6011. set_buffer_new(bh_result);
  6012. /*
  6013. * Need to update the i_size under the extent lock so buffered
  6014. * readers will get the updated i_size when we unlock.
  6015. */
  6016. if (start + len > i_size_read(inode))
  6017. i_size_write(inode, start + len);
  6018. spin_lock(&BTRFS_I(inode)->lock);
  6019. BTRFS_I(inode)->outstanding_extents++;
  6020. spin_unlock(&BTRFS_I(inode)->lock);
  6021. ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
  6022. lockstart + len - 1, EXTENT_DELALLOC, NULL,
  6023. &cached_state, GFP_NOFS);
  6024. BUG_ON(ret);
  6025. }
  6026. /*
  6027. * In the case of write we need to clear and unlock the entire range,
  6028. * in the case of read we need to unlock only the end area that we
  6029. * aren't using if there is any left over space.
  6030. */
  6031. if (lockstart < lockend) {
  6032. clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
  6033. lockend, unlock_bits, 1, 0,
  6034. &cached_state, GFP_NOFS);
  6035. } else {
  6036. free_extent_state(cached_state);
  6037. }
  6038. free_extent_map(em);
  6039. return 0;
  6040. unlock_err:
  6041. clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  6042. unlock_bits, 1, 0, &cached_state, GFP_NOFS);
  6043. return ret;
  6044. }
  6045. struct btrfs_dio_private {
  6046. struct inode *inode;
  6047. u64 logical_offset;
  6048. u64 disk_bytenr;
  6049. u64 bytes;
  6050. void *private;
  6051. /* number of bios pending for this dio */
  6052. atomic_t pending_bios;
  6053. /* IO errors */
  6054. int errors;
  6055. struct bio *orig_bio;
  6056. };
  6057. static void btrfs_endio_direct_read(struct bio *bio, int err)
  6058. {
  6059. struct btrfs_dio_private *dip = bio->bi_private;
  6060. struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
  6061. struct bio_vec *bvec = bio->bi_io_vec;
  6062. struct inode *inode = dip->inode;
  6063. struct btrfs_root *root = BTRFS_I(inode)->root;
  6064. u64 start;
  6065. start = dip->logical_offset;
  6066. do {
  6067. if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
  6068. struct page *page = bvec->bv_page;
  6069. char *kaddr;
  6070. u32 csum = ~(u32)0;
  6071. u64 private = ~(u32)0;
  6072. unsigned long flags;
  6073. if (get_state_private(&BTRFS_I(inode)->io_tree,
  6074. start, &private))
  6075. goto failed;
  6076. local_irq_save(flags);
  6077. kaddr = kmap_atomic(page);
  6078. csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
  6079. csum, bvec->bv_len);
  6080. btrfs_csum_final(csum, (char *)&csum);
  6081. kunmap_atomic(kaddr);
  6082. local_irq_restore(flags);
  6083. flush_dcache_page(bvec->bv_page);
  6084. if (csum != private) {
  6085. failed:
  6086. printk(KERN_ERR "btrfs csum failed ino %llu off"
  6087. " %llu csum %u private %u\n",
  6088. (unsigned long long)btrfs_ino(inode),
  6089. (unsigned long long)start,
  6090. csum, (unsigned)private);
  6091. err = -EIO;
  6092. }
  6093. }
  6094. start += bvec->bv_len;
  6095. bvec++;
  6096. } while (bvec <= bvec_end);
  6097. unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
  6098. dip->logical_offset + dip->bytes - 1);
  6099. bio->bi_private = dip->private;
  6100. kfree(dip);
  6101. /* If we had a csum failure make sure to clear the uptodate flag */
  6102. if (err)
  6103. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  6104. dio_end_io(bio, err);
  6105. }
  6106. static void btrfs_endio_direct_write(struct bio *bio, int err)
  6107. {
  6108. struct btrfs_dio_private *dip = bio->bi_private;
  6109. struct inode *inode = dip->inode;
  6110. struct btrfs_root *root = BTRFS_I(inode)->root;
  6111. struct btrfs_ordered_extent *ordered = NULL;
  6112. u64 ordered_offset = dip->logical_offset;
  6113. u64 ordered_bytes = dip->bytes;
  6114. int ret;
  6115. if (err)
  6116. goto out_done;
  6117. again:
  6118. ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
  6119. &ordered_offset,
  6120. ordered_bytes, !err);
  6121. if (!ret)
  6122. goto out_test;
  6123. ordered->work.func = finish_ordered_fn;
  6124. ordered->work.flags = 0;
  6125. btrfs_queue_worker(&root->fs_info->endio_write_workers,
  6126. &ordered->work);
  6127. out_test:
  6128. /*
  6129. * our bio might span multiple ordered extents. If we haven't
  6130. * completed the accounting for the whole dio, go back and try again
  6131. */
  6132. if (ordered_offset < dip->logical_offset + dip->bytes) {
  6133. ordered_bytes = dip->logical_offset + dip->bytes -
  6134. ordered_offset;
  6135. ordered = NULL;
  6136. goto again;
  6137. }
  6138. out_done:
  6139. bio->bi_private = dip->private;
  6140. kfree(dip);
  6141. /* If we had an error make sure to clear the uptodate flag */
  6142. if (err)
  6143. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  6144. dio_end_io(bio, err);
  6145. }
  6146. static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
  6147. struct bio *bio, int mirror_num,
  6148. unsigned long bio_flags, u64 offset)
  6149. {
  6150. int ret;
  6151. struct btrfs_root *root = BTRFS_I(inode)->root;
  6152. ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
  6153. BUG_ON(ret); /* -ENOMEM */
  6154. return 0;
  6155. }
  6156. static void btrfs_end_dio_bio(struct bio *bio, int err)
  6157. {
  6158. struct btrfs_dio_private *dip = bio->bi_private;
  6159. if (err) {
  6160. printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
  6161. "sector %#Lx len %u err no %d\n",
  6162. (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
  6163. (unsigned long long)bio->bi_sector, bio->bi_size, err);
  6164. dip->errors = 1;
  6165. /*
  6166. * before atomic variable goto zero, we must make sure
  6167. * dip->errors is perceived to be set.
  6168. */
  6169. smp_mb__before_atomic_dec();
  6170. }
  6171. /* if there are more bios still pending for this dio, just exit */
  6172. if (!atomic_dec_and_test(&dip->pending_bios))
  6173. goto out;
  6174. if (dip->errors)
  6175. bio_io_error(dip->orig_bio);
  6176. else {
  6177. set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
  6178. bio_endio(dip->orig_bio, 0);
  6179. }
  6180. out:
  6181. bio_put(bio);
  6182. }
  6183. static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
  6184. u64 first_sector, gfp_t gfp_flags)
  6185. {
  6186. int nr_vecs = bio_get_nr_vecs(bdev);
  6187. return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
  6188. }
  6189. static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
  6190. int rw, u64 file_offset, int skip_sum,
  6191. int async_submit)
  6192. {
  6193. int write = rw & REQ_WRITE;
  6194. struct btrfs_root *root = BTRFS_I(inode)->root;
  6195. int ret;
  6196. if (async_submit)
  6197. async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
  6198. bio_get(bio);
  6199. if (!write) {
  6200. ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
  6201. if (ret)
  6202. goto err;
  6203. }
  6204. if (skip_sum)
  6205. goto map;
  6206. if (write && async_submit) {
  6207. ret = btrfs_wq_submit_bio(root->fs_info,
  6208. inode, rw, bio, 0, 0,
  6209. file_offset,
  6210. __btrfs_submit_bio_start_direct_io,
  6211. __btrfs_submit_bio_done);
  6212. goto err;
  6213. } else if (write) {
  6214. /*
  6215. * If we aren't doing async submit, calculate the csum of the
  6216. * bio now.
  6217. */
  6218. ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
  6219. if (ret)
  6220. goto err;
  6221. } else if (!skip_sum) {
  6222. ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset);
  6223. if (ret)
  6224. goto err;
  6225. }
  6226. map:
  6227. ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
  6228. err:
  6229. bio_put(bio);
  6230. return ret;
  6231. }
  6232. static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
  6233. int skip_sum)
  6234. {
  6235. struct inode *inode = dip->inode;
  6236. struct btrfs_root *root = BTRFS_I(inode)->root;
  6237. struct bio *bio;
  6238. struct bio *orig_bio = dip->orig_bio;
  6239. struct bio_vec *bvec = orig_bio->bi_io_vec;
  6240. u64 start_sector = orig_bio->bi_sector;
  6241. u64 file_offset = dip->logical_offset;
  6242. u64 submit_len = 0;
  6243. u64 map_length;
  6244. int nr_pages = 0;
  6245. int ret = 0;
  6246. int async_submit = 0;
  6247. map_length = orig_bio->bi_size;
  6248. ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
  6249. &map_length, NULL, 0);
  6250. if (ret) {
  6251. bio_put(orig_bio);
  6252. return -EIO;
  6253. }
  6254. if (map_length >= orig_bio->bi_size) {
  6255. bio = orig_bio;
  6256. goto submit;
  6257. }
  6258. /* async crcs make it difficult to collect full stripe writes. */
  6259. if (btrfs_get_alloc_profile(root, 1) &
  6260. (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
  6261. async_submit = 0;
  6262. else
  6263. async_submit = 1;
  6264. bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
  6265. if (!bio)
  6266. return -ENOMEM;
  6267. bio->bi_private = dip;
  6268. bio->bi_end_io = btrfs_end_dio_bio;
  6269. atomic_inc(&dip->pending_bios);
  6270. while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
  6271. if (unlikely(map_length < submit_len + bvec->bv_len ||
  6272. bio_add_page(bio, bvec->bv_page, bvec->bv_len,
  6273. bvec->bv_offset) < bvec->bv_len)) {
  6274. /*
  6275. * inc the count before we submit the bio so
  6276. * we know the end IO handler won't happen before
  6277. * we inc the count. Otherwise, the dip might get freed
  6278. * before we're done setting it up
  6279. */
  6280. atomic_inc(&dip->pending_bios);
  6281. ret = __btrfs_submit_dio_bio(bio, inode, rw,
  6282. file_offset, skip_sum,
  6283. async_submit);
  6284. if (ret) {
  6285. bio_put(bio);
  6286. atomic_dec(&dip->pending_bios);
  6287. goto out_err;
  6288. }
  6289. start_sector += submit_len >> 9;
  6290. file_offset += submit_len;
  6291. submit_len = 0;
  6292. nr_pages = 0;
  6293. bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
  6294. start_sector, GFP_NOFS);
  6295. if (!bio)
  6296. goto out_err;
  6297. bio->bi_private = dip;
  6298. bio->bi_end_io = btrfs_end_dio_bio;
  6299. map_length = orig_bio->bi_size;
  6300. ret = btrfs_map_block(root->fs_info, rw,
  6301. start_sector << 9,
  6302. &map_length, NULL, 0);
  6303. if (ret) {
  6304. bio_put(bio);
  6305. goto out_err;
  6306. }
  6307. } else {
  6308. submit_len += bvec->bv_len;
  6309. nr_pages ++;
  6310. bvec++;
  6311. }
  6312. }
  6313. submit:
  6314. ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
  6315. async_submit);
  6316. if (!ret)
  6317. return 0;
  6318. bio_put(bio);
  6319. out_err:
  6320. dip->errors = 1;
  6321. /*
  6322. * before atomic variable goto zero, we must
  6323. * make sure dip->errors is perceived to be set.
  6324. */
  6325. smp_mb__before_atomic_dec();
  6326. if (atomic_dec_and_test(&dip->pending_bios))
  6327. bio_io_error(dip->orig_bio);
  6328. /* bio_end_io() will handle error, so we needn't return it */
  6329. return 0;
  6330. }
  6331. static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
  6332. loff_t file_offset)
  6333. {
  6334. struct btrfs_root *root = BTRFS_I(inode)->root;
  6335. struct btrfs_dio_private *dip;
  6336. struct bio_vec *bvec = bio->bi_io_vec;
  6337. int skip_sum;
  6338. int write = rw & REQ_WRITE;
  6339. int ret = 0;
  6340. skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  6341. dip = kmalloc(sizeof(*dip), GFP_NOFS);
  6342. if (!dip) {
  6343. ret = -ENOMEM;
  6344. goto free_ordered;
  6345. }
  6346. dip->private = bio->bi_private;
  6347. dip->inode = inode;
  6348. dip->logical_offset = file_offset;
  6349. dip->bytes = 0;
  6350. do {
  6351. dip->bytes += bvec->bv_len;
  6352. bvec++;
  6353. } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
  6354. dip->disk_bytenr = (u64)bio->bi_sector << 9;
  6355. bio->bi_private = dip;
  6356. dip->errors = 0;
  6357. dip->orig_bio = bio;
  6358. atomic_set(&dip->pending_bios, 0);
  6359. if (write)
  6360. bio->bi_end_io = btrfs_endio_direct_write;
  6361. else
  6362. bio->bi_end_io = btrfs_endio_direct_read;
  6363. ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
  6364. if (!ret)
  6365. return;
  6366. free_ordered:
  6367. /*
  6368. * If this is a write, we need to clean up the reserved space and kill
  6369. * the ordered extent.
  6370. */
  6371. if (write) {
  6372. struct btrfs_ordered_extent *ordered;
  6373. ordered = btrfs_lookup_ordered_extent(inode, file_offset);
  6374. if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
  6375. !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
  6376. btrfs_free_reserved_extent(root, ordered->start,
  6377. ordered->disk_len);
  6378. btrfs_put_ordered_extent(ordered);
  6379. btrfs_put_ordered_extent(ordered);
  6380. }
  6381. bio_endio(bio, ret);
  6382. }
  6383. static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
  6384. const struct iovec *iov, loff_t offset,
  6385. unsigned long nr_segs)
  6386. {
  6387. int seg;
  6388. int i;
  6389. size_t size;
  6390. unsigned long addr;
  6391. unsigned blocksize_mask = root->sectorsize - 1;
  6392. ssize_t retval = -EINVAL;
  6393. loff_t end = offset;
  6394. if (offset & blocksize_mask)
  6395. goto out;
  6396. /* Check the memory alignment. Blocks cannot straddle pages */
  6397. for (seg = 0; seg < nr_segs; seg++) {
  6398. addr = (unsigned long)iov[seg].iov_base;
  6399. size = iov[seg].iov_len;
  6400. end += size;
  6401. if ((addr & blocksize_mask) || (size & blocksize_mask))
  6402. goto out;
  6403. /* If this is a write we don't need to check anymore */
  6404. if (rw & WRITE)
  6405. continue;
  6406. /*
  6407. * Check to make sure we don't have duplicate iov_base's in this
  6408. * iovec, if so return EINVAL, otherwise we'll get csum errors
  6409. * when reading back.
  6410. */
  6411. for (i = seg + 1; i < nr_segs; i++) {
  6412. if (iov[seg].iov_base == iov[i].iov_base)
  6413. goto out;
  6414. }
  6415. }
  6416. retval = 0;
  6417. out:
  6418. return retval;
  6419. }
  6420. static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
  6421. const struct iovec *iov, loff_t offset,
  6422. unsigned long nr_segs)
  6423. {
  6424. struct file *file = iocb->ki_filp;
  6425. struct inode *inode = file->f_mapping->host;
  6426. size_t count = 0;
  6427. int flags = 0;
  6428. bool wakeup = true;
  6429. bool relock = false;
  6430. ssize_t ret;
  6431. if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
  6432. offset, nr_segs))
  6433. return 0;
  6434. atomic_inc(&inode->i_dio_count);
  6435. smp_mb__after_atomic_inc();
  6436. if (rw & WRITE) {
  6437. count = iov_length(iov, nr_segs);
  6438. /*
  6439. * If the write DIO is beyond the EOF, we need update
  6440. * the isize, but it is protected by i_mutex. So we can
  6441. * not unlock the i_mutex at this case.
  6442. */
  6443. if (offset + count <= inode->i_size) {
  6444. mutex_unlock(&inode->i_mutex);
  6445. relock = true;
  6446. }
  6447. ret = btrfs_delalloc_reserve_space(inode, count);
  6448. if (ret)
  6449. goto out;
  6450. } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
  6451. &BTRFS_I(inode)->runtime_flags))) {
  6452. inode_dio_done(inode);
  6453. flags = DIO_LOCKING | DIO_SKIP_HOLES;
  6454. wakeup = false;
  6455. }
  6456. ret = __blockdev_direct_IO(rw, iocb, inode,
  6457. BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
  6458. iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
  6459. btrfs_submit_direct, flags);
  6460. if (rw & WRITE) {
  6461. if (ret < 0 && ret != -EIOCBQUEUED)
  6462. btrfs_delalloc_release_space(inode, count);
  6463. else if (ret >= 0 && (size_t)ret < count)
  6464. btrfs_delalloc_release_space(inode,
  6465. count - (size_t)ret);
  6466. else
  6467. btrfs_delalloc_release_metadata(inode, 0);
  6468. }
  6469. out:
  6470. if (wakeup)
  6471. inode_dio_done(inode);
  6472. if (relock)
  6473. mutex_lock(&inode->i_mutex);
  6474. return ret;
  6475. }
  6476. #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
  6477. static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  6478. __u64 start, __u64 len)
  6479. {
  6480. int ret;
  6481. ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
  6482. if (ret)
  6483. return ret;
  6484. return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
  6485. }
  6486. int btrfs_readpage(struct file *file, struct page *page)
  6487. {
  6488. struct extent_io_tree *tree;
  6489. tree = &BTRFS_I(page->mapping->host)->io_tree;
  6490. return extent_read_full_page(tree, page, btrfs_get_extent, 0);
  6491. }
  6492. static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
  6493. {
  6494. struct extent_io_tree *tree;
  6495. if (current->flags & PF_MEMALLOC) {
  6496. redirty_page_for_writepage(wbc, page);
  6497. unlock_page(page);
  6498. return 0;
  6499. }
  6500. tree = &BTRFS_I(page->mapping->host)->io_tree;
  6501. return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
  6502. }
  6503. int btrfs_writepages(struct address_space *mapping,
  6504. struct writeback_control *wbc)
  6505. {
  6506. struct extent_io_tree *tree;
  6507. tree = &BTRFS_I(mapping->host)->io_tree;
  6508. return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
  6509. }
  6510. static int
  6511. btrfs_readpages(struct file *file, struct address_space *mapping,
  6512. struct list_head *pages, unsigned nr_pages)
  6513. {
  6514. struct extent_io_tree *tree;
  6515. tree = &BTRFS_I(mapping->host)->io_tree;
  6516. return extent_readpages(tree, mapping, pages, nr_pages,
  6517. btrfs_get_extent);
  6518. }
  6519. static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  6520. {
  6521. struct extent_io_tree *tree;
  6522. struct extent_map_tree *map;
  6523. int ret;
  6524. tree = &BTRFS_I(page->mapping->host)->io_tree;
  6525. map = &BTRFS_I(page->mapping->host)->extent_tree;
  6526. ret = try_release_extent_mapping(map, tree, page, gfp_flags);
  6527. if (ret == 1) {
  6528. ClearPagePrivate(page);
  6529. set_page_private(page, 0);
  6530. page_cache_release(page);
  6531. }
  6532. return ret;
  6533. }
  6534. static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  6535. {
  6536. if (PageWriteback(page) || PageDirty(page))
  6537. return 0;
  6538. return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
  6539. }
  6540. static void btrfs_invalidatepage(struct page *page, unsigned long offset)
  6541. {
  6542. struct inode *inode = page->mapping->host;
  6543. struct extent_io_tree *tree;
  6544. struct btrfs_ordered_extent *ordered;
  6545. struct extent_state *cached_state = NULL;
  6546. u64 page_start = page_offset(page);
  6547. u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
  6548. /*
  6549. * we have the page locked, so new writeback can't start,
  6550. * and the dirty bit won't be cleared while we are here.
  6551. *
  6552. * Wait for IO on this page so that we can safely clear
  6553. * the PagePrivate2 bit and do ordered accounting
  6554. */
  6555. wait_on_page_writeback(page);
  6556. tree = &BTRFS_I(inode)->io_tree;
  6557. if (offset) {
  6558. btrfs_releasepage(page, GFP_NOFS);
  6559. return;
  6560. }
  6561. lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
  6562. ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
  6563. if (ordered) {
  6564. /*
  6565. * IO on this page will never be started, so we need
  6566. * to account for any ordered extents now
  6567. */
  6568. clear_extent_bit(tree, page_start, page_end,
  6569. EXTENT_DIRTY | EXTENT_DELALLOC |
  6570. EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
  6571. EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
  6572. /*
  6573. * whoever cleared the private bit is responsible
  6574. * for the finish_ordered_io
  6575. */
  6576. if (TestClearPagePrivate2(page) &&
  6577. btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
  6578. PAGE_CACHE_SIZE, 1)) {
  6579. btrfs_finish_ordered_io(ordered);
  6580. }
  6581. btrfs_put_ordered_extent(ordered);
  6582. cached_state = NULL;
  6583. lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
  6584. }
  6585. clear_extent_bit(tree, page_start, page_end,
  6586. EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
  6587. EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
  6588. &cached_state, GFP_NOFS);
  6589. __btrfs_releasepage(page, GFP_NOFS);
  6590. ClearPageChecked(page);
  6591. if (PagePrivate(page)) {
  6592. ClearPagePrivate(page);
  6593. set_page_private(page, 0);
  6594. page_cache_release(page);
  6595. }
  6596. }
  6597. /*
  6598. * btrfs_page_mkwrite() is not allowed to change the file size as it gets
  6599. * called from a page fault handler when a page is first dirtied. Hence we must
  6600. * be careful to check for EOF conditions here. We set the page up correctly
  6601. * for a written page which means we get ENOSPC checking when writing into
  6602. * holes and correct delalloc and unwritten extent mapping on filesystems that
  6603. * support these features.
  6604. *
  6605. * We are not allowed to take the i_mutex here so we have to play games to
  6606. * protect against truncate races as the page could now be beyond EOF. Because
  6607. * vmtruncate() writes the inode size before removing pages, once we have the
  6608. * page lock we can determine safely if the page is beyond EOF. If it is not
  6609. * beyond EOF, then the page is guaranteed safe against truncation until we
  6610. * unlock the page.
  6611. */
  6612. int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  6613. {
  6614. struct page *page = vmf->page;
  6615. struct inode *inode = file_inode(vma->vm_file);
  6616. struct btrfs_root *root = BTRFS_I(inode)->root;
  6617. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  6618. struct btrfs_ordered_extent *ordered;
  6619. struct extent_state *cached_state = NULL;
  6620. char *kaddr;
  6621. unsigned long zero_start;
  6622. loff_t size;
  6623. int ret;
  6624. int reserved = 0;
  6625. u64 page_start;
  6626. u64 page_end;
  6627. sb_start_pagefault(inode->i_sb);
  6628. ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
  6629. if (!ret) {
  6630. ret = file_update_time(vma->vm_file);
  6631. reserved = 1;
  6632. }
  6633. if (ret) {
  6634. if (ret == -ENOMEM)
  6635. ret = VM_FAULT_OOM;
  6636. else /* -ENOSPC, -EIO, etc */
  6637. ret = VM_FAULT_SIGBUS;
  6638. if (reserved)
  6639. goto out;
  6640. goto out_noreserve;
  6641. }
  6642. ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
  6643. again:
  6644. lock_page(page);
  6645. size = i_size_read(inode);
  6646. page_start = page_offset(page);
  6647. page_end = page_start + PAGE_CACHE_SIZE - 1;
  6648. if ((page->mapping != inode->i_mapping) ||
  6649. (page_start >= size)) {
  6650. /* page got truncated out from underneath us */
  6651. goto out_unlock;
  6652. }
  6653. wait_on_page_writeback(page);
  6654. lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
  6655. set_page_extent_mapped(page);
  6656. /*
  6657. * we can't set the delalloc bits if there are pending ordered
  6658. * extents. Drop our locks and wait for them to finish
  6659. */
  6660. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  6661. if (ordered) {
  6662. unlock_extent_cached(io_tree, page_start, page_end,
  6663. &cached_state, GFP_NOFS);
  6664. unlock_page(page);
  6665. btrfs_start_ordered_extent(inode, ordered, 1);
  6666. btrfs_put_ordered_extent(ordered);
  6667. goto again;
  6668. }
  6669. /*
  6670. * XXX - page_mkwrite gets called every time the page is dirtied, even
  6671. * if it was already dirty, so for space accounting reasons we need to
  6672. * clear any delalloc bits for the range we are fixing to save. There
  6673. * is probably a better way to do this, but for now keep consistent with
  6674. * prepare_pages in the normal write path.
  6675. */
  6676. clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
  6677. EXTENT_DIRTY | EXTENT_DELALLOC |
  6678. EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
  6679. 0, 0, &cached_state, GFP_NOFS);
  6680. ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
  6681. &cached_state);
  6682. if (ret) {
  6683. unlock_extent_cached(io_tree, page_start, page_end,
  6684. &cached_state, GFP_NOFS);
  6685. ret = VM_FAULT_SIGBUS;
  6686. goto out_unlock;
  6687. }
  6688. ret = 0;
  6689. /* page is wholly or partially inside EOF */
  6690. if (page_start + PAGE_CACHE_SIZE > size)
  6691. zero_start = size & ~PAGE_CACHE_MASK;
  6692. else
  6693. zero_start = PAGE_CACHE_SIZE;
  6694. if (zero_start != PAGE_CACHE_SIZE) {
  6695. kaddr = kmap(page);
  6696. memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
  6697. flush_dcache_page(page);
  6698. kunmap(page);
  6699. }
  6700. ClearPageChecked(page);
  6701. set_page_dirty(page);
  6702. SetPageUptodate(page);
  6703. BTRFS_I(inode)->last_trans = root->fs_info->generation;
  6704. BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
  6705. BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
  6706. unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
  6707. out_unlock:
  6708. if (!ret) {
  6709. sb_end_pagefault(inode->i_sb);
  6710. return VM_FAULT_LOCKED;
  6711. }
  6712. unlock_page(page);
  6713. out:
  6714. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  6715. out_noreserve:
  6716. sb_end_pagefault(inode->i_sb);
  6717. return ret;
  6718. }
  6719. static int btrfs_truncate(struct inode *inode)
  6720. {
  6721. struct btrfs_root *root = BTRFS_I(inode)->root;
  6722. struct btrfs_block_rsv *rsv;
  6723. int ret;
  6724. int err = 0;
  6725. struct btrfs_trans_handle *trans;
  6726. u64 mask = root->sectorsize - 1;
  6727. u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
  6728. ret = btrfs_truncate_page(inode, inode->i_size, 0, 0);
  6729. if (ret)
  6730. return ret;
  6731. btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
  6732. btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
  6733. /*
  6734. * Yes ladies and gentelment, this is indeed ugly. The fact is we have
  6735. * 3 things going on here
  6736. *
  6737. * 1) We need to reserve space for our orphan item and the space to
  6738. * delete our orphan item. Lord knows we don't want to have a dangling
  6739. * orphan item because we didn't reserve space to remove it.
  6740. *
  6741. * 2) We need to reserve space to update our inode.
  6742. *
  6743. * 3) We need to have something to cache all the space that is going to
  6744. * be free'd up by the truncate operation, but also have some slack
  6745. * space reserved in case it uses space during the truncate (thank you
  6746. * very much snapshotting).
  6747. *
  6748. * And we need these to all be seperate. The fact is we can use alot of
  6749. * space doing the truncate, and we have no earthly idea how much space
  6750. * we will use, so we need the truncate reservation to be seperate so it
  6751. * doesn't end up using space reserved for updating the inode or
  6752. * removing the orphan item. We also need to be able to stop the
  6753. * transaction and start a new one, which means we need to be able to
  6754. * update the inode several times, and we have no idea of knowing how
  6755. * many times that will be, so we can't just reserve 1 item for the
  6756. * entirety of the opration, so that has to be done seperately as well.
  6757. * Then there is the orphan item, which does indeed need to be held on
  6758. * to for the whole operation, and we need nobody to touch this reserved
  6759. * space except the orphan code.
  6760. *
  6761. * So that leaves us with
  6762. *
  6763. * 1) root->orphan_block_rsv - for the orphan deletion.
  6764. * 2) rsv - for the truncate reservation, which we will steal from the
  6765. * transaction reservation.
  6766. * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
  6767. * updating the inode.
  6768. */
  6769. rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
  6770. if (!rsv)
  6771. return -ENOMEM;
  6772. rsv->size = min_size;
  6773. rsv->failfast = 1;
  6774. /*
  6775. * 1 for the truncate slack space
  6776. * 1 for updating the inode.
  6777. */
  6778. trans = btrfs_start_transaction(root, 2);
  6779. if (IS_ERR(trans)) {
  6780. err = PTR_ERR(trans);
  6781. goto out;
  6782. }
  6783. /* Migrate the slack space for the truncate to our reserve */
  6784. ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
  6785. min_size);
  6786. BUG_ON(ret);
  6787. /*
  6788. * setattr is responsible for setting the ordered_data_close flag,
  6789. * but that is only tested during the last file release. That
  6790. * could happen well after the next commit, leaving a great big
  6791. * window where new writes may get lost if someone chooses to write
  6792. * to this file after truncating to zero
  6793. *
  6794. * The inode doesn't have any dirty data here, and so if we commit
  6795. * this is a noop. If someone immediately starts writing to the inode
  6796. * it is very likely we'll catch some of their writes in this
  6797. * transaction, and the commit will find this file on the ordered
  6798. * data list with good things to send down.
  6799. *
  6800. * This is a best effort solution, there is still a window where
  6801. * using truncate to replace the contents of the file will
  6802. * end up with a zero length file after a crash.
  6803. */
  6804. if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
  6805. &BTRFS_I(inode)->runtime_flags))
  6806. btrfs_add_ordered_operation(trans, root, inode);
  6807. /*
  6808. * So if we truncate and then write and fsync we normally would just
  6809. * write the extents that changed, which is a problem if we need to
  6810. * first truncate that entire inode. So set this flag so we write out
  6811. * all of the extents in the inode to the sync log so we're completely
  6812. * safe.
  6813. */
  6814. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
  6815. trans->block_rsv = rsv;
  6816. while (1) {
  6817. ret = btrfs_truncate_inode_items(trans, root, inode,
  6818. inode->i_size,
  6819. BTRFS_EXTENT_DATA_KEY);
  6820. if (ret != -ENOSPC) {
  6821. err = ret;
  6822. break;
  6823. }
  6824. trans->block_rsv = &root->fs_info->trans_block_rsv;
  6825. ret = btrfs_update_inode(trans, root, inode);
  6826. if (ret) {
  6827. err = ret;
  6828. break;
  6829. }
  6830. btrfs_end_transaction(trans, root);
  6831. btrfs_btree_balance_dirty(root);
  6832. trans = btrfs_start_transaction(root, 2);
  6833. if (IS_ERR(trans)) {
  6834. ret = err = PTR_ERR(trans);
  6835. trans = NULL;
  6836. break;
  6837. }
  6838. ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
  6839. rsv, min_size);
  6840. BUG_ON(ret); /* shouldn't happen */
  6841. trans->block_rsv = rsv;
  6842. }
  6843. if (ret == 0 && inode->i_nlink > 0) {
  6844. trans->block_rsv = root->orphan_block_rsv;
  6845. ret = btrfs_orphan_del(trans, inode);
  6846. if (ret)
  6847. err = ret;
  6848. }
  6849. if (trans) {
  6850. trans->block_rsv = &root->fs_info->trans_block_rsv;
  6851. ret = btrfs_update_inode(trans, root, inode);
  6852. if (ret && !err)
  6853. err = ret;
  6854. ret = btrfs_end_transaction(trans, root);
  6855. btrfs_btree_balance_dirty(root);
  6856. }
  6857. out:
  6858. btrfs_free_block_rsv(root, rsv);
  6859. if (ret && !err)
  6860. err = ret;
  6861. return err;
  6862. }
  6863. /*
  6864. * create a new subvolume directory/inode (helper for the ioctl).
  6865. */
  6866. int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
  6867. struct btrfs_root *new_root, u64 new_dirid)
  6868. {
  6869. struct inode *inode;
  6870. int err;
  6871. u64 index = 0;
  6872. inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
  6873. new_dirid, new_dirid,
  6874. S_IFDIR | (~current_umask() & S_IRWXUGO),
  6875. &index);
  6876. if (IS_ERR(inode))
  6877. return PTR_ERR(inode);
  6878. inode->i_op = &btrfs_dir_inode_operations;
  6879. inode->i_fop = &btrfs_dir_file_operations;
  6880. set_nlink(inode, 1);
  6881. btrfs_i_size_write(inode, 0);
  6882. err = btrfs_update_inode(trans, new_root, inode);
  6883. iput(inode);
  6884. return err;
  6885. }
  6886. struct inode *btrfs_alloc_inode(struct super_block *sb)
  6887. {
  6888. struct btrfs_inode *ei;
  6889. struct inode *inode;
  6890. ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
  6891. if (!ei)
  6892. return NULL;
  6893. ei->root = NULL;
  6894. ei->generation = 0;
  6895. ei->last_trans = 0;
  6896. ei->last_sub_trans = 0;
  6897. ei->logged_trans = 0;
  6898. ei->delalloc_bytes = 0;
  6899. ei->disk_i_size = 0;
  6900. ei->flags = 0;
  6901. ei->csum_bytes = 0;
  6902. ei->index_cnt = (u64)-1;
  6903. ei->last_unlink_trans = 0;
  6904. ei->last_log_commit = 0;
  6905. spin_lock_init(&ei->lock);
  6906. ei->outstanding_extents = 0;
  6907. ei->reserved_extents = 0;
  6908. ei->runtime_flags = 0;
  6909. ei->force_compress = BTRFS_COMPRESS_NONE;
  6910. ei->delayed_node = NULL;
  6911. inode = &ei->vfs_inode;
  6912. extent_map_tree_init(&ei->extent_tree);
  6913. extent_io_tree_init(&ei->io_tree, &inode->i_data);
  6914. extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
  6915. ei->io_tree.track_uptodate = 1;
  6916. ei->io_failure_tree.track_uptodate = 1;
  6917. atomic_set(&ei->sync_writers, 0);
  6918. mutex_init(&ei->log_mutex);
  6919. mutex_init(&ei->delalloc_mutex);
  6920. btrfs_ordered_inode_tree_init(&ei->ordered_tree);
  6921. INIT_LIST_HEAD(&ei->delalloc_inodes);
  6922. INIT_LIST_HEAD(&ei->ordered_operations);
  6923. RB_CLEAR_NODE(&ei->rb_node);
  6924. return inode;
  6925. }
  6926. static void btrfs_i_callback(struct rcu_head *head)
  6927. {
  6928. struct inode *inode = container_of(head, struct inode, i_rcu);
  6929. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  6930. }
  6931. void btrfs_destroy_inode(struct inode *inode)
  6932. {
  6933. struct btrfs_ordered_extent *ordered;
  6934. struct btrfs_root *root = BTRFS_I(inode)->root;
  6935. WARN_ON(!hlist_empty(&inode->i_dentry));
  6936. WARN_ON(inode->i_data.nrpages);
  6937. WARN_ON(BTRFS_I(inode)->outstanding_extents);
  6938. WARN_ON(BTRFS_I(inode)->reserved_extents);
  6939. WARN_ON(BTRFS_I(inode)->delalloc_bytes);
  6940. WARN_ON(BTRFS_I(inode)->csum_bytes);
  6941. /*
  6942. * This can happen where we create an inode, but somebody else also
  6943. * created the same inode and we need to destroy the one we already
  6944. * created.
  6945. */
  6946. if (!root)
  6947. goto free;
  6948. /*
  6949. * Make sure we're properly removed from the ordered operation
  6950. * lists.
  6951. */
  6952. smp_mb();
  6953. if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
  6954. spin_lock(&root->fs_info->ordered_extent_lock);
  6955. list_del_init(&BTRFS_I(inode)->ordered_operations);
  6956. spin_unlock(&root->fs_info->ordered_extent_lock);
  6957. }
  6958. if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
  6959. &BTRFS_I(inode)->runtime_flags)) {
  6960. printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
  6961. (unsigned long long)btrfs_ino(inode));
  6962. atomic_dec(&root->orphan_inodes);
  6963. }
  6964. while (1) {
  6965. ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
  6966. if (!ordered)
  6967. break;
  6968. else {
  6969. printk(KERN_ERR "btrfs found ordered "
  6970. "extent %llu %llu on inode cleanup\n",
  6971. (unsigned long long)ordered->file_offset,
  6972. (unsigned long long)ordered->len);
  6973. btrfs_remove_ordered_extent(inode, ordered);
  6974. btrfs_put_ordered_extent(ordered);
  6975. btrfs_put_ordered_extent(ordered);
  6976. }
  6977. }
  6978. inode_tree_del(inode);
  6979. btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
  6980. free:
  6981. btrfs_remove_delayed_node(inode);
  6982. call_rcu(&inode->i_rcu, btrfs_i_callback);
  6983. }
  6984. int btrfs_drop_inode(struct inode *inode)
  6985. {
  6986. struct btrfs_root *root = BTRFS_I(inode)->root;
  6987. /* the snap/subvol tree is on deleting */
  6988. if (btrfs_root_refs(&root->root_item) == 0 &&
  6989. root != root->fs_info->tree_root)
  6990. return 1;
  6991. else
  6992. return generic_drop_inode(inode);
  6993. }
  6994. static void init_once(void *foo)
  6995. {
  6996. struct btrfs_inode *ei = (struct btrfs_inode *) foo;
  6997. inode_init_once(&ei->vfs_inode);
  6998. }
  6999. void btrfs_destroy_cachep(void)
  7000. {
  7001. /*
  7002. * Make sure all delayed rcu free inodes are flushed before we
  7003. * destroy cache.
  7004. */
  7005. rcu_barrier();
  7006. if (btrfs_inode_cachep)
  7007. kmem_cache_destroy(btrfs_inode_cachep);
  7008. if (btrfs_trans_handle_cachep)
  7009. kmem_cache_destroy(btrfs_trans_handle_cachep);
  7010. if (btrfs_transaction_cachep)
  7011. kmem_cache_destroy(btrfs_transaction_cachep);
  7012. if (btrfs_path_cachep)
  7013. kmem_cache_destroy(btrfs_path_cachep);
  7014. if (btrfs_free_space_cachep)
  7015. kmem_cache_destroy(btrfs_free_space_cachep);
  7016. if (btrfs_delalloc_work_cachep)
  7017. kmem_cache_destroy(btrfs_delalloc_work_cachep);
  7018. }
  7019. int btrfs_init_cachep(void)
  7020. {
  7021. btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
  7022. sizeof(struct btrfs_inode), 0,
  7023. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
  7024. if (!btrfs_inode_cachep)
  7025. goto fail;
  7026. btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
  7027. sizeof(struct btrfs_trans_handle), 0,
  7028. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  7029. if (!btrfs_trans_handle_cachep)
  7030. goto fail;
  7031. btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
  7032. sizeof(struct btrfs_transaction), 0,
  7033. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  7034. if (!btrfs_transaction_cachep)
  7035. goto fail;
  7036. btrfs_path_cachep = kmem_cache_create("btrfs_path",
  7037. sizeof(struct btrfs_path), 0,
  7038. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  7039. if (!btrfs_path_cachep)
  7040. goto fail;
  7041. btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
  7042. sizeof(struct btrfs_free_space), 0,
  7043. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  7044. if (!btrfs_free_space_cachep)
  7045. goto fail;
  7046. btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
  7047. sizeof(struct btrfs_delalloc_work), 0,
  7048. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
  7049. NULL);
  7050. if (!btrfs_delalloc_work_cachep)
  7051. goto fail;
  7052. return 0;
  7053. fail:
  7054. btrfs_destroy_cachep();
  7055. return -ENOMEM;
  7056. }
  7057. static int btrfs_getattr(struct vfsmount *mnt,
  7058. struct dentry *dentry, struct kstat *stat)
  7059. {
  7060. u64 delalloc_bytes;
  7061. struct inode *inode = dentry->d_inode;
  7062. u32 blocksize = inode->i_sb->s_blocksize;
  7063. generic_fillattr(inode, stat);
  7064. stat->dev = BTRFS_I(inode)->root->anon_dev;
  7065. stat->blksize = PAGE_CACHE_SIZE;
  7066. spin_lock(&BTRFS_I(inode)->lock);
  7067. delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
  7068. spin_unlock(&BTRFS_I(inode)->lock);
  7069. stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
  7070. ALIGN(delalloc_bytes, blocksize)) >> 9;
  7071. return 0;
  7072. }
  7073. static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  7074. struct inode *new_dir, struct dentry *new_dentry)
  7075. {
  7076. struct btrfs_trans_handle *trans;
  7077. struct btrfs_root *root = BTRFS_I(old_dir)->root;
  7078. struct btrfs_root *dest = BTRFS_I(new_dir)->root;
  7079. struct inode *new_inode = new_dentry->d_inode;
  7080. struct inode *old_inode = old_dentry->d_inode;
  7081. struct timespec ctime = CURRENT_TIME;
  7082. u64 index = 0;
  7083. u64 root_objectid;
  7084. int ret;
  7085. u64 old_ino = btrfs_ino(old_inode);
  7086. if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
  7087. return -EPERM;
  7088. /* we only allow rename subvolume link between subvolumes */
  7089. if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
  7090. return -EXDEV;
  7091. if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
  7092. (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
  7093. return -ENOTEMPTY;
  7094. if (S_ISDIR(old_inode->i_mode) && new_inode &&
  7095. new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
  7096. return -ENOTEMPTY;
  7097. /* check for collisions, even if the name isn't there */
  7098. ret = btrfs_check_dir_item_collision(root, new_dir->i_ino,
  7099. new_dentry->d_name.name,
  7100. new_dentry->d_name.len);
  7101. if (ret) {
  7102. if (ret == -EEXIST) {
  7103. /* we shouldn't get
  7104. * eexist without a new_inode */
  7105. if (!new_inode) {
  7106. WARN_ON(1);
  7107. return ret;
  7108. }
  7109. } else {
  7110. /* maybe -EOVERFLOW */
  7111. return ret;
  7112. }
  7113. }
  7114. ret = 0;
  7115. /*
  7116. * we're using rename to replace one file with another.
  7117. * and the replacement file is large. Start IO on it now so
  7118. * we don't add too much work to the end of the transaction
  7119. */
  7120. if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
  7121. old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
  7122. filemap_flush(old_inode->i_mapping);
  7123. /* close the racy window with snapshot create/destroy ioctl */
  7124. if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
  7125. down_read(&root->fs_info->subvol_sem);
  7126. /*
  7127. * We want to reserve the absolute worst case amount of items. So if
  7128. * both inodes are subvols and we need to unlink them then that would
  7129. * require 4 item modifications, but if they are both normal inodes it
  7130. * would require 5 item modifications, so we'll assume their normal
  7131. * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
  7132. * should cover the worst case number of items we'll modify.
  7133. */
  7134. trans = btrfs_start_transaction(root, 20);
  7135. if (IS_ERR(trans)) {
  7136. ret = PTR_ERR(trans);
  7137. goto out_notrans;
  7138. }
  7139. if (dest != root)
  7140. btrfs_record_root_in_trans(trans, dest);
  7141. ret = btrfs_set_inode_index(new_dir, &index);
  7142. if (ret)
  7143. goto out_fail;
  7144. if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  7145. /* force full log commit if subvolume involved. */
  7146. root->fs_info->last_trans_log_full_commit = trans->transid;
  7147. } else {
  7148. ret = btrfs_insert_inode_ref(trans, dest,
  7149. new_dentry->d_name.name,
  7150. new_dentry->d_name.len,
  7151. old_ino,
  7152. btrfs_ino(new_dir), index);
  7153. if (ret)
  7154. goto out_fail;
  7155. /*
  7156. * this is an ugly little race, but the rename is required
  7157. * to make sure that if we crash, the inode is either at the
  7158. * old name or the new one. pinning the log transaction lets
  7159. * us make sure we don't allow a log commit to come in after
  7160. * we unlink the name but before we add the new name back in.
  7161. */
  7162. btrfs_pin_log_trans(root);
  7163. }
  7164. /*
  7165. * make sure the inode gets flushed if it is replacing
  7166. * something.
  7167. */
  7168. if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
  7169. btrfs_add_ordered_operation(trans, root, old_inode);
  7170. inode_inc_iversion(old_dir);
  7171. inode_inc_iversion(new_dir);
  7172. inode_inc_iversion(old_inode);
  7173. old_dir->i_ctime = old_dir->i_mtime = ctime;
  7174. new_dir->i_ctime = new_dir->i_mtime = ctime;
  7175. old_inode->i_ctime = ctime;
  7176. if (old_dentry->d_parent != new_dentry->d_parent)
  7177. btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
  7178. if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  7179. root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
  7180. ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
  7181. old_dentry->d_name.name,
  7182. old_dentry->d_name.len);
  7183. } else {
  7184. ret = __btrfs_unlink_inode(trans, root, old_dir,
  7185. old_dentry->d_inode,
  7186. old_dentry->d_name.name,
  7187. old_dentry->d_name.len);
  7188. if (!ret)
  7189. ret = btrfs_update_inode(trans, root, old_inode);
  7190. }
  7191. if (ret) {
  7192. btrfs_abort_transaction(trans, root, ret);
  7193. goto out_fail;
  7194. }
  7195. if (new_inode) {
  7196. inode_inc_iversion(new_inode);
  7197. new_inode->i_ctime = CURRENT_TIME;
  7198. if (unlikely(btrfs_ino(new_inode) ==
  7199. BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
  7200. root_objectid = BTRFS_I(new_inode)->location.objectid;
  7201. ret = btrfs_unlink_subvol(trans, dest, new_dir,
  7202. root_objectid,
  7203. new_dentry->d_name.name,
  7204. new_dentry->d_name.len);
  7205. BUG_ON(new_inode->i_nlink == 0);
  7206. } else {
  7207. ret = btrfs_unlink_inode(trans, dest, new_dir,
  7208. new_dentry->d_inode,
  7209. new_dentry->d_name.name,
  7210. new_dentry->d_name.len);
  7211. }
  7212. if (!ret && new_inode->i_nlink == 0) {
  7213. ret = btrfs_orphan_add(trans, new_dentry->d_inode);
  7214. BUG_ON(ret);
  7215. }
  7216. if (ret) {
  7217. btrfs_abort_transaction(trans, root, ret);
  7218. goto out_fail;
  7219. }
  7220. }
  7221. ret = btrfs_add_link(trans, new_dir, old_inode,
  7222. new_dentry->d_name.name,
  7223. new_dentry->d_name.len, 0, index);
  7224. if (ret) {
  7225. btrfs_abort_transaction(trans, root, ret);
  7226. goto out_fail;
  7227. }
  7228. if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
  7229. struct dentry *parent = new_dentry->d_parent;
  7230. btrfs_log_new_name(trans, old_inode, old_dir, parent);
  7231. btrfs_end_log_trans(root);
  7232. }
  7233. out_fail:
  7234. btrfs_end_transaction(trans, root);
  7235. out_notrans:
  7236. if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
  7237. up_read(&root->fs_info->subvol_sem);
  7238. return ret;
  7239. }
  7240. static void btrfs_run_delalloc_work(struct btrfs_work *work)
  7241. {
  7242. struct btrfs_delalloc_work *delalloc_work;
  7243. delalloc_work = container_of(work, struct btrfs_delalloc_work,
  7244. work);
  7245. if (delalloc_work->wait)
  7246. btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
  7247. else
  7248. filemap_flush(delalloc_work->inode->i_mapping);
  7249. if (delalloc_work->delay_iput)
  7250. btrfs_add_delayed_iput(delalloc_work->inode);
  7251. else
  7252. iput(delalloc_work->inode);
  7253. complete(&delalloc_work->completion);
  7254. }
  7255. struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
  7256. int wait, int delay_iput)
  7257. {
  7258. struct btrfs_delalloc_work *work;
  7259. work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
  7260. if (!work)
  7261. return NULL;
  7262. init_completion(&work->completion);
  7263. INIT_LIST_HEAD(&work->list);
  7264. work->inode = inode;
  7265. work->wait = wait;
  7266. work->delay_iput = delay_iput;
  7267. work->work.func = btrfs_run_delalloc_work;
  7268. return work;
  7269. }
  7270. void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
  7271. {
  7272. wait_for_completion(&work->completion);
  7273. kmem_cache_free(btrfs_delalloc_work_cachep, work);
  7274. }
  7275. /*
  7276. * some fairly slow code that needs optimization. This walks the list
  7277. * of all the inodes with pending delalloc and forces them to disk.
  7278. */
  7279. int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
  7280. {
  7281. struct btrfs_inode *binode;
  7282. struct inode *inode;
  7283. struct btrfs_delalloc_work *work, *next;
  7284. struct list_head works;
  7285. struct list_head splice;
  7286. int ret = 0;
  7287. if (root->fs_info->sb->s_flags & MS_RDONLY)
  7288. return -EROFS;
  7289. INIT_LIST_HEAD(&works);
  7290. INIT_LIST_HEAD(&splice);
  7291. spin_lock(&root->fs_info->delalloc_lock);
  7292. list_splice_init(&root->fs_info->delalloc_inodes, &splice);
  7293. while (!list_empty(&splice)) {
  7294. binode = list_entry(splice.next, struct btrfs_inode,
  7295. delalloc_inodes);
  7296. list_del_init(&binode->delalloc_inodes);
  7297. inode = igrab(&binode->vfs_inode);
  7298. if (!inode) {
  7299. clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
  7300. &binode->runtime_flags);
  7301. continue;
  7302. }
  7303. list_add_tail(&binode->delalloc_inodes,
  7304. &root->fs_info->delalloc_inodes);
  7305. spin_unlock(&root->fs_info->delalloc_lock);
  7306. work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
  7307. if (unlikely(!work)) {
  7308. ret = -ENOMEM;
  7309. goto out;
  7310. }
  7311. list_add_tail(&work->list, &works);
  7312. btrfs_queue_worker(&root->fs_info->flush_workers,
  7313. &work->work);
  7314. cond_resched();
  7315. spin_lock(&root->fs_info->delalloc_lock);
  7316. }
  7317. spin_unlock(&root->fs_info->delalloc_lock);
  7318. list_for_each_entry_safe(work, next, &works, list) {
  7319. list_del_init(&work->list);
  7320. btrfs_wait_and_free_delalloc_work(work);
  7321. }
  7322. /* the filemap_flush will queue IO into the worker threads, but
  7323. * we have to make sure the IO is actually started and that
  7324. * ordered extents get created before we return
  7325. */
  7326. atomic_inc(&root->fs_info->async_submit_draining);
  7327. while (atomic_read(&root->fs_info->nr_async_submits) ||
  7328. atomic_read(&root->fs_info->async_delalloc_pages)) {
  7329. wait_event(root->fs_info->async_submit_wait,
  7330. (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
  7331. atomic_read(&root->fs_info->async_delalloc_pages) == 0));
  7332. }
  7333. atomic_dec(&root->fs_info->async_submit_draining);
  7334. return 0;
  7335. out:
  7336. list_for_each_entry_safe(work, next, &works, list) {
  7337. list_del_init(&work->list);
  7338. btrfs_wait_and_free_delalloc_work(work);
  7339. }
  7340. if (!list_empty_careful(&splice)) {
  7341. spin_lock(&root->fs_info->delalloc_lock);
  7342. list_splice_tail(&splice, &root->fs_info->delalloc_inodes);
  7343. spin_unlock(&root->fs_info->delalloc_lock);
  7344. }
  7345. return ret;
  7346. }
  7347. static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
  7348. const char *symname)
  7349. {
  7350. struct btrfs_trans_handle *trans;
  7351. struct btrfs_root *root = BTRFS_I(dir)->root;
  7352. struct btrfs_path *path;
  7353. struct btrfs_key key;
  7354. struct inode *inode = NULL;
  7355. int err;
  7356. int drop_inode = 0;
  7357. u64 objectid;
  7358. u64 index = 0 ;
  7359. int name_len;
  7360. int datasize;
  7361. unsigned long ptr;
  7362. struct btrfs_file_extent_item *ei;
  7363. struct extent_buffer *leaf;
  7364. name_len = strlen(symname) + 1;
  7365. if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
  7366. return -ENAMETOOLONG;
  7367. /*
  7368. * 2 items for inode item and ref
  7369. * 2 items for dir items
  7370. * 1 item for xattr if selinux is on
  7371. */
  7372. trans = btrfs_start_transaction(root, 5);
  7373. if (IS_ERR(trans))
  7374. return PTR_ERR(trans);
  7375. err = btrfs_find_free_ino(root, &objectid);
  7376. if (err)
  7377. goto out_unlock;
  7378. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  7379. dentry->d_name.len, btrfs_ino(dir), objectid,
  7380. S_IFLNK|S_IRWXUGO, &index);
  7381. if (IS_ERR(inode)) {
  7382. err = PTR_ERR(inode);
  7383. goto out_unlock;
  7384. }
  7385. err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
  7386. if (err) {
  7387. drop_inode = 1;
  7388. goto out_unlock;
  7389. }
  7390. /*
  7391. * If the active LSM wants to access the inode during
  7392. * d_instantiate it needs these. Smack checks to see
  7393. * if the filesystem supports xattrs by looking at the
  7394. * ops vector.
  7395. */
  7396. inode->i_fop = &btrfs_file_operations;
  7397. inode->i_op = &btrfs_file_inode_operations;
  7398. err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
  7399. if (err)
  7400. drop_inode = 1;
  7401. else {
  7402. inode->i_mapping->a_ops = &btrfs_aops;
  7403. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  7404. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  7405. }
  7406. if (drop_inode)
  7407. goto out_unlock;
  7408. path = btrfs_alloc_path();
  7409. if (!path) {
  7410. err = -ENOMEM;
  7411. drop_inode = 1;
  7412. goto out_unlock;
  7413. }
  7414. key.objectid = btrfs_ino(inode);
  7415. key.offset = 0;
  7416. btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
  7417. datasize = btrfs_file_extent_calc_inline_size(name_len);
  7418. err = btrfs_insert_empty_item(trans, root, path, &key,
  7419. datasize);
  7420. if (err) {
  7421. drop_inode = 1;
  7422. btrfs_free_path(path);
  7423. goto out_unlock;
  7424. }
  7425. leaf = path->nodes[0];
  7426. ei = btrfs_item_ptr(leaf, path->slots[0],
  7427. struct btrfs_file_extent_item);
  7428. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  7429. btrfs_set_file_extent_type(leaf, ei,
  7430. BTRFS_FILE_EXTENT_INLINE);
  7431. btrfs_set_file_extent_encryption(leaf, ei, 0);
  7432. btrfs_set_file_extent_compression(leaf, ei, 0);
  7433. btrfs_set_file_extent_other_encoding(leaf, ei, 0);
  7434. btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
  7435. ptr = btrfs_file_extent_inline_start(ei);
  7436. write_extent_buffer(leaf, symname, ptr, name_len);
  7437. btrfs_mark_buffer_dirty(leaf);
  7438. btrfs_free_path(path);
  7439. inode->i_op = &btrfs_symlink_inode_operations;
  7440. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  7441. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  7442. inode_set_bytes(inode, name_len);
  7443. btrfs_i_size_write(inode, name_len - 1);
  7444. err = btrfs_update_inode(trans, root, inode);
  7445. if (err)
  7446. drop_inode = 1;
  7447. out_unlock:
  7448. if (!err)
  7449. d_instantiate(dentry, inode);
  7450. btrfs_end_transaction(trans, root);
  7451. if (drop_inode) {
  7452. inode_dec_link_count(inode);
  7453. iput(inode);
  7454. }
  7455. btrfs_btree_balance_dirty(root);
  7456. return err;
  7457. }
  7458. static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
  7459. u64 start, u64 num_bytes, u64 min_size,
  7460. loff_t actual_len, u64 *alloc_hint,
  7461. struct btrfs_trans_handle *trans)
  7462. {
  7463. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  7464. struct extent_map *em;
  7465. struct btrfs_root *root = BTRFS_I(inode)->root;
  7466. struct btrfs_key ins;
  7467. u64 cur_offset = start;
  7468. u64 i_size;
  7469. u64 cur_bytes;
  7470. int ret = 0;
  7471. bool own_trans = true;
  7472. if (trans)
  7473. own_trans = false;
  7474. while (num_bytes > 0) {
  7475. if (own_trans) {
  7476. trans = btrfs_start_transaction(root, 3);
  7477. if (IS_ERR(trans)) {
  7478. ret = PTR_ERR(trans);
  7479. break;
  7480. }
  7481. }
  7482. cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
  7483. cur_bytes = max(cur_bytes, min_size);
  7484. ret = btrfs_reserve_extent(trans, root, cur_bytes,
  7485. min_size, 0, *alloc_hint, &ins, 1);
  7486. if (ret) {
  7487. if (own_trans)
  7488. btrfs_end_transaction(trans, root);
  7489. break;
  7490. }
  7491. ret = insert_reserved_file_extent(trans, inode,
  7492. cur_offset, ins.objectid,
  7493. ins.offset, ins.offset,
  7494. ins.offset, 0, 0, 0,
  7495. BTRFS_FILE_EXTENT_PREALLOC);
  7496. if (ret) {
  7497. btrfs_abort_transaction(trans, root, ret);
  7498. if (own_trans)
  7499. btrfs_end_transaction(trans, root);
  7500. break;
  7501. }
  7502. btrfs_drop_extent_cache(inode, cur_offset,
  7503. cur_offset + ins.offset -1, 0);
  7504. em = alloc_extent_map();
  7505. if (!em) {
  7506. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  7507. &BTRFS_I(inode)->runtime_flags);
  7508. goto next;
  7509. }
  7510. em->start = cur_offset;
  7511. em->orig_start = cur_offset;
  7512. em->len = ins.offset;
  7513. em->block_start = ins.objectid;
  7514. em->block_len = ins.offset;
  7515. em->orig_block_len = ins.offset;
  7516. em->bdev = root->fs_info->fs_devices->latest_bdev;
  7517. set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
  7518. em->generation = trans->transid;
  7519. while (1) {
  7520. write_lock(&em_tree->lock);
  7521. ret = add_extent_mapping(em_tree, em);
  7522. if (!ret)
  7523. list_move(&em->list,
  7524. &em_tree->modified_extents);
  7525. write_unlock(&em_tree->lock);
  7526. if (ret != -EEXIST)
  7527. break;
  7528. btrfs_drop_extent_cache(inode, cur_offset,
  7529. cur_offset + ins.offset - 1,
  7530. 0);
  7531. }
  7532. free_extent_map(em);
  7533. next:
  7534. num_bytes -= ins.offset;
  7535. cur_offset += ins.offset;
  7536. *alloc_hint = ins.objectid + ins.offset;
  7537. inode_inc_iversion(inode);
  7538. inode->i_ctime = CURRENT_TIME;
  7539. BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
  7540. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  7541. (actual_len > inode->i_size) &&
  7542. (cur_offset > inode->i_size)) {
  7543. if (cur_offset > actual_len)
  7544. i_size = actual_len;
  7545. else
  7546. i_size = cur_offset;
  7547. i_size_write(inode, i_size);
  7548. btrfs_ordered_update_i_size(inode, i_size, NULL);
  7549. }
  7550. ret = btrfs_update_inode(trans, root, inode);
  7551. if (ret) {
  7552. btrfs_abort_transaction(trans, root, ret);
  7553. if (own_trans)
  7554. btrfs_end_transaction(trans, root);
  7555. break;
  7556. }
  7557. if (own_trans)
  7558. btrfs_end_transaction(trans, root);
  7559. }
  7560. return ret;
  7561. }
  7562. int btrfs_prealloc_file_range(struct inode *inode, int mode,
  7563. u64 start, u64 num_bytes, u64 min_size,
  7564. loff_t actual_len, u64 *alloc_hint)
  7565. {
  7566. return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
  7567. min_size, actual_len, alloc_hint,
  7568. NULL);
  7569. }
  7570. int btrfs_prealloc_file_range_trans(struct inode *inode,
  7571. struct btrfs_trans_handle *trans, int mode,
  7572. u64 start, u64 num_bytes, u64 min_size,
  7573. loff_t actual_len, u64 *alloc_hint)
  7574. {
  7575. return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
  7576. min_size, actual_len, alloc_hint, trans);
  7577. }
  7578. static int btrfs_set_page_dirty(struct page *page)
  7579. {
  7580. return __set_page_dirty_nobuffers(page);
  7581. }
  7582. static int btrfs_permission(struct inode *inode, int mask)
  7583. {
  7584. struct btrfs_root *root = BTRFS_I(inode)->root;
  7585. umode_t mode = inode->i_mode;
  7586. if (mask & MAY_WRITE &&
  7587. (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
  7588. if (btrfs_root_readonly(root))
  7589. return -EROFS;
  7590. if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
  7591. return -EACCES;
  7592. }
  7593. return generic_permission(inode, mask);
  7594. }
  7595. static const struct inode_operations btrfs_dir_inode_operations = {
  7596. .getattr = btrfs_getattr,
  7597. .lookup = btrfs_lookup,
  7598. .create = btrfs_create,
  7599. .unlink = btrfs_unlink,
  7600. .link = btrfs_link,
  7601. .mkdir = btrfs_mkdir,
  7602. .rmdir = btrfs_rmdir,
  7603. .rename = btrfs_rename,
  7604. .symlink = btrfs_symlink,
  7605. .setattr = btrfs_setattr,
  7606. .mknod = btrfs_mknod,
  7607. .setxattr = btrfs_setxattr,
  7608. .getxattr = btrfs_getxattr,
  7609. .listxattr = btrfs_listxattr,
  7610. .removexattr = btrfs_removexattr,
  7611. .permission = btrfs_permission,
  7612. .get_acl = btrfs_get_acl,
  7613. };
  7614. static const struct inode_operations btrfs_dir_ro_inode_operations = {
  7615. .lookup = btrfs_lookup,
  7616. .permission = btrfs_permission,
  7617. .get_acl = btrfs_get_acl,
  7618. };
  7619. static const struct file_operations btrfs_dir_file_operations = {
  7620. .llseek = generic_file_llseek,
  7621. .read = generic_read_dir,
  7622. .readdir = btrfs_real_readdir,
  7623. .unlocked_ioctl = btrfs_ioctl,
  7624. #ifdef CONFIG_COMPAT
  7625. .compat_ioctl = btrfs_ioctl,
  7626. #endif
  7627. .release = btrfs_release_file,
  7628. .fsync = btrfs_sync_file,
  7629. };
  7630. static struct extent_io_ops btrfs_extent_io_ops = {
  7631. .fill_delalloc = run_delalloc_range,
  7632. .submit_bio_hook = btrfs_submit_bio_hook,
  7633. .merge_bio_hook = btrfs_merge_bio_hook,
  7634. .readpage_end_io_hook = btrfs_readpage_end_io_hook,
  7635. .writepage_end_io_hook = btrfs_writepage_end_io_hook,
  7636. .writepage_start_hook = btrfs_writepage_start_hook,
  7637. .set_bit_hook = btrfs_set_bit_hook,
  7638. .clear_bit_hook = btrfs_clear_bit_hook,
  7639. .merge_extent_hook = btrfs_merge_extent_hook,
  7640. .split_extent_hook = btrfs_split_extent_hook,
  7641. };
  7642. /*
  7643. * btrfs doesn't support the bmap operation because swapfiles
  7644. * use bmap to make a mapping of extents in the file. They assume
  7645. * these extents won't change over the life of the file and they
  7646. * use the bmap result to do IO directly to the drive.
  7647. *
  7648. * the btrfs bmap call would return logical addresses that aren't
  7649. * suitable for IO and they also will change frequently as COW
  7650. * operations happen. So, swapfile + btrfs == corruption.
  7651. *
  7652. * For now we're avoiding this by dropping bmap.
  7653. */
  7654. static const struct address_space_operations btrfs_aops = {
  7655. .readpage = btrfs_readpage,
  7656. .writepage = btrfs_writepage,
  7657. .writepages = btrfs_writepages,
  7658. .readpages = btrfs_readpages,
  7659. .direct_IO = btrfs_direct_IO,
  7660. .invalidatepage = btrfs_invalidatepage,
  7661. .releasepage = btrfs_releasepage,
  7662. .set_page_dirty = btrfs_set_page_dirty,
  7663. .error_remove_page = generic_error_remove_page,
  7664. };
  7665. static const struct address_space_operations btrfs_symlink_aops = {
  7666. .readpage = btrfs_readpage,
  7667. .writepage = btrfs_writepage,
  7668. .invalidatepage = btrfs_invalidatepage,
  7669. .releasepage = btrfs_releasepage,
  7670. };
  7671. static const struct inode_operations btrfs_file_inode_operations = {
  7672. .getattr = btrfs_getattr,
  7673. .setattr = btrfs_setattr,
  7674. .setxattr = btrfs_setxattr,
  7675. .getxattr = btrfs_getxattr,
  7676. .listxattr = btrfs_listxattr,
  7677. .removexattr = btrfs_removexattr,
  7678. .permission = btrfs_permission,
  7679. .fiemap = btrfs_fiemap,
  7680. .get_acl = btrfs_get_acl,
  7681. .update_time = btrfs_update_time,
  7682. };
  7683. static const struct inode_operations btrfs_special_inode_operations = {
  7684. .getattr = btrfs_getattr,
  7685. .setattr = btrfs_setattr,
  7686. .permission = btrfs_permission,
  7687. .setxattr = btrfs_setxattr,
  7688. .getxattr = btrfs_getxattr,
  7689. .listxattr = btrfs_listxattr,
  7690. .removexattr = btrfs_removexattr,
  7691. .get_acl = btrfs_get_acl,
  7692. .update_time = btrfs_update_time,
  7693. };
  7694. static const struct inode_operations btrfs_symlink_inode_operations = {
  7695. .readlink = generic_readlink,
  7696. .follow_link = page_follow_link_light,
  7697. .put_link = page_put_link,
  7698. .getattr = btrfs_getattr,
  7699. .setattr = btrfs_setattr,
  7700. .permission = btrfs_permission,
  7701. .setxattr = btrfs_setxattr,
  7702. .getxattr = btrfs_getxattr,
  7703. .listxattr = btrfs_listxattr,
  7704. .removexattr = btrfs_removexattr,
  7705. .get_acl = btrfs_get_acl,
  7706. .update_time = btrfs_update_time,
  7707. };
  7708. const struct dentry_operations btrfs_dentry_operations = {
  7709. .d_delete = btrfs_dentry_delete,
  7710. .d_release = btrfs_dentry_release,
  7711. };