inode.c 254 KB

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