inode.c 230 KB

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