inode.c 210 KB

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