inode.c 231 KB

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