inode.c 211 KB

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