inode.c 230 KB

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