inode.c 198 KB

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