inode.c 213 KB

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