inode.c 203 KB

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