inode.c 230 KB

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