inode.c 207 KB

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