inode.c 230 KB

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