inode.c 230 KB

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