inode.c 283 KB

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