inode.c 289 KB

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