inode.c 250 KB

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