inode.c 204 KB

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