inode.c 211 KB

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