inode.c 230 KB

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