inode.c 284 KB

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