inode.c 231 KB

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