inode.c 209 KB

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