inode.c 208 KB

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