inode.c 213 KB

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