inode.c 230 KB

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