inode.c 283 KB

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