inode.c 238 KB

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