inode.c 283 KB

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