inode.c 262 KB

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