inode.c 202 KB

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