inode.c 231 KB

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