inode.c 285 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624
  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. * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
  1718. * we don't create 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, fs_info, 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 btrfs_root *root,
  3600. struct inode *dir, u64 objectid,
  3601. const char *name, int name_len)
  3602. {
  3603. struct btrfs_fs_info *fs_info = root->fs_info;
  3604. struct btrfs_path *path;
  3605. struct extent_buffer *leaf;
  3606. struct btrfs_dir_item *di;
  3607. struct btrfs_key key;
  3608. u64 index;
  3609. int ret;
  3610. u64 dir_ino = btrfs_ino(BTRFS_I(dir));
  3611. path = btrfs_alloc_path();
  3612. if (!path)
  3613. return -ENOMEM;
  3614. di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
  3615. name, name_len, -1);
  3616. if (IS_ERR_OR_NULL(di)) {
  3617. if (!di)
  3618. ret = -ENOENT;
  3619. else
  3620. ret = PTR_ERR(di);
  3621. goto out;
  3622. }
  3623. leaf = path->nodes[0];
  3624. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  3625. WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
  3626. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  3627. if (ret) {
  3628. btrfs_abort_transaction(trans, ret);
  3629. goto out;
  3630. }
  3631. btrfs_release_path(path);
  3632. ret = btrfs_del_root_ref(trans, fs_info, objectid,
  3633. root->root_key.objectid, dir_ino,
  3634. &index, name, name_len);
  3635. if (ret < 0) {
  3636. if (ret != -ENOENT) {
  3637. btrfs_abort_transaction(trans, ret);
  3638. goto out;
  3639. }
  3640. di = btrfs_search_dir_index_item(root, path, dir_ino,
  3641. name, name_len);
  3642. if (IS_ERR_OR_NULL(di)) {
  3643. if (!di)
  3644. ret = -ENOENT;
  3645. else
  3646. ret = PTR_ERR(di);
  3647. btrfs_abort_transaction(trans, ret);
  3648. goto out;
  3649. }
  3650. leaf = path->nodes[0];
  3651. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  3652. btrfs_release_path(path);
  3653. index = key.offset;
  3654. }
  3655. btrfs_release_path(path);
  3656. ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
  3657. if (ret) {
  3658. btrfs_abort_transaction(trans, ret);
  3659. goto out;
  3660. }
  3661. btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
  3662. inode_inc_iversion(dir);
  3663. dir->i_mtime = dir->i_ctime = current_time(dir);
  3664. ret = btrfs_update_inode_fallback(trans, root, dir);
  3665. if (ret)
  3666. btrfs_abort_transaction(trans, ret);
  3667. out:
  3668. btrfs_free_path(path);
  3669. return ret;
  3670. }
  3671. /*
  3672. * Helper to check if the subvolume references other subvolumes or if it's
  3673. * default.
  3674. */
  3675. static noinline int may_destroy_subvol(struct btrfs_root *root)
  3676. {
  3677. struct btrfs_fs_info *fs_info = root->fs_info;
  3678. struct btrfs_path *path;
  3679. struct btrfs_dir_item *di;
  3680. struct btrfs_key key;
  3681. u64 dir_id;
  3682. int ret;
  3683. path = btrfs_alloc_path();
  3684. if (!path)
  3685. return -ENOMEM;
  3686. /* Make sure this root isn't set as the default subvol */
  3687. dir_id = btrfs_super_root_dir(fs_info->super_copy);
  3688. di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
  3689. dir_id, "default", 7, 0);
  3690. if (di && !IS_ERR(di)) {
  3691. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
  3692. if (key.objectid == root->root_key.objectid) {
  3693. ret = -EPERM;
  3694. btrfs_err(fs_info,
  3695. "deleting default subvolume %llu is not allowed",
  3696. key.objectid);
  3697. goto out;
  3698. }
  3699. btrfs_release_path(path);
  3700. }
  3701. key.objectid = root->root_key.objectid;
  3702. key.type = BTRFS_ROOT_REF_KEY;
  3703. key.offset = (u64)-1;
  3704. ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
  3705. if (ret < 0)
  3706. goto out;
  3707. BUG_ON(ret == 0);
  3708. ret = 0;
  3709. if (path->slots[0] > 0) {
  3710. path->slots[0]--;
  3711. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  3712. if (key.objectid == root->root_key.objectid &&
  3713. key.type == BTRFS_ROOT_REF_KEY)
  3714. ret = -ENOTEMPTY;
  3715. }
  3716. out:
  3717. btrfs_free_path(path);
  3718. return ret;
  3719. }
  3720. /* Delete all dentries for inodes belonging to the root */
  3721. static void btrfs_prune_dentries(struct btrfs_root *root)
  3722. {
  3723. struct btrfs_fs_info *fs_info = root->fs_info;
  3724. struct rb_node *node;
  3725. struct rb_node *prev;
  3726. struct btrfs_inode *entry;
  3727. struct inode *inode;
  3728. u64 objectid = 0;
  3729. if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
  3730. WARN_ON(btrfs_root_refs(&root->root_item) != 0);
  3731. spin_lock(&root->inode_lock);
  3732. again:
  3733. node = root->inode_tree.rb_node;
  3734. prev = NULL;
  3735. while (node) {
  3736. prev = node;
  3737. entry = rb_entry(node, struct btrfs_inode, rb_node);
  3738. if (objectid < btrfs_ino(entry))
  3739. node = node->rb_left;
  3740. else if (objectid > btrfs_ino(entry))
  3741. node = node->rb_right;
  3742. else
  3743. break;
  3744. }
  3745. if (!node) {
  3746. while (prev) {
  3747. entry = rb_entry(prev, struct btrfs_inode, rb_node);
  3748. if (objectid <= btrfs_ino(entry)) {
  3749. node = prev;
  3750. break;
  3751. }
  3752. prev = rb_next(prev);
  3753. }
  3754. }
  3755. while (node) {
  3756. entry = rb_entry(node, struct btrfs_inode, rb_node);
  3757. objectid = btrfs_ino(entry) + 1;
  3758. inode = igrab(&entry->vfs_inode);
  3759. if (inode) {
  3760. spin_unlock(&root->inode_lock);
  3761. if (atomic_read(&inode->i_count) > 1)
  3762. d_prune_aliases(inode);
  3763. /*
  3764. * btrfs_drop_inode will have it removed from the inode
  3765. * cache when its usage count hits zero.
  3766. */
  3767. iput(inode);
  3768. cond_resched();
  3769. spin_lock(&root->inode_lock);
  3770. goto again;
  3771. }
  3772. if (cond_resched_lock(&root->inode_lock))
  3773. goto again;
  3774. node = rb_next(node);
  3775. }
  3776. spin_unlock(&root->inode_lock);
  3777. }
  3778. int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
  3779. {
  3780. struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
  3781. struct btrfs_root *root = BTRFS_I(dir)->root;
  3782. struct inode *inode = d_inode(dentry);
  3783. struct btrfs_root *dest = BTRFS_I(inode)->root;
  3784. struct btrfs_trans_handle *trans;
  3785. struct btrfs_block_rsv block_rsv;
  3786. u64 root_flags;
  3787. int ret;
  3788. int err;
  3789. /*
  3790. * Don't allow to delete a subvolume with send in progress. This is
  3791. * inside the inode lock so the error handling that has to drop the bit
  3792. * again is not run concurrently.
  3793. */
  3794. spin_lock(&dest->root_item_lock);
  3795. root_flags = btrfs_root_flags(&dest->root_item);
  3796. if (dest->send_in_progress == 0) {
  3797. btrfs_set_root_flags(&dest->root_item,
  3798. root_flags | BTRFS_ROOT_SUBVOL_DEAD);
  3799. spin_unlock(&dest->root_item_lock);
  3800. } else {
  3801. spin_unlock(&dest->root_item_lock);
  3802. btrfs_warn(fs_info,
  3803. "attempt to delete subvolume %llu during send",
  3804. dest->root_key.objectid);
  3805. return -EPERM;
  3806. }
  3807. down_write(&fs_info->subvol_sem);
  3808. err = may_destroy_subvol(dest);
  3809. if (err)
  3810. goto out_up_write;
  3811. btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
  3812. /*
  3813. * One for dir inode,
  3814. * two for dir entries,
  3815. * two for root ref/backref.
  3816. */
  3817. err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
  3818. if (err)
  3819. goto out_up_write;
  3820. trans = btrfs_start_transaction(root, 0);
  3821. if (IS_ERR(trans)) {
  3822. err = PTR_ERR(trans);
  3823. goto out_release;
  3824. }
  3825. trans->block_rsv = &block_rsv;
  3826. trans->bytes_reserved = block_rsv.size;
  3827. btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
  3828. ret = btrfs_unlink_subvol(trans, root, dir,
  3829. dest->root_key.objectid,
  3830. dentry->d_name.name,
  3831. dentry->d_name.len);
  3832. if (ret) {
  3833. err = ret;
  3834. btrfs_abort_transaction(trans, ret);
  3835. goto out_end_trans;
  3836. }
  3837. btrfs_record_root_in_trans(trans, dest);
  3838. memset(&dest->root_item.drop_progress, 0,
  3839. sizeof(dest->root_item.drop_progress));
  3840. dest->root_item.drop_level = 0;
  3841. btrfs_set_root_refs(&dest->root_item, 0);
  3842. if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
  3843. ret = btrfs_insert_orphan_item(trans,
  3844. fs_info->tree_root,
  3845. dest->root_key.objectid);
  3846. if (ret) {
  3847. btrfs_abort_transaction(trans, ret);
  3848. err = ret;
  3849. goto out_end_trans;
  3850. }
  3851. }
  3852. ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
  3853. BTRFS_UUID_KEY_SUBVOL,
  3854. dest->root_key.objectid);
  3855. if (ret && ret != -ENOENT) {
  3856. btrfs_abort_transaction(trans, ret);
  3857. err = ret;
  3858. goto out_end_trans;
  3859. }
  3860. if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
  3861. ret = btrfs_uuid_tree_remove(trans,
  3862. dest->root_item.received_uuid,
  3863. BTRFS_UUID_KEY_RECEIVED_SUBVOL,
  3864. dest->root_key.objectid);
  3865. if (ret && ret != -ENOENT) {
  3866. btrfs_abort_transaction(trans, ret);
  3867. err = ret;
  3868. goto out_end_trans;
  3869. }
  3870. }
  3871. out_end_trans:
  3872. trans->block_rsv = NULL;
  3873. trans->bytes_reserved = 0;
  3874. ret = btrfs_end_transaction(trans);
  3875. if (ret && !err)
  3876. err = ret;
  3877. inode->i_flags |= S_DEAD;
  3878. out_release:
  3879. btrfs_subvolume_release_metadata(fs_info, &block_rsv);
  3880. out_up_write:
  3881. up_write(&fs_info->subvol_sem);
  3882. if (err) {
  3883. spin_lock(&dest->root_item_lock);
  3884. root_flags = btrfs_root_flags(&dest->root_item);
  3885. btrfs_set_root_flags(&dest->root_item,
  3886. root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
  3887. spin_unlock(&dest->root_item_lock);
  3888. } else {
  3889. d_invalidate(dentry);
  3890. btrfs_prune_dentries(dest);
  3891. ASSERT(dest->send_in_progress == 0);
  3892. /* the last ref */
  3893. if (dest->ino_cache_inode) {
  3894. iput(dest->ino_cache_inode);
  3895. dest->ino_cache_inode = NULL;
  3896. }
  3897. }
  3898. return err;
  3899. }
  3900. static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
  3901. {
  3902. struct inode *inode = d_inode(dentry);
  3903. int err = 0;
  3904. struct btrfs_root *root = BTRFS_I(dir)->root;
  3905. struct btrfs_trans_handle *trans;
  3906. u64 last_unlink_trans;
  3907. if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
  3908. return -ENOTEMPTY;
  3909. if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
  3910. return btrfs_delete_subvolume(dir, dentry);
  3911. trans = __unlink_start_trans(dir);
  3912. if (IS_ERR(trans))
  3913. return PTR_ERR(trans);
  3914. if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
  3915. err = btrfs_unlink_subvol(trans, root, dir,
  3916. BTRFS_I(inode)->location.objectid,
  3917. dentry->d_name.name,
  3918. dentry->d_name.len);
  3919. goto out;
  3920. }
  3921. err = btrfs_orphan_add(trans, BTRFS_I(inode));
  3922. if (err)
  3923. goto out;
  3924. last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
  3925. /* now the directory is empty */
  3926. err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
  3927. BTRFS_I(d_inode(dentry)), dentry->d_name.name,
  3928. dentry->d_name.len);
  3929. if (!err) {
  3930. btrfs_i_size_write(BTRFS_I(inode), 0);
  3931. /*
  3932. * Propagate the last_unlink_trans value of the deleted dir to
  3933. * its parent directory. This is to prevent an unrecoverable
  3934. * log tree in the case we do something like this:
  3935. * 1) create dir foo
  3936. * 2) create snapshot under dir foo
  3937. * 3) delete the snapshot
  3938. * 4) rmdir foo
  3939. * 5) mkdir foo
  3940. * 6) fsync foo or some file inside foo
  3941. */
  3942. if (last_unlink_trans >= trans->transid)
  3943. BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
  3944. }
  3945. out:
  3946. btrfs_end_transaction(trans);
  3947. btrfs_btree_balance_dirty(root->fs_info);
  3948. return err;
  3949. }
  3950. static int truncate_space_check(struct btrfs_trans_handle *trans,
  3951. struct btrfs_root *root,
  3952. u64 bytes_deleted)
  3953. {
  3954. struct btrfs_fs_info *fs_info = root->fs_info;
  3955. int ret;
  3956. /*
  3957. * This is only used to apply pressure to the enospc system, we don't
  3958. * intend to use this reservation at all.
  3959. */
  3960. bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
  3961. bytes_deleted *= fs_info->nodesize;
  3962. ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
  3963. bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
  3964. if (!ret) {
  3965. trace_btrfs_space_reservation(fs_info, "transaction",
  3966. trans->transid,
  3967. bytes_deleted, 1);
  3968. trans->bytes_reserved += bytes_deleted;
  3969. }
  3970. return ret;
  3971. }
  3972. /*
  3973. * Return this if we need to call truncate_block for the last bit of the
  3974. * truncate.
  3975. */
  3976. #define NEED_TRUNCATE_BLOCK 1
  3977. /*
  3978. * this can truncate away extent items, csum items and directory items.
  3979. * It starts at a high offset and removes keys until it can't find
  3980. * any higher than new_size
  3981. *
  3982. * csum items that cross the new i_size are truncated to the new size
  3983. * as well.
  3984. *
  3985. * min_type is the minimum key type to truncate down to. If set to 0, this
  3986. * will kill all the items on this inode, including the INODE_ITEM_KEY.
  3987. */
  3988. int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
  3989. struct btrfs_root *root,
  3990. struct inode *inode,
  3991. u64 new_size, u32 min_type)
  3992. {
  3993. struct btrfs_fs_info *fs_info = root->fs_info;
  3994. struct btrfs_path *path;
  3995. struct extent_buffer *leaf;
  3996. struct btrfs_file_extent_item *fi;
  3997. struct btrfs_key key;
  3998. struct btrfs_key found_key;
  3999. u64 extent_start = 0;
  4000. u64 extent_num_bytes = 0;
  4001. u64 extent_offset = 0;
  4002. u64 item_end = 0;
  4003. u64 last_size = new_size;
  4004. u32 found_type = (u8)-1;
  4005. int found_extent;
  4006. int del_item;
  4007. int pending_del_nr = 0;
  4008. int pending_del_slot = 0;
  4009. int extent_type = -1;
  4010. int ret;
  4011. u64 ino = btrfs_ino(BTRFS_I(inode));
  4012. u64 bytes_deleted = 0;
  4013. bool be_nice = false;
  4014. bool should_throttle = false;
  4015. bool should_end = false;
  4016. BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
  4017. /*
  4018. * for non-free space inodes and ref cows, we want to back off from
  4019. * time to time
  4020. */
  4021. if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
  4022. test_bit(BTRFS_ROOT_REF_COWS, &root->state))
  4023. be_nice = true;
  4024. path = btrfs_alloc_path();
  4025. if (!path)
  4026. return -ENOMEM;
  4027. path->reada = READA_BACK;
  4028. /*
  4029. * We want to drop from the next block forward in case this new size is
  4030. * not block aligned since we will be keeping the last block of the
  4031. * extent just the way it is.
  4032. */
  4033. if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
  4034. root == fs_info->tree_root)
  4035. btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
  4036. fs_info->sectorsize),
  4037. (u64)-1, 0);
  4038. /*
  4039. * This function is also used to drop the items in the log tree before
  4040. * we relog the inode, so if root != BTRFS_I(inode)->root, it means
  4041. * it is used to drop the loged items. So we shouldn't kill the delayed
  4042. * items.
  4043. */
  4044. if (min_type == 0 && root == BTRFS_I(inode)->root)
  4045. btrfs_kill_delayed_inode_items(BTRFS_I(inode));
  4046. key.objectid = ino;
  4047. key.offset = (u64)-1;
  4048. key.type = (u8)-1;
  4049. search_again:
  4050. /*
  4051. * with a 16K leaf size and 128MB extents, you can actually queue
  4052. * up a huge file in a single leaf. Most of the time that
  4053. * bytes_deleted is > 0, it will be huge by the time we get here
  4054. */
  4055. if (be_nice && bytes_deleted > SZ_32M &&
  4056. btrfs_should_end_transaction(trans)) {
  4057. ret = -EAGAIN;
  4058. goto out;
  4059. }
  4060. path->leave_spinning = 1;
  4061. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  4062. if (ret < 0)
  4063. goto out;
  4064. if (ret > 0) {
  4065. ret = 0;
  4066. /* there are no items in the tree for us to truncate, we're
  4067. * done
  4068. */
  4069. if (path->slots[0] == 0)
  4070. goto out;
  4071. path->slots[0]--;
  4072. }
  4073. while (1) {
  4074. fi = NULL;
  4075. leaf = path->nodes[0];
  4076. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  4077. found_type = found_key.type;
  4078. if (found_key.objectid != ino)
  4079. break;
  4080. if (found_type < min_type)
  4081. break;
  4082. item_end = found_key.offset;
  4083. if (found_type == BTRFS_EXTENT_DATA_KEY) {
  4084. fi = btrfs_item_ptr(leaf, path->slots[0],
  4085. struct btrfs_file_extent_item);
  4086. extent_type = btrfs_file_extent_type(leaf, fi);
  4087. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  4088. item_end +=
  4089. btrfs_file_extent_num_bytes(leaf, fi);
  4090. trace_btrfs_truncate_show_fi_regular(
  4091. BTRFS_I(inode), leaf, fi,
  4092. found_key.offset);
  4093. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  4094. item_end += btrfs_file_extent_ram_bytes(leaf,
  4095. fi);
  4096. trace_btrfs_truncate_show_fi_inline(
  4097. BTRFS_I(inode), leaf, fi, path->slots[0],
  4098. found_key.offset);
  4099. }
  4100. item_end--;
  4101. }
  4102. if (found_type > min_type) {
  4103. del_item = 1;
  4104. } else {
  4105. if (item_end < new_size)
  4106. break;
  4107. if (found_key.offset >= new_size)
  4108. del_item = 1;
  4109. else
  4110. del_item = 0;
  4111. }
  4112. found_extent = 0;
  4113. /* FIXME, shrink the extent if the ref count is only 1 */
  4114. if (found_type != BTRFS_EXTENT_DATA_KEY)
  4115. goto delete;
  4116. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  4117. u64 num_dec;
  4118. extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
  4119. if (!del_item) {
  4120. u64 orig_num_bytes =
  4121. btrfs_file_extent_num_bytes(leaf, fi);
  4122. extent_num_bytes = ALIGN(new_size -
  4123. found_key.offset,
  4124. fs_info->sectorsize);
  4125. btrfs_set_file_extent_num_bytes(leaf, fi,
  4126. extent_num_bytes);
  4127. num_dec = (orig_num_bytes -
  4128. extent_num_bytes);
  4129. if (test_bit(BTRFS_ROOT_REF_COWS,
  4130. &root->state) &&
  4131. extent_start != 0)
  4132. inode_sub_bytes(inode, num_dec);
  4133. btrfs_mark_buffer_dirty(leaf);
  4134. } else {
  4135. extent_num_bytes =
  4136. btrfs_file_extent_disk_num_bytes(leaf,
  4137. fi);
  4138. extent_offset = found_key.offset -
  4139. btrfs_file_extent_offset(leaf, fi);
  4140. /* FIXME blocksize != 4096 */
  4141. num_dec = btrfs_file_extent_num_bytes(leaf, fi);
  4142. if (extent_start != 0) {
  4143. found_extent = 1;
  4144. if (test_bit(BTRFS_ROOT_REF_COWS,
  4145. &root->state))
  4146. inode_sub_bytes(inode, num_dec);
  4147. }
  4148. }
  4149. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  4150. /*
  4151. * we can't truncate inline items that have had
  4152. * special encodings
  4153. */
  4154. if (!del_item &&
  4155. btrfs_file_extent_encryption(leaf, fi) == 0 &&
  4156. btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
  4157. btrfs_file_extent_compression(leaf, fi) == 0) {
  4158. u32 size = (u32)(new_size - found_key.offset);
  4159. btrfs_set_file_extent_ram_bytes(leaf, fi, size);
  4160. size = btrfs_file_extent_calc_inline_size(size);
  4161. btrfs_truncate_item(root->fs_info, path, size, 1);
  4162. } else if (!del_item) {
  4163. /*
  4164. * We have to bail so the last_size is set to
  4165. * just before this extent.
  4166. */
  4167. ret = NEED_TRUNCATE_BLOCK;
  4168. break;
  4169. }
  4170. if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
  4171. inode_sub_bytes(inode, item_end + 1 - new_size);
  4172. }
  4173. delete:
  4174. if (del_item)
  4175. last_size = found_key.offset;
  4176. else
  4177. last_size = new_size;
  4178. if (del_item) {
  4179. if (!pending_del_nr) {
  4180. /* no pending yet, add ourselves */
  4181. pending_del_slot = path->slots[0];
  4182. pending_del_nr = 1;
  4183. } else if (pending_del_nr &&
  4184. path->slots[0] + 1 == pending_del_slot) {
  4185. /* hop on the pending chunk */
  4186. pending_del_nr++;
  4187. pending_del_slot = path->slots[0];
  4188. } else {
  4189. BUG();
  4190. }
  4191. } else {
  4192. break;
  4193. }
  4194. should_throttle = false;
  4195. if (found_extent &&
  4196. (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
  4197. root == fs_info->tree_root)) {
  4198. btrfs_set_path_blocking(path);
  4199. bytes_deleted += extent_num_bytes;
  4200. ret = btrfs_free_extent(trans, root, extent_start,
  4201. extent_num_bytes, 0,
  4202. btrfs_header_owner(leaf),
  4203. ino, extent_offset);
  4204. if (ret) {
  4205. btrfs_abort_transaction(trans, ret);
  4206. break;
  4207. }
  4208. if (btrfs_should_throttle_delayed_refs(trans, fs_info))
  4209. btrfs_async_run_delayed_refs(fs_info,
  4210. trans->delayed_ref_updates * 2,
  4211. trans->transid, 0);
  4212. if (be_nice) {
  4213. if (truncate_space_check(trans, root,
  4214. extent_num_bytes)) {
  4215. should_end = true;
  4216. }
  4217. if (btrfs_should_throttle_delayed_refs(trans,
  4218. fs_info))
  4219. should_throttle = true;
  4220. }
  4221. }
  4222. if (found_type == BTRFS_INODE_ITEM_KEY)
  4223. break;
  4224. if (path->slots[0] == 0 ||
  4225. path->slots[0] != pending_del_slot ||
  4226. should_throttle || should_end) {
  4227. if (pending_del_nr) {
  4228. ret = btrfs_del_items(trans, root, path,
  4229. pending_del_slot,
  4230. pending_del_nr);
  4231. if (ret) {
  4232. btrfs_abort_transaction(trans, ret);
  4233. break;
  4234. }
  4235. pending_del_nr = 0;
  4236. }
  4237. btrfs_release_path(path);
  4238. if (should_throttle) {
  4239. unsigned long updates = trans->delayed_ref_updates;
  4240. if (updates) {
  4241. trans->delayed_ref_updates = 0;
  4242. ret = btrfs_run_delayed_refs(trans,
  4243. updates * 2);
  4244. if (ret)
  4245. break;
  4246. }
  4247. }
  4248. /*
  4249. * if we failed to refill our space rsv, bail out
  4250. * and let the transaction restart
  4251. */
  4252. if (should_end) {
  4253. ret = -EAGAIN;
  4254. break;
  4255. }
  4256. goto search_again;
  4257. } else {
  4258. path->slots[0]--;
  4259. }
  4260. }
  4261. out:
  4262. if (ret >= 0 && pending_del_nr) {
  4263. int err;
  4264. err = btrfs_del_items(trans, root, path, pending_del_slot,
  4265. pending_del_nr);
  4266. if (err) {
  4267. btrfs_abort_transaction(trans, err);
  4268. ret = err;
  4269. }
  4270. }
  4271. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
  4272. ASSERT(last_size >= new_size);
  4273. if (!ret && last_size > new_size)
  4274. last_size = new_size;
  4275. btrfs_ordered_update_i_size(inode, last_size, NULL);
  4276. }
  4277. btrfs_free_path(path);
  4278. if (be_nice && bytes_deleted > SZ_32M && (ret >= 0 || ret == -EAGAIN)) {
  4279. unsigned long updates = trans->delayed_ref_updates;
  4280. int err;
  4281. if (updates) {
  4282. trans->delayed_ref_updates = 0;
  4283. err = btrfs_run_delayed_refs(trans, updates * 2);
  4284. if (err)
  4285. ret = err;
  4286. }
  4287. }
  4288. return ret;
  4289. }
  4290. /*
  4291. * btrfs_truncate_block - read, zero a chunk and write a block
  4292. * @inode - inode that we're zeroing
  4293. * @from - the offset to start zeroing
  4294. * @len - the length to zero, 0 to zero the entire range respective to the
  4295. * offset
  4296. * @front - zero up to the offset instead of from the offset on
  4297. *
  4298. * This will find the block for the "from" offset and cow the block and zero the
  4299. * part we want to zero. This is used with truncate and hole punching.
  4300. */
  4301. int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
  4302. int front)
  4303. {
  4304. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  4305. struct address_space *mapping = inode->i_mapping;
  4306. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  4307. struct btrfs_ordered_extent *ordered;
  4308. struct extent_state *cached_state = NULL;
  4309. struct extent_changeset *data_reserved = NULL;
  4310. char *kaddr;
  4311. u32 blocksize = fs_info->sectorsize;
  4312. pgoff_t index = from >> PAGE_SHIFT;
  4313. unsigned offset = from & (blocksize - 1);
  4314. struct page *page;
  4315. gfp_t mask = btrfs_alloc_write_mask(mapping);
  4316. int ret = 0;
  4317. u64 block_start;
  4318. u64 block_end;
  4319. if (IS_ALIGNED(offset, blocksize) &&
  4320. (!len || IS_ALIGNED(len, blocksize)))
  4321. goto out;
  4322. block_start = round_down(from, blocksize);
  4323. block_end = block_start + blocksize - 1;
  4324. ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
  4325. block_start, blocksize);
  4326. if (ret)
  4327. goto out;
  4328. again:
  4329. page = find_or_create_page(mapping, index, mask);
  4330. if (!page) {
  4331. btrfs_delalloc_release_space(inode, data_reserved,
  4332. block_start, blocksize, true);
  4333. btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, true);
  4334. ret = -ENOMEM;
  4335. goto out;
  4336. }
  4337. if (!PageUptodate(page)) {
  4338. ret = btrfs_readpage(NULL, page);
  4339. lock_page(page);
  4340. if (page->mapping != mapping) {
  4341. unlock_page(page);
  4342. put_page(page);
  4343. goto again;
  4344. }
  4345. if (!PageUptodate(page)) {
  4346. ret = -EIO;
  4347. goto out_unlock;
  4348. }
  4349. }
  4350. wait_on_page_writeback(page);
  4351. lock_extent_bits(io_tree, block_start, block_end, &cached_state);
  4352. set_page_extent_mapped(page);
  4353. ordered = btrfs_lookup_ordered_extent(inode, block_start);
  4354. if (ordered) {
  4355. unlock_extent_cached(io_tree, block_start, block_end,
  4356. &cached_state);
  4357. unlock_page(page);
  4358. put_page(page);
  4359. btrfs_start_ordered_extent(inode, ordered, 1);
  4360. btrfs_put_ordered_extent(ordered);
  4361. goto again;
  4362. }
  4363. clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
  4364. EXTENT_DIRTY | EXTENT_DELALLOC |
  4365. EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
  4366. 0, 0, &cached_state);
  4367. ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
  4368. &cached_state, 0);
  4369. if (ret) {
  4370. unlock_extent_cached(io_tree, block_start, block_end,
  4371. &cached_state);
  4372. goto out_unlock;
  4373. }
  4374. if (offset != blocksize) {
  4375. if (!len)
  4376. len = blocksize - offset;
  4377. kaddr = kmap(page);
  4378. if (front)
  4379. memset(kaddr + (block_start - page_offset(page)),
  4380. 0, offset);
  4381. else
  4382. memset(kaddr + (block_start - page_offset(page)) + offset,
  4383. 0, len);
  4384. flush_dcache_page(page);
  4385. kunmap(page);
  4386. }
  4387. ClearPageChecked(page);
  4388. set_page_dirty(page);
  4389. unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
  4390. out_unlock:
  4391. if (ret)
  4392. btrfs_delalloc_release_space(inode, data_reserved, block_start,
  4393. blocksize, true);
  4394. btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, (ret != 0));
  4395. unlock_page(page);
  4396. put_page(page);
  4397. out:
  4398. extent_changeset_free(data_reserved);
  4399. return ret;
  4400. }
  4401. static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
  4402. u64 offset, u64 len)
  4403. {
  4404. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  4405. struct btrfs_trans_handle *trans;
  4406. int ret;
  4407. /*
  4408. * Still need to make sure the inode looks like it's been updated so
  4409. * that any holes get logged if we fsync.
  4410. */
  4411. if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
  4412. BTRFS_I(inode)->last_trans = fs_info->generation;
  4413. BTRFS_I(inode)->last_sub_trans = root->log_transid;
  4414. BTRFS_I(inode)->last_log_commit = root->last_log_commit;
  4415. return 0;
  4416. }
  4417. /*
  4418. * 1 - for the one we're dropping
  4419. * 1 - for the one we're adding
  4420. * 1 - for updating the inode.
  4421. */
  4422. trans = btrfs_start_transaction(root, 3);
  4423. if (IS_ERR(trans))
  4424. return PTR_ERR(trans);
  4425. ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
  4426. if (ret) {
  4427. btrfs_abort_transaction(trans, ret);
  4428. btrfs_end_transaction(trans);
  4429. return ret;
  4430. }
  4431. ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
  4432. offset, 0, 0, len, 0, len, 0, 0, 0);
  4433. if (ret)
  4434. btrfs_abort_transaction(trans, ret);
  4435. else
  4436. btrfs_update_inode(trans, root, inode);
  4437. btrfs_end_transaction(trans);
  4438. return ret;
  4439. }
  4440. /*
  4441. * This function puts in dummy file extents for the area we're creating a hole
  4442. * for. So if we are truncating this file to a larger size we need to insert
  4443. * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
  4444. * the range between oldsize and size
  4445. */
  4446. int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
  4447. {
  4448. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  4449. struct btrfs_root *root = BTRFS_I(inode)->root;
  4450. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  4451. struct extent_map *em = NULL;
  4452. struct extent_state *cached_state = NULL;
  4453. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  4454. u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
  4455. u64 block_end = ALIGN(size, fs_info->sectorsize);
  4456. u64 last_byte;
  4457. u64 cur_offset;
  4458. u64 hole_size;
  4459. int err = 0;
  4460. /*
  4461. * If our size started in the middle of a block we need to zero out the
  4462. * rest of the block before we expand the i_size, otherwise we could
  4463. * expose stale data.
  4464. */
  4465. err = btrfs_truncate_block(inode, oldsize, 0, 0);
  4466. if (err)
  4467. return err;
  4468. if (size <= hole_start)
  4469. return 0;
  4470. while (1) {
  4471. struct btrfs_ordered_extent *ordered;
  4472. lock_extent_bits(io_tree, hole_start, block_end - 1,
  4473. &cached_state);
  4474. ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
  4475. block_end - hole_start);
  4476. if (!ordered)
  4477. break;
  4478. unlock_extent_cached(io_tree, hole_start, block_end - 1,
  4479. &cached_state);
  4480. btrfs_start_ordered_extent(inode, ordered, 1);
  4481. btrfs_put_ordered_extent(ordered);
  4482. }
  4483. cur_offset = hole_start;
  4484. while (1) {
  4485. em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
  4486. block_end - cur_offset, 0);
  4487. if (IS_ERR(em)) {
  4488. err = PTR_ERR(em);
  4489. em = NULL;
  4490. break;
  4491. }
  4492. last_byte = min(extent_map_end(em), block_end);
  4493. last_byte = ALIGN(last_byte, fs_info->sectorsize);
  4494. if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
  4495. struct extent_map *hole_em;
  4496. hole_size = last_byte - cur_offset;
  4497. err = maybe_insert_hole(root, inode, cur_offset,
  4498. hole_size);
  4499. if (err)
  4500. break;
  4501. btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
  4502. cur_offset + hole_size - 1, 0);
  4503. hole_em = alloc_extent_map();
  4504. if (!hole_em) {
  4505. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  4506. &BTRFS_I(inode)->runtime_flags);
  4507. goto next;
  4508. }
  4509. hole_em->start = cur_offset;
  4510. hole_em->len = hole_size;
  4511. hole_em->orig_start = cur_offset;
  4512. hole_em->block_start = EXTENT_MAP_HOLE;
  4513. hole_em->block_len = 0;
  4514. hole_em->orig_block_len = 0;
  4515. hole_em->ram_bytes = hole_size;
  4516. hole_em->bdev = fs_info->fs_devices->latest_bdev;
  4517. hole_em->compress_type = BTRFS_COMPRESS_NONE;
  4518. hole_em->generation = fs_info->generation;
  4519. while (1) {
  4520. write_lock(&em_tree->lock);
  4521. err = add_extent_mapping(em_tree, hole_em, 1);
  4522. write_unlock(&em_tree->lock);
  4523. if (err != -EEXIST)
  4524. break;
  4525. btrfs_drop_extent_cache(BTRFS_I(inode),
  4526. cur_offset,
  4527. cur_offset +
  4528. hole_size - 1, 0);
  4529. }
  4530. free_extent_map(hole_em);
  4531. }
  4532. next:
  4533. free_extent_map(em);
  4534. em = NULL;
  4535. cur_offset = last_byte;
  4536. if (cur_offset >= block_end)
  4537. break;
  4538. }
  4539. free_extent_map(em);
  4540. unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
  4541. return err;
  4542. }
  4543. static int btrfs_setsize(struct inode *inode, struct iattr *attr)
  4544. {
  4545. struct btrfs_root *root = BTRFS_I(inode)->root;
  4546. struct btrfs_trans_handle *trans;
  4547. loff_t oldsize = i_size_read(inode);
  4548. loff_t newsize = attr->ia_size;
  4549. int mask = attr->ia_valid;
  4550. int ret;
  4551. /*
  4552. * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
  4553. * special case where we need to update the times despite not having
  4554. * these flags set. For all other operations the VFS set these flags
  4555. * explicitly if it wants a timestamp update.
  4556. */
  4557. if (newsize != oldsize) {
  4558. inode_inc_iversion(inode);
  4559. if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
  4560. inode->i_ctime = inode->i_mtime =
  4561. current_time(inode);
  4562. }
  4563. if (newsize > oldsize) {
  4564. /*
  4565. * Don't do an expanding truncate while snapshotting is ongoing.
  4566. * This is to ensure the snapshot captures a fully consistent
  4567. * state of this file - if the snapshot captures this expanding
  4568. * truncation, it must capture all writes that happened before
  4569. * this truncation.
  4570. */
  4571. btrfs_wait_for_snapshot_creation(root);
  4572. ret = btrfs_cont_expand(inode, oldsize, newsize);
  4573. if (ret) {
  4574. btrfs_end_write_no_snapshotting(root);
  4575. return ret;
  4576. }
  4577. trans = btrfs_start_transaction(root, 1);
  4578. if (IS_ERR(trans)) {
  4579. btrfs_end_write_no_snapshotting(root);
  4580. return PTR_ERR(trans);
  4581. }
  4582. i_size_write(inode, newsize);
  4583. btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
  4584. pagecache_isize_extended(inode, oldsize, newsize);
  4585. ret = btrfs_update_inode(trans, root, inode);
  4586. btrfs_end_write_no_snapshotting(root);
  4587. btrfs_end_transaction(trans);
  4588. } else {
  4589. /*
  4590. * We're truncating a file that used to have good data down to
  4591. * zero. Make sure it gets into the ordered flush list so that
  4592. * any new writes get down to disk quickly.
  4593. */
  4594. if (newsize == 0)
  4595. set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
  4596. &BTRFS_I(inode)->runtime_flags);
  4597. truncate_setsize(inode, newsize);
  4598. /* Disable nonlocked read DIO to avoid the end less truncate */
  4599. btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
  4600. inode_dio_wait(inode);
  4601. btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
  4602. ret = btrfs_truncate(inode, newsize == oldsize);
  4603. if (ret && inode->i_nlink) {
  4604. int err;
  4605. /*
  4606. * Truncate failed, so fix up the in-memory size. We
  4607. * adjusted disk_i_size down as we removed extents, so
  4608. * wait for disk_i_size to be stable and then update the
  4609. * in-memory size to match.
  4610. */
  4611. err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
  4612. if (err)
  4613. return err;
  4614. i_size_write(inode, BTRFS_I(inode)->disk_i_size);
  4615. }
  4616. }
  4617. return ret;
  4618. }
  4619. static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
  4620. {
  4621. struct inode *inode = d_inode(dentry);
  4622. struct btrfs_root *root = BTRFS_I(inode)->root;
  4623. int err;
  4624. if (btrfs_root_readonly(root))
  4625. return -EROFS;
  4626. err = setattr_prepare(dentry, attr);
  4627. if (err)
  4628. return err;
  4629. if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
  4630. err = btrfs_setsize(inode, attr);
  4631. if (err)
  4632. return err;
  4633. }
  4634. if (attr->ia_valid) {
  4635. setattr_copy(inode, attr);
  4636. inode_inc_iversion(inode);
  4637. err = btrfs_dirty_inode(inode);
  4638. if (!err && attr->ia_valid & ATTR_MODE)
  4639. err = posix_acl_chmod(inode, inode->i_mode);
  4640. }
  4641. return err;
  4642. }
  4643. /*
  4644. * While truncating the inode pages during eviction, we get the VFS calling
  4645. * btrfs_invalidatepage() against each page of the inode. This is slow because
  4646. * the calls to btrfs_invalidatepage() result in a huge amount of calls to
  4647. * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
  4648. * extent_state structures over and over, wasting lots of time.
  4649. *
  4650. * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
  4651. * those expensive operations on a per page basis and do only the ordered io
  4652. * finishing, while we release here the extent_map and extent_state structures,
  4653. * without the excessive merging and splitting.
  4654. */
  4655. static void evict_inode_truncate_pages(struct inode *inode)
  4656. {
  4657. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  4658. struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
  4659. struct rb_node *node;
  4660. ASSERT(inode->i_state & I_FREEING);
  4661. truncate_inode_pages_final(&inode->i_data);
  4662. write_lock(&map_tree->lock);
  4663. while (!RB_EMPTY_ROOT(&map_tree->map)) {
  4664. struct extent_map *em;
  4665. node = rb_first(&map_tree->map);
  4666. em = rb_entry(node, struct extent_map, rb_node);
  4667. clear_bit(EXTENT_FLAG_PINNED, &em->flags);
  4668. clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
  4669. remove_extent_mapping(map_tree, em);
  4670. free_extent_map(em);
  4671. if (need_resched()) {
  4672. write_unlock(&map_tree->lock);
  4673. cond_resched();
  4674. write_lock(&map_tree->lock);
  4675. }
  4676. }
  4677. write_unlock(&map_tree->lock);
  4678. /*
  4679. * Keep looping until we have no more ranges in the io tree.
  4680. * We can have ongoing bios started by readpages (called from readahead)
  4681. * that have their endio callback (extent_io.c:end_bio_extent_readpage)
  4682. * still in progress (unlocked the pages in the bio but did not yet
  4683. * unlocked the ranges in the io tree). Therefore this means some
  4684. * ranges can still be locked and eviction started because before
  4685. * submitting those bios, which are executed by a separate task (work
  4686. * queue kthread), inode references (inode->i_count) were not taken
  4687. * (which would be dropped in the end io callback of each bio).
  4688. * Therefore here we effectively end up waiting for those bios and
  4689. * anyone else holding locked ranges without having bumped the inode's
  4690. * reference count - if we don't do it, when they access the inode's
  4691. * io_tree to unlock a range it may be too late, leading to an
  4692. * use-after-free issue.
  4693. */
  4694. spin_lock(&io_tree->lock);
  4695. while (!RB_EMPTY_ROOT(&io_tree->state)) {
  4696. struct extent_state *state;
  4697. struct extent_state *cached_state = NULL;
  4698. u64 start;
  4699. u64 end;
  4700. node = rb_first(&io_tree->state);
  4701. state = rb_entry(node, struct extent_state, rb_node);
  4702. start = state->start;
  4703. end = state->end;
  4704. spin_unlock(&io_tree->lock);
  4705. lock_extent_bits(io_tree, start, end, &cached_state);
  4706. /*
  4707. * If still has DELALLOC flag, the extent didn't reach disk,
  4708. * and its reserved space won't be freed by delayed_ref.
  4709. * So we need to free its reserved space here.
  4710. * (Refer to comment in btrfs_invalidatepage, case 2)
  4711. *
  4712. * Note, end is the bytenr of last byte, so we need + 1 here.
  4713. */
  4714. if (state->state & EXTENT_DELALLOC)
  4715. btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
  4716. clear_extent_bit(io_tree, start, end,
  4717. EXTENT_LOCKED | EXTENT_DIRTY |
  4718. EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
  4719. EXTENT_DEFRAG, 1, 1, &cached_state);
  4720. cond_resched();
  4721. spin_lock(&io_tree->lock);
  4722. }
  4723. spin_unlock(&io_tree->lock);
  4724. }
  4725. static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
  4726. struct btrfs_block_rsv *rsv,
  4727. u64 min_size)
  4728. {
  4729. struct btrfs_fs_info *fs_info = root->fs_info;
  4730. struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
  4731. int failures = 0;
  4732. for (;;) {
  4733. struct btrfs_trans_handle *trans;
  4734. int ret;
  4735. ret = btrfs_block_rsv_refill(root, rsv, min_size,
  4736. BTRFS_RESERVE_FLUSH_LIMIT);
  4737. if (ret && ++failures > 2) {
  4738. btrfs_warn(fs_info,
  4739. "could not allocate space for a delete; will truncate on mount");
  4740. return ERR_PTR(-ENOSPC);
  4741. }
  4742. trans = btrfs_join_transaction(root);
  4743. if (IS_ERR(trans) || !ret)
  4744. return trans;
  4745. /*
  4746. * Try to steal from the global reserve if there is space for
  4747. * it.
  4748. */
  4749. if (!btrfs_check_space_for_delayed_refs(trans, fs_info) &&
  4750. !btrfs_block_rsv_migrate(global_rsv, rsv, min_size, 0))
  4751. return trans;
  4752. /* If not, commit and try again. */
  4753. ret = btrfs_commit_transaction(trans);
  4754. if (ret)
  4755. return ERR_PTR(ret);
  4756. }
  4757. }
  4758. void btrfs_evict_inode(struct inode *inode)
  4759. {
  4760. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  4761. struct btrfs_trans_handle *trans;
  4762. struct btrfs_root *root = BTRFS_I(inode)->root;
  4763. struct btrfs_block_rsv *rsv;
  4764. u64 min_size;
  4765. int ret;
  4766. trace_btrfs_inode_evict(inode);
  4767. if (!root) {
  4768. clear_inode(inode);
  4769. return;
  4770. }
  4771. min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
  4772. evict_inode_truncate_pages(inode);
  4773. if (inode->i_nlink &&
  4774. ((btrfs_root_refs(&root->root_item) != 0 &&
  4775. root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
  4776. btrfs_is_free_space_inode(BTRFS_I(inode))))
  4777. goto no_delete;
  4778. if (is_bad_inode(inode))
  4779. goto no_delete;
  4780. /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
  4781. if (!special_file(inode->i_mode))
  4782. btrfs_wait_ordered_range(inode, 0, (u64)-1);
  4783. btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
  4784. if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
  4785. goto no_delete;
  4786. if (inode->i_nlink > 0) {
  4787. BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
  4788. root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
  4789. goto no_delete;
  4790. }
  4791. ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
  4792. if (ret)
  4793. goto no_delete;
  4794. rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
  4795. if (!rsv)
  4796. goto no_delete;
  4797. rsv->size = min_size;
  4798. rsv->failfast = 1;
  4799. btrfs_i_size_write(BTRFS_I(inode), 0);
  4800. while (1) {
  4801. trans = evict_refill_and_join(root, rsv, min_size);
  4802. if (IS_ERR(trans))
  4803. goto free_rsv;
  4804. trans->block_rsv = rsv;
  4805. ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
  4806. trans->block_rsv = &fs_info->trans_block_rsv;
  4807. btrfs_end_transaction(trans);
  4808. btrfs_btree_balance_dirty(fs_info);
  4809. if (ret && ret != -ENOSPC && ret != -EAGAIN)
  4810. goto free_rsv;
  4811. else if (!ret)
  4812. break;
  4813. }
  4814. /*
  4815. * Errors here aren't a big deal, it just means we leave orphan items in
  4816. * the tree. They will be cleaned up on the next mount. If the inode
  4817. * number gets reused, cleanup deletes the orphan item without doing
  4818. * anything, and unlink reuses the existing orphan item.
  4819. *
  4820. * If it turns out that we are dropping too many of these, we might want
  4821. * to add a mechanism for retrying these after a commit.
  4822. */
  4823. trans = evict_refill_and_join(root, rsv, min_size);
  4824. if (!IS_ERR(trans)) {
  4825. trans->block_rsv = rsv;
  4826. btrfs_orphan_del(trans, BTRFS_I(inode));
  4827. trans->block_rsv = &fs_info->trans_block_rsv;
  4828. btrfs_end_transaction(trans);
  4829. }
  4830. if (!(root == fs_info->tree_root ||
  4831. root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
  4832. btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
  4833. free_rsv:
  4834. btrfs_free_block_rsv(fs_info, rsv);
  4835. no_delete:
  4836. /*
  4837. * If we didn't successfully delete, the orphan item will still be in
  4838. * the tree and we'll retry on the next mount. Again, we might also want
  4839. * to retry these periodically in the future.
  4840. */
  4841. btrfs_remove_delayed_node(BTRFS_I(inode));
  4842. clear_inode(inode);
  4843. }
  4844. /*
  4845. * this returns the key found in the dir entry in the location pointer.
  4846. * If no dir entries were found, returns -ENOENT.
  4847. * If found a corrupted location in dir entry, returns -EUCLEAN.
  4848. */
  4849. static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
  4850. struct btrfs_key *location)
  4851. {
  4852. const char *name = dentry->d_name.name;
  4853. int namelen = dentry->d_name.len;
  4854. struct btrfs_dir_item *di;
  4855. struct btrfs_path *path;
  4856. struct btrfs_root *root = BTRFS_I(dir)->root;
  4857. int ret = 0;
  4858. path = btrfs_alloc_path();
  4859. if (!path)
  4860. return -ENOMEM;
  4861. di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
  4862. name, namelen, 0);
  4863. if (!di) {
  4864. ret = -ENOENT;
  4865. goto out;
  4866. }
  4867. if (IS_ERR(di)) {
  4868. ret = PTR_ERR(di);
  4869. goto out;
  4870. }
  4871. btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
  4872. if (location->type != BTRFS_INODE_ITEM_KEY &&
  4873. location->type != BTRFS_ROOT_ITEM_KEY) {
  4874. ret = -EUCLEAN;
  4875. btrfs_warn(root->fs_info,
  4876. "%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
  4877. __func__, name, btrfs_ino(BTRFS_I(dir)),
  4878. location->objectid, location->type, location->offset);
  4879. }
  4880. out:
  4881. btrfs_free_path(path);
  4882. return ret;
  4883. }
  4884. /*
  4885. * when we hit a tree root in a directory, the btrfs part of the inode
  4886. * needs to be changed to reflect the root directory of the tree root. This
  4887. * is kind of like crossing a mount point.
  4888. */
  4889. static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
  4890. struct inode *dir,
  4891. struct dentry *dentry,
  4892. struct btrfs_key *location,
  4893. struct btrfs_root **sub_root)
  4894. {
  4895. struct btrfs_path *path;
  4896. struct btrfs_root *new_root;
  4897. struct btrfs_root_ref *ref;
  4898. struct extent_buffer *leaf;
  4899. struct btrfs_key key;
  4900. int ret;
  4901. int err = 0;
  4902. path = btrfs_alloc_path();
  4903. if (!path) {
  4904. err = -ENOMEM;
  4905. goto out;
  4906. }
  4907. err = -ENOENT;
  4908. key.objectid = BTRFS_I(dir)->root->root_key.objectid;
  4909. key.type = BTRFS_ROOT_REF_KEY;
  4910. key.offset = location->objectid;
  4911. ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
  4912. if (ret) {
  4913. if (ret < 0)
  4914. err = ret;
  4915. goto out;
  4916. }
  4917. leaf = path->nodes[0];
  4918. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
  4919. if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
  4920. btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
  4921. goto out;
  4922. ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
  4923. (unsigned long)(ref + 1),
  4924. dentry->d_name.len);
  4925. if (ret)
  4926. goto out;
  4927. btrfs_release_path(path);
  4928. new_root = btrfs_read_fs_root_no_name(fs_info, location);
  4929. if (IS_ERR(new_root)) {
  4930. err = PTR_ERR(new_root);
  4931. goto out;
  4932. }
  4933. *sub_root = new_root;
  4934. location->objectid = btrfs_root_dirid(&new_root->root_item);
  4935. location->type = BTRFS_INODE_ITEM_KEY;
  4936. location->offset = 0;
  4937. err = 0;
  4938. out:
  4939. btrfs_free_path(path);
  4940. return err;
  4941. }
  4942. static void inode_tree_add(struct inode *inode)
  4943. {
  4944. struct btrfs_root *root = BTRFS_I(inode)->root;
  4945. struct btrfs_inode *entry;
  4946. struct rb_node **p;
  4947. struct rb_node *parent;
  4948. struct rb_node *new = &BTRFS_I(inode)->rb_node;
  4949. u64 ino = btrfs_ino(BTRFS_I(inode));
  4950. if (inode_unhashed(inode))
  4951. return;
  4952. parent = NULL;
  4953. spin_lock(&root->inode_lock);
  4954. p = &root->inode_tree.rb_node;
  4955. while (*p) {
  4956. parent = *p;
  4957. entry = rb_entry(parent, struct btrfs_inode, rb_node);
  4958. if (ino < btrfs_ino(entry))
  4959. p = &parent->rb_left;
  4960. else if (ino > btrfs_ino(entry))
  4961. p = &parent->rb_right;
  4962. else {
  4963. WARN_ON(!(entry->vfs_inode.i_state &
  4964. (I_WILL_FREE | I_FREEING)));
  4965. rb_replace_node(parent, new, &root->inode_tree);
  4966. RB_CLEAR_NODE(parent);
  4967. spin_unlock(&root->inode_lock);
  4968. return;
  4969. }
  4970. }
  4971. rb_link_node(new, parent, p);
  4972. rb_insert_color(new, &root->inode_tree);
  4973. spin_unlock(&root->inode_lock);
  4974. }
  4975. static void inode_tree_del(struct inode *inode)
  4976. {
  4977. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  4978. struct btrfs_root *root = BTRFS_I(inode)->root;
  4979. int empty = 0;
  4980. spin_lock(&root->inode_lock);
  4981. if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
  4982. rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
  4983. RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
  4984. empty = RB_EMPTY_ROOT(&root->inode_tree);
  4985. }
  4986. spin_unlock(&root->inode_lock);
  4987. if (empty && btrfs_root_refs(&root->root_item) == 0) {
  4988. synchronize_srcu(&fs_info->subvol_srcu);
  4989. spin_lock(&root->inode_lock);
  4990. empty = RB_EMPTY_ROOT(&root->inode_tree);
  4991. spin_unlock(&root->inode_lock);
  4992. if (empty)
  4993. btrfs_add_dead_root(root);
  4994. }
  4995. }
  4996. static int btrfs_init_locked_inode(struct inode *inode, void *p)
  4997. {
  4998. struct btrfs_iget_args *args = p;
  4999. inode->i_ino = args->location->objectid;
  5000. memcpy(&BTRFS_I(inode)->location, args->location,
  5001. sizeof(*args->location));
  5002. BTRFS_I(inode)->root = args->root;
  5003. return 0;
  5004. }
  5005. static int btrfs_find_actor(struct inode *inode, void *opaque)
  5006. {
  5007. struct btrfs_iget_args *args = opaque;
  5008. return args->location->objectid == BTRFS_I(inode)->location.objectid &&
  5009. args->root == BTRFS_I(inode)->root;
  5010. }
  5011. static struct inode *btrfs_iget_locked(struct super_block *s,
  5012. struct btrfs_key *location,
  5013. struct btrfs_root *root)
  5014. {
  5015. struct inode *inode;
  5016. struct btrfs_iget_args args;
  5017. unsigned long hashval = btrfs_inode_hash(location->objectid, root);
  5018. args.location = location;
  5019. args.root = root;
  5020. inode = iget5_locked(s, hashval, btrfs_find_actor,
  5021. btrfs_init_locked_inode,
  5022. (void *)&args);
  5023. return inode;
  5024. }
  5025. /* Get an inode object given its location and corresponding root.
  5026. * Returns in *is_new if the inode was read from disk
  5027. */
  5028. struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
  5029. struct btrfs_root *root, int *new)
  5030. {
  5031. struct inode *inode;
  5032. inode = btrfs_iget_locked(s, location, root);
  5033. if (!inode)
  5034. return ERR_PTR(-ENOMEM);
  5035. if (inode->i_state & I_NEW) {
  5036. int ret;
  5037. ret = btrfs_read_locked_inode(inode);
  5038. if (!is_bad_inode(inode)) {
  5039. inode_tree_add(inode);
  5040. unlock_new_inode(inode);
  5041. if (new)
  5042. *new = 1;
  5043. } else {
  5044. unlock_new_inode(inode);
  5045. iput(inode);
  5046. ASSERT(ret < 0);
  5047. inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
  5048. }
  5049. }
  5050. return inode;
  5051. }
  5052. static struct inode *new_simple_dir(struct super_block *s,
  5053. struct btrfs_key *key,
  5054. struct btrfs_root *root)
  5055. {
  5056. struct inode *inode = new_inode(s);
  5057. if (!inode)
  5058. return ERR_PTR(-ENOMEM);
  5059. BTRFS_I(inode)->root = root;
  5060. memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
  5061. set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
  5062. inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
  5063. inode->i_op = &btrfs_dir_ro_inode_operations;
  5064. inode->i_opflags &= ~IOP_XATTR;
  5065. inode->i_fop = &simple_dir_operations;
  5066. inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
  5067. inode->i_mtime = current_time(inode);
  5068. inode->i_atime = inode->i_mtime;
  5069. inode->i_ctime = inode->i_mtime;
  5070. BTRFS_I(inode)->i_otime = inode->i_mtime;
  5071. return inode;
  5072. }
  5073. struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
  5074. {
  5075. struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
  5076. struct inode *inode;
  5077. struct btrfs_root *root = BTRFS_I(dir)->root;
  5078. struct btrfs_root *sub_root = root;
  5079. struct btrfs_key location;
  5080. int index;
  5081. int ret = 0;
  5082. if (dentry->d_name.len > BTRFS_NAME_LEN)
  5083. return ERR_PTR(-ENAMETOOLONG);
  5084. ret = btrfs_inode_by_name(dir, dentry, &location);
  5085. if (ret < 0)
  5086. return ERR_PTR(ret);
  5087. if (location.type == BTRFS_INODE_ITEM_KEY) {
  5088. inode = btrfs_iget(dir->i_sb, &location, root, NULL);
  5089. return inode;
  5090. }
  5091. index = srcu_read_lock(&fs_info->subvol_srcu);
  5092. ret = fixup_tree_root_location(fs_info, dir, dentry,
  5093. &location, &sub_root);
  5094. if (ret < 0) {
  5095. if (ret != -ENOENT)
  5096. inode = ERR_PTR(ret);
  5097. else
  5098. inode = new_simple_dir(dir->i_sb, &location, sub_root);
  5099. } else {
  5100. inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
  5101. }
  5102. srcu_read_unlock(&fs_info->subvol_srcu, index);
  5103. if (!IS_ERR(inode) && root != sub_root) {
  5104. down_read(&fs_info->cleanup_work_sem);
  5105. if (!sb_rdonly(inode->i_sb))
  5106. ret = btrfs_orphan_cleanup(sub_root);
  5107. up_read(&fs_info->cleanup_work_sem);
  5108. if (ret) {
  5109. iput(inode);
  5110. inode = ERR_PTR(ret);
  5111. }
  5112. }
  5113. return inode;
  5114. }
  5115. static int btrfs_dentry_delete(const struct dentry *dentry)
  5116. {
  5117. struct btrfs_root *root;
  5118. struct inode *inode = d_inode(dentry);
  5119. if (!inode && !IS_ROOT(dentry))
  5120. inode = d_inode(dentry->d_parent);
  5121. if (inode) {
  5122. root = BTRFS_I(inode)->root;
  5123. if (btrfs_root_refs(&root->root_item) == 0)
  5124. return 1;
  5125. if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
  5126. return 1;
  5127. }
  5128. return 0;
  5129. }
  5130. static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
  5131. unsigned int flags)
  5132. {
  5133. struct inode *inode;
  5134. inode = btrfs_lookup_dentry(dir, dentry);
  5135. if (IS_ERR(inode)) {
  5136. if (PTR_ERR(inode) == -ENOENT)
  5137. inode = NULL;
  5138. else
  5139. return ERR_CAST(inode);
  5140. }
  5141. return d_splice_alias(inode, dentry);
  5142. }
  5143. unsigned char btrfs_filetype_table[] = {
  5144. DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
  5145. };
  5146. /*
  5147. * All this infrastructure exists because dir_emit can fault, and we are holding
  5148. * the tree lock when doing readdir. For now just allocate a buffer and copy
  5149. * our information into that, and then dir_emit from the buffer. This is
  5150. * similar to what NFS does, only we don't keep the buffer around in pagecache
  5151. * because I'm afraid I'll mess that up. Long term we need to make filldir do
  5152. * copy_to_user_inatomic so we don't have to worry about page faulting under the
  5153. * tree lock.
  5154. */
  5155. static int btrfs_opendir(struct inode *inode, struct file *file)
  5156. {
  5157. struct btrfs_file_private *private;
  5158. private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
  5159. if (!private)
  5160. return -ENOMEM;
  5161. private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
  5162. if (!private->filldir_buf) {
  5163. kfree(private);
  5164. return -ENOMEM;
  5165. }
  5166. file->private_data = private;
  5167. return 0;
  5168. }
  5169. struct dir_entry {
  5170. u64 ino;
  5171. u64 offset;
  5172. unsigned type;
  5173. int name_len;
  5174. };
  5175. static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
  5176. {
  5177. while (entries--) {
  5178. struct dir_entry *entry = addr;
  5179. char *name = (char *)(entry + 1);
  5180. ctx->pos = get_unaligned(&entry->offset);
  5181. if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
  5182. get_unaligned(&entry->ino),
  5183. get_unaligned(&entry->type)))
  5184. return 1;
  5185. addr += sizeof(struct dir_entry) +
  5186. get_unaligned(&entry->name_len);
  5187. ctx->pos++;
  5188. }
  5189. return 0;
  5190. }
  5191. static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
  5192. {
  5193. struct inode *inode = file_inode(file);
  5194. struct btrfs_root *root = BTRFS_I(inode)->root;
  5195. struct btrfs_file_private *private = file->private_data;
  5196. struct btrfs_dir_item *di;
  5197. struct btrfs_key key;
  5198. struct btrfs_key found_key;
  5199. struct btrfs_path *path;
  5200. void *addr;
  5201. struct list_head ins_list;
  5202. struct list_head del_list;
  5203. int ret;
  5204. struct extent_buffer *leaf;
  5205. int slot;
  5206. char *name_ptr;
  5207. int name_len;
  5208. int entries = 0;
  5209. int total_len = 0;
  5210. bool put = false;
  5211. struct btrfs_key location;
  5212. if (!dir_emit_dots(file, ctx))
  5213. return 0;
  5214. path = btrfs_alloc_path();
  5215. if (!path)
  5216. return -ENOMEM;
  5217. addr = private->filldir_buf;
  5218. path->reada = READA_FORWARD;
  5219. INIT_LIST_HEAD(&ins_list);
  5220. INIT_LIST_HEAD(&del_list);
  5221. put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
  5222. again:
  5223. key.type = BTRFS_DIR_INDEX_KEY;
  5224. key.offset = ctx->pos;
  5225. key.objectid = btrfs_ino(BTRFS_I(inode));
  5226. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  5227. if (ret < 0)
  5228. goto err;
  5229. while (1) {
  5230. struct dir_entry *entry;
  5231. leaf = path->nodes[0];
  5232. slot = path->slots[0];
  5233. if (slot >= btrfs_header_nritems(leaf)) {
  5234. ret = btrfs_next_leaf(root, path);
  5235. if (ret < 0)
  5236. goto err;
  5237. else if (ret > 0)
  5238. break;
  5239. continue;
  5240. }
  5241. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  5242. if (found_key.objectid != key.objectid)
  5243. break;
  5244. if (found_key.type != BTRFS_DIR_INDEX_KEY)
  5245. break;
  5246. if (found_key.offset < ctx->pos)
  5247. goto next;
  5248. if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
  5249. goto next;
  5250. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  5251. name_len = btrfs_dir_name_len(leaf, di);
  5252. if ((total_len + sizeof(struct dir_entry) + name_len) >=
  5253. PAGE_SIZE) {
  5254. btrfs_release_path(path);
  5255. ret = btrfs_filldir(private->filldir_buf, entries, ctx);
  5256. if (ret)
  5257. goto nopos;
  5258. addr = private->filldir_buf;
  5259. entries = 0;
  5260. total_len = 0;
  5261. goto again;
  5262. }
  5263. entry = addr;
  5264. put_unaligned(name_len, &entry->name_len);
  5265. name_ptr = (char *)(entry + 1);
  5266. read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
  5267. name_len);
  5268. put_unaligned(btrfs_filetype_table[btrfs_dir_type(leaf, di)],
  5269. &entry->type);
  5270. btrfs_dir_item_key_to_cpu(leaf, di, &location);
  5271. put_unaligned(location.objectid, &entry->ino);
  5272. put_unaligned(found_key.offset, &entry->offset);
  5273. entries++;
  5274. addr += sizeof(struct dir_entry) + name_len;
  5275. total_len += sizeof(struct dir_entry) + name_len;
  5276. next:
  5277. path->slots[0]++;
  5278. }
  5279. btrfs_release_path(path);
  5280. ret = btrfs_filldir(private->filldir_buf, entries, ctx);
  5281. if (ret)
  5282. goto nopos;
  5283. ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
  5284. if (ret)
  5285. goto nopos;
  5286. /*
  5287. * Stop new entries from being returned after we return the last
  5288. * entry.
  5289. *
  5290. * New directory entries are assigned a strictly increasing
  5291. * offset. This means that new entries created during readdir
  5292. * are *guaranteed* to be seen in the future by that readdir.
  5293. * This has broken buggy programs which operate on names as
  5294. * they're returned by readdir. Until we re-use freed offsets
  5295. * we have this hack to stop new entries from being returned
  5296. * under the assumption that they'll never reach this huge
  5297. * offset.
  5298. *
  5299. * This is being careful not to overflow 32bit loff_t unless the
  5300. * last entry requires it because doing so has broken 32bit apps
  5301. * in the past.
  5302. */
  5303. if (ctx->pos >= INT_MAX)
  5304. ctx->pos = LLONG_MAX;
  5305. else
  5306. ctx->pos = INT_MAX;
  5307. nopos:
  5308. ret = 0;
  5309. err:
  5310. if (put)
  5311. btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
  5312. btrfs_free_path(path);
  5313. return ret;
  5314. }
  5315. int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
  5316. {
  5317. struct btrfs_root *root = BTRFS_I(inode)->root;
  5318. struct btrfs_trans_handle *trans;
  5319. int ret = 0;
  5320. bool nolock = false;
  5321. if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
  5322. return 0;
  5323. if (btrfs_fs_closing(root->fs_info) &&
  5324. btrfs_is_free_space_inode(BTRFS_I(inode)))
  5325. nolock = true;
  5326. if (wbc->sync_mode == WB_SYNC_ALL) {
  5327. if (nolock)
  5328. trans = btrfs_join_transaction_nolock(root);
  5329. else
  5330. trans = btrfs_join_transaction(root);
  5331. if (IS_ERR(trans))
  5332. return PTR_ERR(trans);
  5333. ret = btrfs_commit_transaction(trans);
  5334. }
  5335. return ret;
  5336. }
  5337. /*
  5338. * This is somewhat expensive, updating the tree every time the
  5339. * inode changes. But, it is most likely to find the inode in cache.
  5340. * FIXME, needs more benchmarking...there are no reasons other than performance
  5341. * to keep or drop this code.
  5342. */
  5343. static int btrfs_dirty_inode(struct inode *inode)
  5344. {
  5345. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  5346. struct btrfs_root *root = BTRFS_I(inode)->root;
  5347. struct btrfs_trans_handle *trans;
  5348. int ret;
  5349. if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
  5350. return 0;
  5351. trans = btrfs_join_transaction(root);
  5352. if (IS_ERR(trans))
  5353. return PTR_ERR(trans);
  5354. ret = btrfs_update_inode(trans, root, inode);
  5355. if (ret && ret == -ENOSPC) {
  5356. /* whoops, lets try again with the full transaction */
  5357. btrfs_end_transaction(trans);
  5358. trans = btrfs_start_transaction(root, 1);
  5359. if (IS_ERR(trans))
  5360. return PTR_ERR(trans);
  5361. ret = btrfs_update_inode(trans, root, inode);
  5362. }
  5363. btrfs_end_transaction(trans);
  5364. if (BTRFS_I(inode)->delayed_node)
  5365. btrfs_balance_delayed_items(fs_info);
  5366. return ret;
  5367. }
  5368. /*
  5369. * This is a copy of file_update_time. We need this so we can return error on
  5370. * ENOSPC for updating the inode in the case of file write and mmap writes.
  5371. */
  5372. static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
  5373. int flags)
  5374. {
  5375. struct btrfs_root *root = BTRFS_I(inode)->root;
  5376. bool dirty = flags & ~S_VERSION;
  5377. if (btrfs_root_readonly(root))
  5378. return -EROFS;
  5379. if (flags & S_VERSION)
  5380. dirty |= inode_maybe_inc_iversion(inode, dirty);
  5381. if (flags & S_CTIME)
  5382. inode->i_ctime = *now;
  5383. if (flags & S_MTIME)
  5384. inode->i_mtime = *now;
  5385. if (flags & S_ATIME)
  5386. inode->i_atime = *now;
  5387. return dirty ? btrfs_dirty_inode(inode) : 0;
  5388. }
  5389. /*
  5390. * find the highest existing sequence number in a directory
  5391. * and then set the in-memory index_cnt variable to reflect
  5392. * free sequence numbers
  5393. */
  5394. static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
  5395. {
  5396. struct btrfs_root *root = inode->root;
  5397. struct btrfs_key key, found_key;
  5398. struct btrfs_path *path;
  5399. struct extent_buffer *leaf;
  5400. int ret;
  5401. key.objectid = btrfs_ino(inode);
  5402. key.type = BTRFS_DIR_INDEX_KEY;
  5403. key.offset = (u64)-1;
  5404. path = btrfs_alloc_path();
  5405. if (!path)
  5406. return -ENOMEM;
  5407. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  5408. if (ret < 0)
  5409. goto out;
  5410. /* FIXME: we should be able to handle this */
  5411. if (ret == 0)
  5412. goto out;
  5413. ret = 0;
  5414. /*
  5415. * MAGIC NUMBER EXPLANATION:
  5416. * since we search a directory based on f_pos we have to start at 2
  5417. * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
  5418. * else has to start at 2
  5419. */
  5420. if (path->slots[0] == 0) {
  5421. inode->index_cnt = 2;
  5422. goto out;
  5423. }
  5424. path->slots[0]--;
  5425. leaf = path->nodes[0];
  5426. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  5427. if (found_key.objectid != btrfs_ino(inode) ||
  5428. found_key.type != BTRFS_DIR_INDEX_KEY) {
  5429. inode->index_cnt = 2;
  5430. goto out;
  5431. }
  5432. inode->index_cnt = found_key.offset + 1;
  5433. out:
  5434. btrfs_free_path(path);
  5435. return ret;
  5436. }
  5437. /*
  5438. * helper to find a free sequence number in a given directory. This current
  5439. * code is very simple, later versions will do smarter things in the btree
  5440. */
  5441. int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
  5442. {
  5443. int ret = 0;
  5444. if (dir->index_cnt == (u64)-1) {
  5445. ret = btrfs_inode_delayed_dir_index_count(dir);
  5446. if (ret) {
  5447. ret = btrfs_set_inode_index_count(dir);
  5448. if (ret)
  5449. return ret;
  5450. }
  5451. }
  5452. *index = dir->index_cnt;
  5453. dir->index_cnt++;
  5454. return ret;
  5455. }
  5456. static int btrfs_insert_inode_locked(struct inode *inode)
  5457. {
  5458. struct btrfs_iget_args args;
  5459. args.location = &BTRFS_I(inode)->location;
  5460. args.root = BTRFS_I(inode)->root;
  5461. return insert_inode_locked4(inode,
  5462. btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
  5463. btrfs_find_actor, &args);
  5464. }
  5465. /*
  5466. * Inherit flags from the parent inode.
  5467. *
  5468. * Currently only the compression flags and the cow flags are inherited.
  5469. */
  5470. static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
  5471. {
  5472. unsigned int flags;
  5473. if (!dir)
  5474. return;
  5475. flags = BTRFS_I(dir)->flags;
  5476. if (flags & BTRFS_INODE_NOCOMPRESS) {
  5477. BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
  5478. BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
  5479. } else if (flags & BTRFS_INODE_COMPRESS) {
  5480. BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
  5481. BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
  5482. }
  5483. if (flags & BTRFS_INODE_NODATACOW) {
  5484. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
  5485. if (S_ISREG(inode->i_mode))
  5486. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
  5487. }
  5488. btrfs_sync_inode_flags_to_i_flags(inode);
  5489. }
  5490. static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
  5491. struct btrfs_root *root,
  5492. struct inode *dir,
  5493. const char *name, int name_len,
  5494. u64 ref_objectid, u64 objectid,
  5495. umode_t mode, u64 *index)
  5496. {
  5497. struct btrfs_fs_info *fs_info = root->fs_info;
  5498. struct inode *inode;
  5499. struct btrfs_inode_item *inode_item;
  5500. struct btrfs_key *location;
  5501. struct btrfs_path *path;
  5502. struct btrfs_inode_ref *ref;
  5503. struct btrfs_key key[2];
  5504. u32 sizes[2];
  5505. int nitems = name ? 2 : 1;
  5506. unsigned long ptr;
  5507. int ret;
  5508. path = btrfs_alloc_path();
  5509. if (!path)
  5510. return ERR_PTR(-ENOMEM);
  5511. inode = new_inode(fs_info->sb);
  5512. if (!inode) {
  5513. btrfs_free_path(path);
  5514. return ERR_PTR(-ENOMEM);
  5515. }
  5516. /*
  5517. * O_TMPFILE, set link count to 0, so that after this point,
  5518. * we fill in an inode item with the correct link count.
  5519. */
  5520. if (!name)
  5521. set_nlink(inode, 0);
  5522. /*
  5523. * we have to initialize this early, so we can reclaim the inode
  5524. * number if we fail afterwards in this function.
  5525. */
  5526. inode->i_ino = objectid;
  5527. if (dir && name) {
  5528. trace_btrfs_inode_request(dir);
  5529. ret = btrfs_set_inode_index(BTRFS_I(dir), index);
  5530. if (ret) {
  5531. btrfs_free_path(path);
  5532. iput(inode);
  5533. return ERR_PTR(ret);
  5534. }
  5535. } else if (dir) {
  5536. *index = 0;
  5537. }
  5538. /*
  5539. * index_cnt is ignored for everything but a dir,
  5540. * btrfs_set_inode_index_count has an explanation for the magic
  5541. * number
  5542. */
  5543. BTRFS_I(inode)->index_cnt = 2;
  5544. BTRFS_I(inode)->dir_index = *index;
  5545. BTRFS_I(inode)->root = root;
  5546. BTRFS_I(inode)->generation = trans->transid;
  5547. inode->i_generation = BTRFS_I(inode)->generation;
  5548. /*
  5549. * We could have gotten an inode number from somebody who was fsynced
  5550. * and then removed in this same transaction, so let's just set full
  5551. * sync since it will be a full sync anyway and this will blow away the
  5552. * old info in the log.
  5553. */
  5554. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
  5555. key[0].objectid = objectid;
  5556. key[0].type = BTRFS_INODE_ITEM_KEY;
  5557. key[0].offset = 0;
  5558. sizes[0] = sizeof(struct btrfs_inode_item);
  5559. if (name) {
  5560. /*
  5561. * Start new inodes with an inode_ref. This is slightly more
  5562. * efficient for small numbers of hard links since they will
  5563. * be packed into one item. Extended refs will kick in if we
  5564. * add more hard links than can fit in the ref item.
  5565. */
  5566. key[1].objectid = objectid;
  5567. key[1].type = BTRFS_INODE_REF_KEY;
  5568. key[1].offset = ref_objectid;
  5569. sizes[1] = name_len + sizeof(*ref);
  5570. }
  5571. location = &BTRFS_I(inode)->location;
  5572. location->objectid = objectid;
  5573. location->offset = 0;
  5574. location->type = BTRFS_INODE_ITEM_KEY;
  5575. ret = btrfs_insert_inode_locked(inode);
  5576. if (ret < 0)
  5577. goto fail;
  5578. path->leave_spinning = 1;
  5579. ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
  5580. if (ret != 0)
  5581. goto fail_unlock;
  5582. inode_init_owner(inode, dir, mode);
  5583. inode_set_bytes(inode, 0);
  5584. inode->i_mtime = current_time(inode);
  5585. inode->i_atime = inode->i_mtime;
  5586. inode->i_ctime = inode->i_mtime;
  5587. BTRFS_I(inode)->i_otime = inode->i_mtime;
  5588. inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  5589. struct btrfs_inode_item);
  5590. memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
  5591. sizeof(*inode_item));
  5592. fill_inode_item(trans, path->nodes[0], inode_item, inode);
  5593. if (name) {
  5594. ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
  5595. struct btrfs_inode_ref);
  5596. btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
  5597. btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
  5598. ptr = (unsigned long)(ref + 1);
  5599. write_extent_buffer(path->nodes[0], name, ptr, name_len);
  5600. }
  5601. btrfs_mark_buffer_dirty(path->nodes[0]);
  5602. btrfs_free_path(path);
  5603. btrfs_inherit_iflags(inode, dir);
  5604. if (S_ISREG(mode)) {
  5605. if (btrfs_test_opt(fs_info, NODATASUM))
  5606. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
  5607. if (btrfs_test_opt(fs_info, NODATACOW))
  5608. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
  5609. BTRFS_INODE_NODATASUM;
  5610. }
  5611. inode_tree_add(inode);
  5612. trace_btrfs_inode_new(inode);
  5613. btrfs_set_inode_last_trans(trans, inode);
  5614. btrfs_update_root_times(trans, root);
  5615. ret = btrfs_inode_inherit_props(trans, inode, dir);
  5616. if (ret)
  5617. btrfs_err(fs_info,
  5618. "error inheriting props for ino %llu (root %llu): %d",
  5619. btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
  5620. return inode;
  5621. fail_unlock:
  5622. unlock_new_inode(inode);
  5623. fail:
  5624. if (dir && name)
  5625. BTRFS_I(dir)->index_cnt--;
  5626. btrfs_free_path(path);
  5627. iput(inode);
  5628. return ERR_PTR(ret);
  5629. }
  5630. static inline u8 btrfs_inode_type(struct inode *inode)
  5631. {
  5632. return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
  5633. }
  5634. /*
  5635. * utility function to add 'inode' into 'parent_inode' with
  5636. * a give name and a given sequence number.
  5637. * if 'add_backref' is true, also insert a backref from the
  5638. * inode to the parent directory.
  5639. */
  5640. int btrfs_add_link(struct btrfs_trans_handle *trans,
  5641. struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
  5642. const char *name, int name_len, int add_backref, u64 index)
  5643. {
  5644. struct btrfs_fs_info *fs_info = trans->fs_info;
  5645. int ret = 0;
  5646. struct btrfs_key key;
  5647. struct btrfs_root *root = parent_inode->root;
  5648. u64 ino = btrfs_ino(inode);
  5649. u64 parent_ino = btrfs_ino(parent_inode);
  5650. if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
  5651. memcpy(&key, &inode->root->root_key, sizeof(key));
  5652. } else {
  5653. key.objectid = ino;
  5654. key.type = BTRFS_INODE_ITEM_KEY;
  5655. key.offset = 0;
  5656. }
  5657. if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
  5658. ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
  5659. root->root_key.objectid, parent_ino,
  5660. index, name, name_len);
  5661. } else if (add_backref) {
  5662. ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
  5663. parent_ino, index);
  5664. }
  5665. /* Nothing to clean up yet */
  5666. if (ret)
  5667. return ret;
  5668. ret = btrfs_insert_dir_item(trans, root, name, name_len,
  5669. parent_inode, &key,
  5670. btrfs_inode_type(&inode->vfs_inode), index);
  5671. if (ret == -EEXIST || ret == -EOVERFLOW)
  5672. goto fail_dir_item;
  5673. else if (ret) {
  5674. btrfs_abort_transaction(trans, ret);
  5675. return ret;
  5676. }
  5677. btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
  5678. name_len * 2);
  5679. inode_inc_iversion(&parent_inode->vfs_inode);
  5680. parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
  5681. current_time(&parent_inode->vfs_inode);
  5682. ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
  5683. if (ret)
  5684. btrfs_abort_transaction(trans, ret);
  5685. return ret;
  5686. fail_dir_item:
  5687. if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
  5688. u64 local_index;
  5689. int err;
  5690. err = btrfs_del_root_ref(trans, fs_info, key.objectid,
  5691. root->root_key.objectid, parent_ino,
  5692. &local_index, name, name_len);
  5693. } else if (add_backref) {
  5694. u64 local_index;
  5695. int err;
  5696. err = btrfs_del_inode_ref(trans, root, name, name_len,
  5697. ino, parent_ino, &local_index);
  5698. }
  5699. return ret;
  5700. }
  5701. static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
  5702. struct btrfs_inode *dir, struct dentry *dentry,
  5703. struct btrfs_inode *inode, int backref, u64 index)
  5704. {
  5705. int err = btrfs_add_link(trans, dir, inode,
  5706. dentry->d_name.name, dentry->d_name.len,
  5707. backref, index);
  5708. if (err > 0)
  5709. err = -EEXIST;
  5710. return err;
  5711. }
  5712. static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
  5713. umode_t mode, dev_t rdev)
  5714. {
  5715. struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
  5716. struct btrfs_trans_handle *trans;
  5717. struct btrfs_root *root = BTRFS_I(dir)->root;
  5718. struct inode *inode = NULL;
  5719. int err;
  5720. int drop_inode = 0;
  5721. u64 objectid;
  5722. u64 index = 0;
  5723. /*
  5724. * 2 for inode item and ref
  5725. * 2 for dir items
  5726. * 1 for xattr if selinux is on
  5727. */
  5728. trans = btrfs_start_transaction(root, 5);
  5729. if (IS_ERR(trans))
  5730. return PTR_ERR(trans);
  5731. err = btrfs_find_free_ino(root, &objectid);
  5732. if (err)
  5733. goto out_unlock;
  5734. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  5735. dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
  5736. mode, &index);
  5737. if (IS_ERR(inode)) {
  5738. err = PTR_ERR(inode);
  5739. goto out_unlock;
  5740. }
  5741. /*
  5742. * If the active LSM wants to access the inode during
  5743. * d_instantiate it needs these. Smack checks to see
  5744. * if the filesystem supports xattrs by looking at the
  5745. * ops vector.
  5746. */
  5747. inode->i_op = &btrfs_special_inode_operations;
  5748. init_special_inode(inode, inode->i_mode, rdev);
  5749. err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
  5750. if (err)
  5751. goto out_unlock_inode;
  5752. err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
  5753. 0, index);
  5754. if (err) {
  5755. goto out_unlock_inode;
  5756. } else {
  5757. btrfs_update_inode(trans, root, inode);
  5758. d_instantiate_new(dentry, inode);
  5759. }
  5760. out_unlock:
  5761. btrfs_end_transaction(trans);
  5762. btrfs_btree_balance_dirty(fs_info);
  5763. if (drop_inode) {
  5764. inode_dec_link_count(inode);
  5765. iput(inode);
  5766. }
  5767. return err;
  5768. out_unlock_inode:
  5769. drop_inode = 1;
  5770. unlock_new_inode(inode);
  5771. goto out_unlock;
  5772. }
  5773. static int btrfs_create(struct inode *dir, struct dentry *dentry,
  5774. umode_t mode, bool excl)
  5775. {
  5776. struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
  5777. struct btrfs_trans_handle *trans;
  5778. struct btrfs_root *root = BTRFS_I(dir)->root;
  5779. struct inode *inode = NULL;
  5780. int drop_inode_on_err = 0;
  5781. int err;
  5782. u64 objectid;
  5783. u64 index = 0;
  5784. /*
  5785. * 2 for inode item and ref
  5786. * 2 for dir items
  5787. * 1 for xattr if selinux is on
  5788. */
  5789. trans = btrfs_start_transaction(root, 5);
  5790. if (IS_ERR(trans))
  5791. return PTR_ERR(trans);
  5792. err = btrfs_find_free_ino(root, &objectid);
  5793. if (err)
  5794. goto out_unlock;
  5795. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  5796. dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
  5797. mode, &index);
  5798. if (IS_ERR(inode)) {
  5799. err = PTR_ERR(inode);
  5800. goto out_unlock;
  5801. }
  5802. drop_inode_on_err = 1;
  5803. /*
  5804. * If the active LSM wants to access the inode during
  5805. * d_instantiate it needs these. Smack checks to see
  5806. * if the filesystem supports xattrs by looking at the
  5807. * ops vector.
  5808. */
  5809. inode->i_fop = &btrfs_file_operations;
  5810. inode->i_op = &btrfs_file_inode_operations;
  5811. inode->i_mapping->a_ops = &btrfs_aops;
  5812. err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
  5813. if (err)
  5814. goto out_unlock_inode;
  5815. err = btrfs_update_inode(trans, root, inode);
  5816. if (err)
  5817. goto out_unlock_inode;
  5818. err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
  5819. 0, index);
  5820. if (err)
  5821. goto out_unlock_inode;
  5822. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  5823. d_instantiate_new(dentry, inode);
  5824. out_unlock:
  5825. btrfs_end_transaction(trans);
  5826. if (err && drop_inode_on_err) {
  5827. inode_dec_link_count(inode);
  5828. iput(inode);
  5829. }
  5830. btrfs_btree_balance_dirty(fs_info);
  5831. return err;
  5832. out_unlock_inode:
  5833. unlock_new_inode(inode);
  5834. goto out_unlock;
  5835. }
  5836. static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
  5837. struct dentry *dentry)
  5838. {
  5839. struct btrfs_trans_handle *trans = NULL;
  5840. struct btrfs_root *root = BTRFS_I(dir)->root;
  5841. struct inode *inode = d_inode(old_dentry);
  5842. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  5843. u64 index;
  5844. int err;
  5845. int drop_inode = 0;
  5846. /* do not allow sys_link's with other subvols of the same device */
  5847. if (root->objectid != BTRFS_I(inode)->root->objectid)
  5848. return -EXDEV;
  5849. if (inode->i_nlink >= BTRFS_LINK_MAX)
  5850. return -EMLINK;
  5851. err = btrfs_set_inode_index(BTRFS_I(dir), &index);
  5852. if (err)
  5853. goto fail;
  5854. /*
  5855. * 2 items for inode and inode ref
  5856. * 2 items for dir items
  5857. * 1 item for parent inode
  5858. * 1 item for orphan item deletion if O_TMPFILE
  5859. */
  5860. trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
  5861. if (IS_ERR(trans)) {
  5862. err = PTR_ERR(trans);
  5863. trans = NULL;
  5864. goto fail;
  5865. }
  5866. /* There are several dir indexes for this inode, clear the cache. */
  5867. BTRFS_I(inode)->dir_index = 0ULL;
  5868. inc_nlink(inode);
  5869. inode_inc_iversion(inode);
  5870. inode->i_ctime = current_time(inode);
  5871. ihold(inode);
  5872. set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
  5873. err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
  5874. 1, index);
  5875. if (err) {
  5876. drop_inode = 1;
  5877. } else {
  5878. struct dentry *parent = dentry->d_parent;
  5879. err = btrfs_update_inode(trans, root, inode);
  5880. if (err)
  5881. goto fail;
  5882. if (inode->i_nlink == 1) {
  5883. /*
  5884. * If new hard link count is 1, it's a file created
  5885. * with open(2) O_TMPFILE flag.
  5886. */
  5887. err = btrfs_orphan_del(trans, BTRFS_I(inode));
  5888. if (err)
  5889. goto fail;
  5890. }
  5891. d_instantiate(dentry, inode);
  5892. btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
  5893. }
  5894. fail:
  5895. if (trans)
  5896. btrfs_end_transaction(trans);
  5897. if (drop_inode) {
  5898. inode_dec_link_count(inode);
  5899. iput(inode);
  5900. }
  5901. btrfs_btree_balance_dirty(fs_info);
  5902. return err;
  5903. }
  5904. static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  5905. {
  5906. struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
  5907. struct inode *inode = NULL;
  5908. struct btrfs_trans_handle *trans;
  5909. struct btrfs_root *root = BTRFS_I(dir)->root;
  5910. int err = 0;
  5911. int drop_on_err = 0;
  5912. u64 objectid = 0;
  5913. u64 index = 0;
  5914. /*
  5915. * 2 items for inode and ref
  5916. * 2 items for dir items
  5917. * 1 for xattr if selinux is on
  5918. */
  5919. trans = btrfs_start_transaction(root, 5);
  5920. if (IS_ERR(trans))
  5921. return PTR_ERR(trans);
  5922. err = btrfs_find_free_ino(root, &objectid);
  5923. if (err)
  5924. goto out_fail;
  5925. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  5926. dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
  5927. S_IFDIR | mode, &index);
  5928. if (IS_ERR(inode)) {
  5929. err = PTR_ERR(inode);
  5930. goto out_fail;
  5931. }
  5932. drop_on_err = 1;
  5933. /* these must be set before we unlock the inode */
  5934. inode->i_op = &btrfs_dir_inode_operations;
  5935. inode->i_fop = &btrfs_dir_file_operations;
  5936. err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
  5937. if (err)
  5938. goto out_fail_inode;
  5939. btrfs_i_size_write(BTRFS_I(inode), 0);
  5940. err = btrfs_update_inode(trans, root, inode);
  5941. if (err)
  5942. goto out_fail_inode;
  5943. err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
  5944. dentry->d_name.name,
  5945. dentry->d_name.len, 0, index);
  5946. if (err)
  5947. goto out_fail_inode;
  5948. d_instantiate_new(dentry, inode);
  5949. drop_on_err = 0;
  5950. out_fail:
  5951. btrfs_end_transaction(trans);
  5952. if (drop_on_err) {
  5953. inode_dec_link_count(inode);
  5954. iput(inode);
  5955. }
  5956. btrfs_btree_balance_dirty(fs_info);
  5957. return err;
  5958. out_fail_inode:
  5959. unlock_new_inode(inode);
  5960. goto out_fail;
  5961. }
  5962. static noinline int uncompress_inline(struct btrfs_path *path,
  5963. struct page *page,
  5964. size_t pg_offset, u64 extent_offset,
  5965. struct btrfs_file_extent_item *item)
  5966. {
  5967. int ret;
  5968. struct extent_buffer *leaf = path->nodes[0];
  5969. char *tmp;
  5970. size_t max_size;
  5971. unsigned long inline_size;
  5972. unsigned long ptr;
  5973. int compress_type;
  5974. WARN_ON(pg_offset != 0);
  5975. compress_type = btrfs_file_extent_compression(leaf, item);
  5976. max_size = btrfs_file_extent_ram_bytes(leaf, item);
  5977. inline_size = btrfs_file_extent_inline_item_len(leaf,
  5978. btrfs_item_nr(path->slots[0]));
  5979. tmp = kmalloc(inline_size, GFP_NOFS);
  5980. if (!tmp)
  5981. return -ENOMEM;
  5982. ptr = btrfs_file_extent_inline_start(item);
  5983. read_extent_buffer(leaf, tmp, ptr, inline_size);
  5984. max_size = min_t(unsigned long, PAGE_SIZE, max_size);
  5985. ret = btrfs_decompress(compress_type, tmp, page,
  5986. extent_offset, inline_size, max_size);
  5987. /*
  5988. * decompression code contains a memset to fill in any space between the end
  5989. * of the uncompressed data and the end of max_size in case the decompressed
  5990. * data ends up shorter than ram_bytes. That doesn't cover the hole between
  5991. * the end of an inline extent and the beginning of the next block, so we
  5992. * cover that region here.
  5993. */
  5994. if (max_size + pg_offset < PAGE_SIZE) {
  5995. char *map = kmap(page);
  5996. memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
  5997. kunmap(page);
  5998. }
  5999. kfree(tmp);
  6000. return ret;
  6001. }
  6002. /*
  6003. * a bit scary, this does extent mapping from logical file offset to the disk.
  6004. * the ugly parts come from merging extents from the disk with the in-ram
  6005. * representation. This gets more complex because of the data=ordered code,
  6006. * where the in-ram extents might be locked pending data=ordered completion.
  6007. *
  6008. * This also copies inline extents directly into the page.
  6009. */
  6010. struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
  6011. struct page *page,
  6012. size_t pg_offset, u64 start, u64 len,
  6013. int create)
  6014. {
  6015. struct btrfs_fs_info *fs_info = inode->root->fs_info;
  6016. int ret;
  6017. int err = 0;
  6018. u64 extent_start = 0;
  6019. u64 extent_end = 0;
  6020. u64 objectid = btrfs_ino(inode);
  6021. u32 found_type;
  6022. struct btrfs_path *path = NULL;
  6023. struct btrfs_root *root = inode->root;
  6024. struct btrfs_file_extent_item *item;
  6025. struct extent_buffer *leaf;
  6026. struct btrfs_key found_key;
  6027. struct extent_map *em = NULL;
  6028. struct extent_map_tree *em_tree = &inode->extent_tree;
  6029. struct extent_io_tree *io_tree = &inode->io_tree;
  6030. const bool new_inline = !page || create;
  6031. read_lock(&em_tree->lock);
  6032. em = lookup_extent_mapping(em_tree, start, len);
  6033. if (em)
  6034. em->bdev = fs_info->fs_devices->latest_bdev;
  6035. read_unlock(&em_tree->lock);
  6036. if (em) {
  6037. if (em->start > start || em->start + em->len <= start)
  6038. free_extent_map(em);
  6039. else if (em->block_start == EXTENT_MAP_INLINE && page)
  6040. free_extent_map(em);
  6041. else
  6042. goto out;
  6043. }
  6044. em = alloc_extent_map();
  6045. if (!em) {
  6046. err = -ENOMEM;
  6047. goto out;
  6048. }
  6049. em->bdev = fs_info->fs_devices->latest_bdev;
  6050. em->start = EXTENT_MAP_HOLE;
  6051. em->orig_start = EXTENT_MAP_HOLE;
  6052. em->len = (u64)-1;
  6053. em->block_len = (u64)-1;
  6054. if (!path) {
  6055. path = btrfs_alloc_path();
  6056. if (!path) {
  6057. err = -ENOMEM;
  6058. goto out;
  6059. }
  6060. /*
  6061. * Chances are we'll be called again, so go ahead and do
  6062. * readahead
  6063. */
  6064. path->reada = READA_FORWARD;
  6065. }
  6066. ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
  6067. if (ret < 0) {
  6068. err = ret;
  6069. goto out;
  6070. }
  6071. if (ret != 0) {
  6072. if (path->slots[0] == 0)
  6073. goto not_found;
  6074. path->slots[0]--;
  6075. }
  6076. leaf = path->nodes[0];
  6077. item = btrfs_item_ptr(leaf, path->slots[0],
  6078. struct btrfs_file_extent_item);
  6079. /* are we inside the extent that was found? */
  6080. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  6081. found_type = found_key.type;
  6082. if (found_key.objectid != objectid ||
  6083. found_type != BTRFS_EXTENT_DATA_KEY) {
  6084. /*
  6085. * If we backup past the first extent we want to move forward
  6086. * and see if there is an extent in front of us, otherwise we'll
  6087. * say there is a hole for our whole search range which can
  6088. * cause problems.
  6089. */
  6090. extent_end = start;
  6091. goto next;
  6092. }
  6093. found_type = btrfs_file_extent_type(leaf, item);
  6094. extent_start = found_key.offset;
  6095. if (found_type == BTRFS_FILE_EXTENT_REG ||
  6096. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  6097. extent_end = extent_start +
  6098. btrfs_file_extent_num_bytes(leaf, item);
  6099. trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
  6100. extent_start);
  6101. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  6102. size_t size;
  6103. size = btrfs_file_extent_ram_bytes(leaf, item);
  6104. extent_end = ALIGN(extent_start + size,
  6105. fs_info->sectorsize);
  6106. trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
  6107. path->slots[0],
  6108. extent_start);
  6109. }
  6110. next:
  6111. if (start >= extent_end) {
  6112. path->slots[0]++;
  6113. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  6114. ret = btrfs_next_leaf(root, path);
  6115. if (ret < 0) {
  6116. err = ret;
  6117. goto out;
  6118. }
  6119. if (ret > 0)
  6120. goto not_found;
  6121. leaf = path->nodes[0];
  6122. }
  6123. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  6124. if (found_key.objectid != objectid ||
  6125. found_key.type != BTRFS_EXTENT_DATA_KEY)
  6126. goto not_found;
  6127. if (start + len <= found_key.offset)
  6128. goto not_found;
  6129. if (start > found_key.offset)
  6130. goto next;
  6131. em->start = start;
  6132. em->orig_start = start;
  6133. em->len = found_key.offset - start;
  6134. goto not_found_em;
  6135. }
  6136. btrfs_extent_item_to_extent_map(inode, path, item,
  6137. new_inline, em);
  6138. if (found_type == BTRFS_FILE_EXTENT_REG ||
  6139. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  6140. goto insert;
  6141. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  6142. unsigned long ptr;
  6143. char *map;
  6144. size_t size;
  6145. size_t extent_offset;
  6146. size_t copy_size;
  6147. if (new_inline)
  6148. goto out;
  6149. size = btrfs_file_extent_ram_bytes(leaf, item);
  6150. extent_offset = page_offset(page) + pg_offset - extent_start;
  6151. copy_size = min_t(u64, PAGE_SIZE - pg_offset,
  6152. size - extent_offset);
  6153. em->start = extent_start + extent_offset;
  6154. em->len = ALIGN(copy_size, fs_info->sectorsize);
  6155. em->orig_block_len = em->len;
  6156. em->orig_start = em->start;
  6157. ptr = btrfs_file_extent_inline_start(item) + extent_offset;
  6158. if (!PageUptodate(page)) {
  6159. if (btrfs_file_extent_compression(leaf, item) !=
  6160. BTRFS_COMPRESS_NONE) {
  6161. ret = uncompress_inline(path, page, pg_offset,
  6162. extent_offset, item);
  6163. if (ret) {
  6164. err = ret;
  6165. goto out;
  6166. }
  6167. } else {
  6168. map = kmap(page);
  6169. read_extent_buffer(leaf, map + pg_offset, ptr,
  6170. copy_size);
  6171. if (pg_offset + copy_size < PAGE_SIZE) {
  6172. memset(map + pg_offset + copy_size, 0,
  6173. PAGE_SIZE - pg_offset -
  6174. copy_size);
  6175. }
  6176. kunmap(page);
  6177. }
  6178. flush_dcache_page(page);
  6179. }
  6180. set_extent_uptodate(io_tree, em->start,
  6181. extent_map_end(em) - 1, NULL, GFP_NOFS);
  6182. goto insert;
  6183. }
  6184. not_found:
  6185. em->start = start;
  6186. em->orig_start = start;
  6187. em->len = len;
  6188. not_found_em:
  6189. em->block_start = EXTENT_MAP_HOLE;
  6190. insert:
  6191. btrfs_release_path(path);
  6192. if (em->start > start || extent_map_end(em) <= start) {
  6193. btrfs_err(fs_info,
  6194. "bad extent! em: [%llu %llu] passed [%llu %llu]",
  6195. em->start, em->len, start, len);
  6196. err = -EIO;
  6197. goto out;
  6198. }
  6199. err = 0;
  6200. write_lock(&em_tree->lock);
  6201. err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
  6202. write_unlock(&em_tree->lock);
  6203. out:
  6204. trace_btrfs_get_extent(root, inode, em);
  6205. btrfs_free_path(path);
  6206. if (err) {
  6207. free_extent_map(em);
  6208. return ERR_PTR(err);
  6209. }
  6210. BUG_ON(!em); /* Error is always set */
  6211. return em;
  6212. }
  6213. struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
  6214. struct page *page,
  6215. size_t pg_offset, u64 start, u64 len,
  6216. int create)
  6217. {
  6218. struct extent_map *em;
  6219. struct extent_map *hole_em = NULL;
  6220. u64 range_start = start;
  6221. u64 end;
  6222. u64 found;
  6223. u64 found_end;
  6224. int err = 0;
  6225. em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
  6226. if (IS_ERR(em))
  6227. return em;
  6228. /*
  6229. * If our em maps to:
  6230. * - a hole or
  6231. * - a pre-alloc extent,
  6232. * there might actually be delalloc bytes behind it.
  6233. */
  6234. if (em->block_start != EXTENT_MAP_HOLE &&
  6235. !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  6236. return em;
  6237. else
  6238. hole_em = em;
  6239. /* check to see if we've wrapped (len == -1 or similar) */
  6240. end = start + len;
  6241. if (end < start)
  6242. end = (u64)-1;
  6243. else
  6244. end -= 1;
  6245. em = NULL;
  6246. /* ok, we didn't find anything, lets look for delalloc */
  6247. found = count_range_bits(&inode->io_tree, &range_start,
  6248. end, len, EXTENT_DELALLOC, 1);
  6249. found_end = range_start + found;
  6250. if (found_end < range_start)
  6251. found_end = (u64)-1;
  6252. /*
  6253. * we didn't find anything useful, return
  6254. * the original results from get_extent()
  6255. */
  6256. if (range_start > end || found_end <= start) {
  6257. em = hole_em;
  6258. hole_em = NULL;
  6259. goto out;
  6260. }
  6261. /* adjust the range_start to make sure it doesn't
  6262. * go backwards from the start they passed in
  6263. */
  6264. range_start = max(start, range_start);
  6265. found = found_end - range_start;
  6266. if (found > 0) {
  6267. u64 hole_start = start;
  6268. u64 hole_len = len;
  6269. em = alloc_extent_map();
  6270. if (!em) {
  6271. err = -ENOMEM;
  6272. goto out;
  6273. }
  6274. /*
  6275. * when btrfs_get_extent can't find anything it
  6276. * returns one huge hole
  6277. *
  6278. * make sure what it found really fits our range, and
  6279. * adjust to make sure it is based on the start from
  6280. * the caller
  6281. */
  6282. if (hole_em) {
  6283. u64 calc_end = extent_map_end(hole_em);
  6284. if (calc_end <= start || (hole_em->start > end)) {
  6285. free_extent_map(hole_em);
  6286. hole_em = NULL;
  6287. } else {
  6288. hole_start = max(hole_em->start, start);
  6289. hole_len = calc_end - hole_start;
  6290. }
  6291. }
  6292. em->bdev = NULL;
  6293. if (hole_em && range_start > hole_start) {
  6294. /* our hole starts before our delalloc, so we
  6295. * have to return just the parts of the hole
  6296. * that go until the delalloc starts
  6297. */
  6298. em->len = min(hole_len,
  6299. range_start - hole_start);
  6300. em->start = hole_start;
  6301. em->orig_start = hole_start;
  6302. /*
  6303. * don't adjust block start at all,
  6304. * it is fixed at EXTENT_MAP_HOLE
  6305. */
  6306. em->block_start = hole_em->block_start;
  6307. em->block_len = hole_len;
  6308. if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
  6309. set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
  6310. } else {
  6311. em->start = range_start;
  6312. em->len = found;
  6313. em->orig_start = range_start;
  6314. em->block_start = EXTENT_MAP_DELALLOC;
  6315. em->block_len = found;
  6316. }
  6317. } else {
  6318. return hole_em;
  6319. }
  6320. out:
  6321. free_extent_map(hole_em);
  6322. if (err) {
  6323. free_extent_map(em);
  6324. return ERR_PTR(err);
  6325. }
  6326. return em;
  6327. }
  6328. static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
  6329. const u64 start,
  6330. const u64 len,
  6331. const u64 orig_start,
  6332. const u64 block_start,
  6333. const u64 block_len,
  6334. const u64 orig_block_len,
  6335. const u64 ram_bytes,
  6336. const int type)
  6337. {
  6338. struct extent_map *em = NULL;
  6339. int ret;
  6340. if (type != BTRFS_ORDERED_NOCOW) {
  6341. em = create_io_em(inode, start, len, orig_start,
  6342. block_start, block_len, orig_block_len,
  6343. ram_bytes,
  6344. BTRFS_COMPRESS_NONE, /* compress_type */
  6345. type);
  6346. if (IS_ERR(em))
  6347. goto out;
  6348. }
  6349. ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
  6350. len, block_len, type);
  6351. if (ret) {
  6352. if (em) {
  6353. free_extent_map(em);
  6354. btrfs_drop_extent_cache(BTRFS_I(inode), start,
  6355. start + len - 1, 0);
  6356. }
  6357. em = ERR_PTR(ret);
  6358. }
  6359. out:
  6360. return em;
  6361. }
  6362. static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
  6363. u64 start, u64 len)
  6364. {
  6365. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  6366. struct btrfs_root *root = BTRFS_I(inode)->root;
  6367. struct extent_map *em;
  6368. struct btrfs_key ins;
  6369. u64 alloc_hint;
  6370. int ret;
  6371. alloc_hint = get_extent_allocation_hint(inode, start, len);
  6372. ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
  6373. 0, alloc_hint, &ins, 1, 1);
  6374. if (ret)
  6375. return ERR_PTR(ret);
  6376. em = btrfs_create_dio_extent(inode, start, ins.offset, start,
  6377. ins.objectid, ins.offset, ins.offset,
  6378. ins.offset, BTRFS_ORDERED_REGULAR);
  6379. btrfs_dec_block_group_reservations(fs_info, ins.objectid);
  6380. if (IS_ERR(em))
  6381. btrfs_free_reserved_extent(fs_info, ins.objectid,
  6382. ins.offset, 1);
  6383. return em;
  6384. }
  6385. /*
  6386. * returns 1 when the nocow is safe, < 1 on error, 0 if the
  6387. * block must be cow'd
  6388. */
  6389. noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
  6390. u64 *orig_start, u64 *orig_block_len,
  6391. u64 *ram_bytes)
  6392. {
  6393. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  6394. struct btrfs_path *path;
  6395. int ret;
  6396. struct extent_buffer *leaf;
  6397. struct btrfs_root *root = BTRFS_I(inode)->root;
  6398. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  6399. struct btrfs_file_extent_item *fi;
  6400. struct btrfs_key key;
  6401. u64 disk_bytenr;
  6402. u64 backref_offset;
  6403. u64 extent_end;
  6404. u64 num_bytes;
  6405. int slot;
  6406. int found_type;
  6407. bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
  6408. path = btrfs_alloc_path();
  6409. if (!path)
  6410. return -ENOMEM;
  6411. ret = btrfs_lookup_file_extent(NULL, root, path,
  6412. btrfs_ino(BTRFS_I(inode)), offset, 0);
  6413. if (ret < 0)
  6414. goto out;
  6415. slot = path->slots[0];
  6416. if (ret == 1) {
  6417. if (slot == 0) {
  6418. /* can't find the item, must cow */
  6419. ret = 0;
  6420. goto out;
  6421. }
  6422. slot--;
  6423. }
  6424. ret = 0;
  6425. leaf = path->nodes[0];
  6426. btrfs_item_key_to_cpu(leaf, &key, slot);
  6427. if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
  6428. key.type != BTRFS_EXTENT_DATA_KEY) {
  6429. /* not our file or wrong item type, must cow */
  6430. goto out;
  6431. }
  6432. if (key.offset > offset) {
  6433. /* Wrong offset, must cow */
  6434. goto out;
  6435. }
  6436. fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  6437. found_type = btrfs_file_extent_type(leaf, fi);
  6438. if (found_type != BTRFS_FILE_EXTENT_REG &&
  6439. found_type != BTRFS_FILE_EXTENT_PREALLOC) {
  6440. /* not a regular extent, must cow */
  6441. goto out;
  6442. }
  6443. if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
  6444. goto out;
  6445. extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
  6446. if (extent_end <= offset)
  6447. goto out;
  6448. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  6449. if (disk_bytenr == 0)
  6450. goto out;
  6451. if (btrfs_file_extent_compression(leaf, fi) ||
  6452. btrfs_file_extent_encryption(leaf, fi) ||
  6453. btrfs_file_extent_other_encoding(leaf, fi))
  6454. goto out;
  6455. /*
  6456. * Do the same check as in btrfs_cross_ref_exist but without the
  6457. * unnecessary search.
  6458. */
  6459. if (btrfs_file_extent_generation(leaf, fi) <=
  6460. btrfs_root_last_snapshot(&root->root_item))
  6461. goto out;
  6462. backref_offset = btrfs_file_extent_offset(leaf, fi);
  6463. if (orig_start) {
  6464. *orig_start = key.offset - backref_offset;
  6465. *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
  6466. *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
  6467. }
  6468. if (btrfs_extent_readonly(fs_info, disk_bytenr))
  6469. goto out;
  6470. num_bytes = min(offset + *len, extent_end) - offset;
  6471. if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  6472. u64 range_end;
  6473. range_end = round_up(offset + num_bytes,
  6474. root->fs_info->sectorsize) - 1;
  6475. ret = test_range_bit(io_tree, offset, range_end,
  6476. EXTENT_DELALLOC, 0, NULL);
  6477. if (ret) {
  6478. ret = -EAGAIN;
  6479. goto out;
  6480. }
  6481. }
  6482. btrfs_release_path(path);
  6483. /*
  6484. * look for other files referencing this extent, if we
  6485. * find any we must cow
  6486. */
  6487. ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
  6488. key.offset - backref_offset, disk_bytenr);
  6489. if (ret) {
  6490. ret = 0;
  6491. goto out;
  6492. }
  6493. /*
  6494. * adjust disk_bytenr and num_bytes to cover just the bytes
  6495. * in this extent we are about to write. If there
  6496. * are any csums in that range we have to cow in order
  6497. * to keep the csums correct
  6498. */
  6499. disk_bytenr += backref_offset;
  6500. disk_bytenr += offset - key.offset;
  6501. if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
  6502. goto out;
  6503. /*
  6504. * all of the above have passed, it is safe to overwrite this extent
  6505. * without cow
  6506. */
  6507. *len = num_bytes;
  6508. ret = 1;
  6509. out:
  6510. btrfs_free_path(path);
  6511. return ret;
  6512. }
  6513. static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
  6514. struct extent_state **cached_state, int writing)
  6515. {
  6516. struct btrfs_ordered_extent *ordered;
  6517. int ret = 0;
  6518. while (1) {
  6519. lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  6520. cached_state);
  6521. /*
  6522. * We're concerned with the entire range that we're going to be
  6523. * doing DIO to, so we need to make sure there's no ordered
  6524. * extents in this range.
  6525. */
  6526. ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
  6527. lockend - lockstart + 1);
  6528. /*
  6529. * We need to make sure there are no buffered pages in this
  6530. * range either, we could have raced between the invalidate in
  6531. * generic_file_direct_write and locking the extent. The
  6532. * invalidate needs to happen so that reads after a write do not
  6533. * get stale data.
  6534. */
  6535. if (!ordered &&
  6536. (!writing || !filemap_range_has_page(inode->i_mapping,
  6537. lockstart, lockend)))
  6538. break;
  6539. unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  6540. cached_state);
  6541. if (ordered) {
  6542. /*
  6543. * If we are doing a DIO read and the ordered extent we
  6544. * found is for a buffered write, we can not wait for it
  6545. * to complete and retry, because if we do so we can
  6546. * deadlock with concurrent buffered writes on page
  6547. * locks. This happens only if our DIO read covers more
  6548. * than one extent map, if at this point has already
  6549. * created an ordered extent for a previous extent map
  6550. * and locked its range in the inode's io tree, and a
  6551. * concurrent write against that previous extent map's
  6552. * range and this range started (we unlock the ranges
  6553. * in the io tree only when the bios complete and
  6554. * buffered writes always lock pages before attempting
  6555. * to lock range in the io tree).
  6556. */
  6557. if (writing ||
  6558. test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
  6559. btrfs_start_ordered_extent(inode, ordered, 1);
  6560. else
  6561. ret = -ENOTBLK;
  6562. btrfs_put_ordered_extent(ordered);
  6563. } else {
  6564. /*
  6565. * We could trigger writeback for this range (and wait
  6566. * for it to complete) and then invalidate the pages for
  6567. * this range (through invalidate_inode_pages2_range()),
  6568. * but that can lead us to a deadlock with a concurrent
  6569. * call to readpages() (a buffered read or a defrag call
  6570. * triggered a readahead) on a page lock due to an
  6571. * ordered dio extent we created before but did not have
  6572. * yet a corresponding bio submitted (whence it can not
  6573. * complete), which makes readpages() wait for that
  6574. * ordered extent to complete while holding a lock on
  6575. * that page.
  6576. */
  6577. ret = -ENOTBLK;
  6578. }
  6579. if (ret)
  6580. break;
  6581. cond_resched();
  6582. }
  6583. return ret;
  6584. }
  6585. /* The callers of this must take lock_extent() */
  6586. static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
  6587. u64 orig_start, u64 block_start,
  6588. u64 block_len, u64 orig_block_len,
  6589. u64 ram_bytes, int compress_type,
  6590. int type)
  6591. {
  6592. struct extent_map_tree *em_tree;
  6593. struct extent_map *em;
  6594. struct btrfs_root *root = BTRFS_I(inode)->root;
  6595. int ret;
  6596. ASSERT(type == BTRFS_ORDERED_PREALLOC ||
  6597. type == BTRFS_ORDERED_COMPRESSED ||
  6598. type == BTRFS_ORDERED_NOCOW ||
  6599. type == BTRFS_ORDERED_REGULAR);
  6600. em_tree = &BTRFS_I(inode)->extent_tree;
  6601. em = alloc_extent_map();
  6602. if (!em)
  6603. return ERR_PTR(-ENOMEM);
  6604. em->start = start;
  6605. em->orig_start = orig_start;
  6606. em->len = len;
  6607. em->block_len = block_len;
  6608. em->block_start = block_start;
  6609. em->bdev = root->fs_info->fs_devices->latest_bdev;
  6610. em->orig_block_len = orig_block_len;
  6611. em->ram_bytes = ram_bytes;
  6612. em->generation = -1;
  6613. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  6614. if (type == BTRFS_ORDERED_PREALLOC) {
  6615. set_bit(EXTENT_FLAG_FILLING, &em->flags);
  6616. } else if (type == BTRFS_ORDERED_COMPRESSED) {
  6617. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  6618. em->compress_type = compress_type;
  6619. }
  6620. do {
  6621. btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
  6622. em->start + em->len - 1, 0);
  6623. write_lock(&em_tree->lock);
  6624. ret = add_extent_mapping(em_tree, em, 1);
  6625. write_unlock(&em_tree->lock);
  6626. /*
  6627. * The caller has taken lock_extent(), who could race with us
  6628. * to add em?
  6629. */
  6630. } while (ret == -EEXIST);
  6631. if (ret) {
  6632. free_extent_map(em);
  6633. return ERR_PTR(ret);
  6634. }
  6635. /* em got 2 refs now, callers needs to do free_extent_map once. */
  6636. return em;
  6637. }
  6638. static int btrfs_get_blocks_direct_read(struct extent_map *em,
  6639. struct buffer_head *bh_result,
  6640. struct inode *inode,
  6641. u64 start, u64 len)
  6642. {
  6643. if (em->block_start == EXTENT_MAP_HOLE ||
  6644. test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  6645. return -ENOENT;
  6646. len = min(len, em->len - (start - em->start));
  6647. bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
  6648. inode->i_blkbits;
  6649. bh_result->b_size = len;
  6650. bh_result->b_bdev = em->bdev;
  6651. set_buffer_mapped(bh_result);
  6652. return 0;
  6653. }
  6654. static int btrfs_get_blocks_direct_write(struct extent_map **map,
  6655. struct buffer_head *bh_result,
  6656. struct inode *inode,
  6657. struct btrfs_dio_data *dio_data,
  6658. u64 start, u64 len)
  6659. {
  6660. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  6661. struct extent_map *em = *map;
  6662. int ret = 0;
  6663. /*
  6664. * We don't allocate a new extent in the following cases
  6665. *
  6666. * 1) The inode is marked as NODATACOW. In this case we'll just use the
  6667. * existing extent.
  6668. * 2) The extent is marked as PREALLOC. We're good to go here and can
  6669. * just use the extent.
  6670. *
  6671. */
  6672. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
  6673. ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
  6674. em->block_start != EXTENT_MAP_HOLE)) {
  6675. int type;
  6676. u64 block_start, orig_start, orig_block_len, ram_bytes;
  6677. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  6678. type = BTRFS_ORDERED_PREALLOC;
  6679. else
  6680. type = BTRFS_ORDERED_NOCOW;
  6681. len = min(len, em->len - (start - em->start));
  6682. block_start = em->block_start + (start - em->start);
  6683. if (can_nocow_extent(inode, start, &len, &orig_start,
  6684. &orig_block_len, &ram_bytes) == 1 &&
  6685. btrfs_inc_nocow_writers(fs_info, block_start)) {
  6686. struct extent_map *em2;
  6687. em2 = btrfs_create_dio_extent(inode, start, len,
  6688. orig_start, block_start,
  6689. len, orig_block_len,
  6690. ram_bytes, type);
  6691. btrfs_dec_nocow_writers(fs_info, block_start);
  6692. if (type == BTRFS_ORDERED_PREALLOC) {
  6693. free_extent_map(em);
  6694. *map = em = em2;
  6695. }
  6696. if (em2 && IS_ERR(em2)) {
  6697. ret = PTR_ERR(em2);
  6698. goto out;
  6699. }
  6700. /*
  6701. * For inode marked NODATACOW or extent marked PREALLOC,
  6702. * use the existing or preallocated extent, so does not
  6703. * need to adjust btrfs_space_info's bytes_may_use.
  6704. */
  6705. btrfs_free_reserved_data_space_noquota(inode, start,
  6706. len);
  6707. goto skip_cow;
  6708. }
  6709. }
  6710. /* this will cow the extent */
  6711. len = bh_result->b_size;
  6712. free_extent_map(em);
  6713. *map = em = btrfs_new_extent_direct(inode, start, len);
  6714. if (IS_ERR(em)) {
  6715. ret = PTR_ERR(em);
  6716. goto out;
  6717. }
  6718. len = min(len, em->len - (start - em->start));
  6719. skip_cow:
  6720. bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
  6721. inode->i_blkbits;
  6722. bh_result->b_size = len;
  6723. bh_result->b_bdev = em->bdev;
  6724. set_buffer_mapped(bh_result);
  6725. if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  6726. set_buffer_new(bh_result);
  6727. /*
  6728. * Need to update the i_size under the extent lock so buffered
  6729. * readers will get the updated i_size when we unlock.
  6730. */
  6731. if (!dio_data->overwrite && start + len > i_size_read(inode))
  6732. i_size_write(inode, start + len);
  6733. WARN_ON(dio_data->reserve < len);
  6734. dio_data->reserve -= len;
  6735. dio_data->unsubmitted_oe_range_end = start + len;
  6736. current->journal_info = dio_data;
  6737. out:
  6738. return ret;
  6739. }
  6740. static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
  6741. struct buffer_head *bh_result, int create)
  6742. {
  6743. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  6744. struct extent_map *em;
  6745. struct extent_state *cached_state = NULL;
  6746. struct btrfs_dio_data *dio_data = NULL;
  6747. u64 start = iblock << inode->i_blkbits;
  6748. u64 lockstart, lockend;
  6749. u64 len = bh_result->b_size;
  6750. int unlock_bits = EXTENT_LOCKED;
  6751. int ret = 0;
  6752. if (create)
  6753. unlock_bits |= EXTENT_DIRTY;
  6754. else
  6755. len = min_t(u64, len, fs_info->sectorsize);
  6756. lockstart = start;
  6757. lockend = start + len - 1;
  6758. if (current->journal_info) {
  6759. /*
  6760. * Need to pull our outstanding extents and set journal_info to NULL so
  6761. * that anything that needs to check if there's a transaction doesn't get
  6762. * confused.
  6763. */
  6764. dio_data = current->journal_info;
  6765. current->journal_info = NULL;
  6766. }
  6767. /*
  6768. * If this errors out it's because we couldn't invalidate pagecache for
  6769. * this range and we need to fallback to buffered.
  6770. */
  6771. if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
  6772. create)) {
  6773. ret = -ENOTBLK;
  6774. goto err;
  6775. }
  6776. em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
  6777. if (IS_ERR(em)) {
  6778. ret = PTR_ERR(em);
  6779. goto unlock_err;
  6780. }
  6781. /*
  6782. * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
  6783. * io. INLINE is special, and we could probably kludge it in here, but
  6784. * it's still buffered so for safety lets just fall back to the generic
  6785. * buffered path.
  6786. *
  6787. * For COMPRESSED we _have_ to read the entire extent in so we can
  6788. * decompress it, so there will be buffering required no matter what we
  6789. * do, so go ahead and fallback to buffered.
  6790. *
  6791. * We return -ENOTBLK because that's what makes DIO go ahead and go back
  6792. * to buffered IO. Don't blame me, this is the price we pay for using
  6793. * the generic code.
  6794. */
  6795. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
  6796. em->block_start == EXTENT_MAP_INLINE) {
  6797. free_extent_map(em);
  6798. ret = -ENOTBLK;
  6799. goto unlock_err;
  6800. }
  6801. if (create) {
  6802. ret = btrfs_get_blocks_direct_write(&em, bh_result, inode,
  6803. dio_data, start, len);
  6804. if (ret < 0)
  6805. goto unlock_err;
  6806. /* clear and unlock the entire range */
  6807. clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  6808. unlock_bits, 1, 0, &cached_state);
  6809. } else {
  6810. ret = btrfs_get_blocks_direct_read(em, bh_result, inode,
  6811. start, len);
  6812. /* Can be negative only if we read from a hole */
  6813. if (ret < 0) {
  6814. ret = 0;
  6815. free_extent_map(em);
  6816. goto unlock_err;
  6817. }
  6818. /*
  6819. * We need to unlock only the end area that we aren't using.
  6820. * The rest is going to be unlocked by the endio routine.
  6821. */
  6822. lockstart = start + bh_result->b_size;
  6823. if (lockstart < lockend) {
  6824. clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
  6825. lockend, unlock_bits, 1, 0,
  6826. &cached_state);
  6827. } else {
  6828. free_extent_state(cached_state);
  6829. }
  6830. }
  6831. free_extent_map(em);
  6832. return 0;
  6833. unlock_err:
  6834. clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  6835. unlock_bits, 1, 0, &cached_state);
  6836. err:
  6837. if (dio_data)
  6838. current->journal_info = dio_data;
  6839. return ret;
  6840. }
  6841. static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
  6842. struct bio *bio,
  6843. int mirror_num)
  6844. {
  6845. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  6846. blk_status_t ret;
  6847. BUG_ON(bio_op(bio) == REQ_OP_WRITE);
  6848. ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
  6849. if (ret)
  6850. return ret;
  6851. ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
  6852. return ret;
  6853. }
  6854. static int btrfs_check_dio_repairable(struct inode *inode,
  6855. struct bio *failed_bio,
  6856. struct io_failure_record *failrec,
  6857. int failed_mirror)
  6858. {
  6859. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  6860. int num_copies;
  6861. num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
  6862. if (num_copies == 1) {
  6863. /*
  6864. * we only have a single copy of the data, so don't bother with
  6865. * all the retry and error correction code that follows. no
  6866. * matter what the error is, it is very likely to persist.
  6867. */
  6868. btrfs_debug(fs_info,
  6869. "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
  6870. num_copies, failrec->this_mirror, failed_mirror);
  6871. return 0;
  6872. }
  6873. failrec->failed_mirror = failed_mirror;
  6874. failrec->this_mirror++;
  6875. if (failrec->this_mirror == failed_mirror)
  6876. failrec->this_mirror++;
  6877. if (failrec->this_mirror > num_copies) {
  6878. btrfs_debug(fs_info,
  6879. "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
  6880. num_copies, failrec->this_mirror, failed_mirror);
  6881. return 0;
  6882. }
  6883. return 1;
  6884. }
  6885. static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
  6886. struct page *page, unsigned int pgoff,
  6887. u64 start, u64 end, int failed_mirror,
  6888. bio_end_io_t *repair_endio, void *repair_arg)
  6889. {
  6890. struct io_failure_record *failrec;
  6891. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  6892. struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
  6893. struct bio *bio;
  6894. int isector;
  6895. unsigned int read_mode = 0;
  6896. int segs;
  6897. int ret;
  6898. blk_status_t status;
  6899. struct bio_vec bvec;
  6900. BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
  6901. ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
  6902. if (ret)
  6903. return errno_to_blk_status(ret);
  6904. ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
  6905. failed_mirror);
  6906. if (!ret) {
  6907. free_io_failure(failure_tree, io_tree, failrec);
  6908. return BLK_STS_IOERR;
  6909. }
  6910. segs = bio_segments(failed_bio);
  6911. bio_get_first_bvec(failed_bio, &bvec);
  6912. if (segs > 1 ||
  6913. (bvec.bv_len > btrfs_inode_sectorsize(inode)))
  6914. read_mode |= REQ_FAILFAST_DEV;
  6915. isector = start - btrfs_io_bio(failed_bio)->logical;
  6916. isector >>= inode->i_sb->s_blocksize_bits;
  6917. bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
  6918. pgoff, isector, repair_endio, repair_arg);
  6919. bio->bi_opf = REQ_OP_READ | read_mode;
  6920. btrfs_debug(BTRFS_I(inode)->root->fs_info,
  6921. "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
  6922. read_mode, failrec->this_mirror, failrec->in_validation);
  6923. status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
  6924. if (status) {
  6925. free_io_failure(failure_tree, io_tree, failrec);
  6926. bio_put(bio);
  6927. }
  6928. return status;
  6929. }
  6930. struct btrfs_retry_complete {
  6931. struct completion done;
  6932. struct inode *inode;
  6933. u64 start;
  6934. int uptodate;
  6935. };
  6936. static void btrfs_retry_endio_nocsum(struct bio *bio)
  6937. {
  6938. struct btrfs_retry_complete *done = bio->bi_private;
  6939. struct inode *inode = done->inode;
  6940. struct bio_vec *bvec;
  6941. struct extent_io_tree *io_tree, *failure_tree;
  6942. int i;
  6943. if (bio->bi_status)
  6944. goto end;
  6945. ASSERT(bio->bi_vcnt == 1);
  6946. io_tree = &BTRFS_I(inode)->io_tree;
  6947. failure_tree = &BTRFS_I(inode)->io_failure_tree;
  6948. ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(inode));
  6949. done->uptodate = 1;
  6950. ASSERT(!bio_flagged(bio, BIO_CLONED));
  6951. bio_for_each_segment_all(bvec, bio, i)
  6952. clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
  6953. io_tree, done->start, bvec->bv_page,
  6954. btrfs_ino(BTRFS_I(inode)), 0);
  6955. end:
  6956. complete(&done->done);
  6957. bio_put(bio);
  6958. }
  6959. static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
  6960. struct btrfs_io_bio *io_bio)
  6961. {
  6962. struct btrfs_fs_info *fs_info;
  6963. struct bio_vec bvec;
  6964. struct bvec_iter iter;
  6965. struct btrfs_retry_complete done;
  6966. u64 start;
  6967. unsigned int pgoff;
  6968. u32 sectorsize;
  6969. int nr_sectors;
  6970. blk_status_t ret;
  6971. blk_status_t err = BLK_STS_OK;
  6972. fs_info = BTRFS_I(inode)->root->fs_info;
  6973. sectorsize = fs_info->sectorsize;
  6974. start = io_bio->logical;
  6975. done.inode = inode;
  6976. io_bio->bio.bi_iter = io_bio->iter;
  6977. bio_for_each_segment(bvec, &io_bio->bio, iter) {
  6978. nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
  6979. pgoff = bvec.bv_offset;
  6980. next_block_or_try_again:
  6981. done.uptodate = 0;
  6982. done.start = start;
  6983. init_completion(&done.done);
  6984. ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
  6985. pgoff, start, start + sectorsize - 1,
  6986. io_bio->mirror_num,
  6987. btrfs_retry_endio_nocsum, &done);
  6988. if (ret) {
  6989. err = ret;
  6990. goto next;
  6991. }
  6992. wait_for_completion_io(&done.done);
  6993. if (!done.uptodate) {
  6994. /* We might have another mirror, so try again */
  6995. goto next_block_or_try_again;
  6996. }
  6997. next:
  6998. start += sectorsize;
  6999. nr_sectors--;
  7000. if (nr_sectors) {
  7001. pgoff += sectorsize;
  7002. ASSERT(pgoff < PAGE_SIZE);
  7003. goto next_block_or_try_again;
  7004. }
  7005. }
  7006. return err;
  7007. }
  7008. static void btrfs_retry_endio(struct bio *bio)
  7009. {
  7010. struct btrfs_retry_complete *done = bio->bi_private;
  7011. struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
  7012. struct extent_io_tree *io_tree, *failure_tree;
  7013. struct inode *inode = done->inode;
  7014. struct bio_vec *bvec;
  7015. int uptodate;
  7016. int ret;
  7017. int i;
  7018. if (bio->bi_status)
  7019. goto end;
  7020. uptodate = 1;
  7021. ASSERT(bio->bi_vcnt == 1);
  7022. ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(done->inode));
  7023. io_tree = &BTRFS_I(inode)->io_tree;
  7024. failure_tree = &BTRFS_I(inode)->io_failure_tree;
  7025. ASSERT(!bio_flagged(bio, BIO_CLONED));
  7026. bio_for_each_segment_all(bvec, bio, i) {
  7027. ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
  7028. bvec->bv_offset, done->start,
  7029. bvec->bv_len);
  7030. if (!ret)
  7031. clean_io_failure(BTRFS_I(inode)->root->fs_info,
  7032. failure_tree, io_tree, done->start,
  7033. bvec->bv_page,
  7034. btrfs_ino(BTRFS_I(inode)),
  7035. bvec->bv_offset);
  7036. else
  7037. uptodate = 0;
  7038. }
  7039. done->uptodate = uptodate;
  7040. end:
  7041. complete(&done->done);
  7042. bio_put(bio);
  7043. }
  7044. static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
  7045. struct btrfs_io_bio *io_bio, blk_status_t err)
  7046. {
  7047. struct btrfs_fs_info *fs_info;
  7048. struct bio_vec bvec;
  7049. struct bvec_iter iter;
  7050. struct btrfs_retry_complete done;
  7051. u64 start;
  7052. u64 offset = 0;
  7053. u32 sectorsize;
  7054. int nr_sectors;
  7055. unsigned int pgoff;
  7056. int csum_pos;
  7057. bool uptodate = (err == 0);
  7058. int ret;
  7059. blk_status_t status;
  7060. fs_info = BTRFS_I(inode)->root->fs_info;
  7061. sectorsize = fs_info->sectorsize;
  7062. err = BLK_STS_OK;
  7063. start = io_bio->logical;
  7064. done.inode = inode;
  7065. io_bio->bio.bi_iter = io_bio->iter;
  7066. bio_for_each_segment(bvec, &io_bio->bio, iter) {
  7067. nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
  7068. pgoff = bvec.bv_offset;
  7069. next_block:
  7070. if (uptodate) {
  7071. csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
  7072. ret = __readpage_endio_check(inode, io_bio, csum_pos,
  7073. bvec.bv_page, pgoff, start, sectorsize);
  7074. if (likely(!ret))
  7075. goto next;
  7076. }
  7077. try_again:
  7078. done.uptodate = 0;
  7079. done.start = start;
  7080. init_completion(&done.done);
  7081. status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
  7082. pgoff, start, start + sectorsize - 1,
  7083. io_bio->mirror_num, btrfs_retry_endio,
  7084. &done);
  7085. if (status) {
  7086. err = status;
  7087. goto next;
  7088. }
  7089. wait_for_completion_io(&done.done);
  7090. if (!done.uptodate) {
  7091. /* We might have another mirror, so try again */
  7092. goto try_again;
  7093. }
  7094. next:
  7095. offset += sectorsize;
  7096. start += sectorsize;
  7097. ASSERT(nr_sectors);
  7098. nr_sectors--;
  7099. if (nr_sectors) {
  7100. pgoff += sectorsize;
  7101. ASSERT(pgoff < PAGE_SIZE);
  7102. goto next_block;
  7103. }
  7104. }
  7105. return err;
  7106. }
  7107. static blk_status_t btrfs_subio_endio_read(struct inode *inode,
  7108. struct btrfs_io_bio *io_bio, blk_status_t err)
  7109. {
  7110. bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  7111. if (skip_csum) {
  7112. if (unlikely(err))
  7113. return __btrfs_correct_data_nocsum(inode, io_bio);
  7114. else
  7115. return BLK_STS_OK;
  7116. } else {
  7117. return __btrfs_subio_endio_read(inode, io_bio, err);
  7118. }
  7119. }
  7120. static void btrfs_endio_direct_read(struct bio *bio)
  7121. {
  7122. struct btrfs_dio_private *dip = bio->bi_private;
  7123. struct inode *inode = dip->inode;
  7124. struct bio *dio_bio;
  7125. struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
  7126. blk_status_t err = bio->bi_status;
  7127. if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
  7128. err = btrfs_subio_endio_read(inode, io_bio, err);
  7129. unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
  7130. dip->logical_offset + dip->bytes - 1);
  7131. dio_bio = dip->dio_bio;
  7132. kfree(dip);
  7133. dio_bio->bi_status = err;
  7134. dio_end_io(dio_bio);
  7135. if (io_bio->end_io)
  7136. io_bio->end_io(io_bio, blk_status_to_errno(err));
  7137. bio_put(bio);
  7138. }
  7139. static void __endio_write_update_ordered(struct inode *inode,
  7140. const u64 offset, const u64 bytes,
  7141. const bool uptodate)
  7142. {
  7143. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  7144. struct btrfs_ordered_extent *ordered = NULL;
  7145. struct btrfs_workqueue *wq;
  7146. btrfs_work_func_t func;
  7147. u64 ordered_offset = offset;
  7148. u64 ordered_bytes = bytes;
  7149. u64 last_offset;
  7150. if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
  7151. wq = fs_info->endio_freespace_worker;
  7152. func = btrfs_freespace_write_helper;
  7153. } else {
  7154. wq = fs_info->endio_write_workers;
  7155. func = btrfs_endio_write_helper;
  7156. }
  7157. while (ordered_offset < offset + bytes) {
  7158. last_offset = ordered_offset;
  7159. if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
  7160. &ordered_offset,
  7161. ordered_bytes,
  7162. uptodate)) {
  7163. btrfs_init_work(&ordered->work, func,
  7164. finish_ordered_fn,
  7165. NULL, NULL);
  7166. btrfs_queue_work(wq, &ordered->work);
  7167. }
  7168. /*
  7169. * If btrfs_dec_test_ordered_pending does not find any ordered
  7170. * extent in the range, we can exit.
  7171. */
  7172. if (ordered_offset == last_offset)
  7173. return;
  7174. /*
  7175. * Our bio might span multiple ordered extents. In this case
  7176. * we keep goin until we have accounted the whole dio.
  7177. */
  7178. if (ordered_offset < offset + bytes) {
  7179. ordered_bytes = offset + bytes - ordered_offset;
  7180. ordered = NULL;
  7181. }
  7182. }
  7183. }
  7184. static void btrfs_endio_direct_write(struct bio *bio)
  7185. {
  7186. struct btrfs_dio_private *dip = bio->bi_private;
  7187. struct bio *dio_bio = dip->dio_bio;
  7188. __endio_write_update_ordered(dip->inode, dip->logical_offset,
  7189. dip->bytes, !bio->bi_status);
  7190. kfree(dip);
  7191. dio_bio->bi_status = bio->bi_status;
  7192. dio_end_io(dio_bio);
  7193. bio_put(bio);
  7194. }
  7195. static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
  7196. struct bio *bio, u64 offset)
  7197. {
  7198. struct inode *inode = private_data;
  7199. blk_status_t ret;
  7200. ret = btrfs_csum_one_bio(inode, bio, offset, 1);
  7201. BUG_ON(ret); /* -ENOMEM */
  7202. return 0;
  7203. }
  7204. static void btrfs_end_dio_bio(struct bio *bio)
  7205. {
  7206. struct btrfs_dio_private *dip = bio->bi_private;
  7207. blk_status_t err = bio->bi_status;
  7208. if (err)
  7209. btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
  7210. "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
  7211. btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
  7212. bio->bi_opf,
  7213. (unsigned long long)bio->bi_iter.bi_sector,
  7214. bio->bi_iter.bi_size, err);
  7215. if (dip->subio_endio)
  7216. err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
  7217. if (err) {
  7218. /*
  7219. * We want to perceive the errors flag being set before
  7220. * decrementing the reference count. We don't need a barrier
  7221. * since atomic operations with a return value are fully
  7222. * ordered as per atomic_t.txt
  7223. */
  7224. dip->errors = 1;
  7225. }
  7226. /* if there are more bios still pending for this dio, just exit */
  7227. if (!atomic_dec_and_test(&dip->pending_bios))
  7228. goto out;
  7229. if (dip->errors) {
  7230. bio_io_error(dip->orig_bio);
  7231. } else {
  7232. dip->dio_bio->bi_status = BLK_STS_OK;
  7233. bio_endio(dip->orig_bio);
  7234. }
  7235. out:
  7236. bio_put(bio);
  7237. }
  7238. static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
  7239. struct btrfs_dio_private *dip,
  7240. struct bio *bio,
  7241. u64 file_offset)
  7242. {
  7243. struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
  7244. struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
  7245. blk_status_t ret;
  7246. /*
  7247. * We load all the csum data we need when we submit
  7248. * the first bio to reduce the csum tree search and
  7249. * contention.
  7250. */
  7251. if (dip->logical_offset == file_offset) {
  7252. ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
  7253. file_offset);
  7254. if (ret)
  7255. return ret;
  7256. }
  7257. if (bio == dip->orig_bio)
  7258. return 0;
  7259. file_offset -= dip->logical_offset;
  7260. file_offset >>= inode->i_sb->s_blocksize_bits;
  7261. io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
  7262. return 0;
  7263. }
  7264. static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
  7265. struct inode *inode, u64 file_offset, int async_submit)
  7266. {
  7267. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  7268. struct btrfs_dio_private *dip = bio->bi_private;
  7269. bool write = bio_op(bio) == REQ_OP_WRITE;
  7270. blk_status_t ret;
  7271. /* Check btrfs_submit_bio_hook() for rules about async submit. */
  7272. if (async_submit)
  7273. async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
  7274. if (!write) {
  7275. ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
  7276. if (ret)
  7277. goto err;
  7278. }
  7279. if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
  7280. goto map;
  7281. if (write && async_submit) {
  7282. ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
  7283. file_offset, inode,
  7284. btrfs_submit_bio_start_direct_io);
  7285. goto err;
  7286. } else if (write) {
  7287. /*
  7288. * If we aren't doing async submit, calculate the csum of the
  7289. * bio now.
  7290. */
  7291. ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
  7292. if (ret)
  7293. goto err;
  7294. } else {
  7295. ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
  7296. file_offset);
  7297. if (ret)
  7298. goto err;
  7299. }
  7300. map:
  7301. ret = btrfs_map_bio(fs_info, bio, 0, 0);
  7302. err:
  7303. return ret;
  7304. }
  7305. static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
  7306. {
  7307. struct inode *inode = dip->inode;
  7308. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  7309. struct bio *bio;
  7310. struct bio *orig_bio = dip->orig_bio;
  7311. u64 start_sector = orig_bio->bi_iter.bi_sector;
  7312. u64 file_offset = dip->logical_offset;
  7313. u64 map_length;
  7314. int async_submit = 0;
  7315. u64 submit_len;
  7316. int clone_offset = 0;
  7317. int clone_len;
  7318. int ret;
  7319. blk_status_t status;
  7320. map_length = orig_bio->bi_iter.bi_size;
  7321. submit_len = map_length;
  7322. ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
  7323. &map_length, NULL, 0);
  7324. if (ret)
  7325. return -EIO;
  7326. if (map_length >= submit_len) {
  7327. bio = orig_bio;
  7328. dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
  7329. goto submit;
  7330. }
  7331. /* async crcs make it difficult to collect full stripe writes. */
  7332. if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
  7333. async_submit = 0;
  7334. else
  7335. async_submit = 1;
  7336. /* bio split */
  7337. ASSERT(map_length <= INT_MAX);
  7338. atomic_inc(&dip->pending_bios);
  7339. do {
  7340. clone_len = min_t(int, submit_len, map_length);
  7341. /*
  7342. * This will never fail as it's passing GPF_NOFS and
  7343. * the allocation is backed by btrfs_bioset.
  7344. */
  7345. bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
  7346. clone_len);
  7347. bio->bi_private = dip;
  7348. bio->bi_end_io = btrfs_end_dio_bio;
  7349. btrfs_io_bio(bio)->logical = file_offset;
  7350. ASSERT(submit_len >= clone_len);
  7351. submit_len -= clone_len;
  7352. if (submit_len == 0)
  7353. break;
  7354. /*
  7355. * Increase the count before we submit the bio so we know
  7356. * the end IO handler won't happen before we increase the
  7357. * count. Otherwise, the dip might get freed before we're
  7358. * done setting it up.
  7359. */
  7360. atomic_inc(&dip->pending_bios);
  7361. status = btrfs_submit_dio_bio(bio, inode, file_offset,
  7362. async_submit);
  7363. if (status) {
  7364. bio_put(bio);
  7365. atomic_dec(&dip->pending_bios);
  7366. goto out_err;
  7367. }
  7368. clone_offset += clone_len;
  7369. start_sector += clone_len >> 9;
  7370. file_offset += clone_len;
  7371. map_length = submit_len;
  7372. ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
  7373. start_sector << 9, &map_length, NULL, 0);
  7374. if (ret)
  7375. goto out_err;
  7376. } while (submit_len > 0);
  7377. submit:
  7378. status = btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
  7379. if (!status)
  7380. return 0;
  7381. bio_put(bio);
  7382. out_err:
  7383. dip->errors = 1;
  7384. /*
  7385. * Before atomic variable goto zero, we must make sure dip->errors is
  7386. * perceived to be set. This ordering is ensured by the fact that an
  7387. * atomic operations with a return value are fully ordered as per
  7388. * atomic_t.txt
  7389. */
  7390. if (atomic_dec_and_test(&dip->pending_bios))
  7391. bio_io_error(dip->orig_bio);
  7392. /* bio_end_io() will handle error, so we needn't return it */
  7393. return 0;
  7394. }
  7395. static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
  7396. loff_t file_offset)
  7397. {
  7398. struct btrfs_dio_private *dip = NULL;
  7399. struct bio *bio = NULL;
  7400. struct btrfs_io_bio *io_bio;
  7401. bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
  7402. int ret = 0;
  7403. bio = btrfs_bio_clone(dio_bio);
  7404. dip = kzalloc(sizeof(*dip), GFP_NOFS);
  7405. if (!dip) {
  7406. ret = -ENOMEM;
  7407. goto free_ordered;
  7408. }
  7409. dip->private = dio_bio->bi_private;
  7410. dip->inode = inode;
  7411. dip->logical_offset = file_offset;
  7412. dip->bytes = dio_bio->bi_iter.bi_size;
  7413. dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
  7414. bio->bi_private = dip;
  7415. dip->orig_bio = bio;
  7416. dip->dio_bio = dio_bio;
  7417. atomic_set(&dip->pending_bios, 0);
  7418. io_bio = btrfs_io_bio(bio);
  7419. io_bio->logical = file_offset;
  7420. if (write) {
  7421. bio->bi_end_io = btrfs_endio_direct_write;
  7422. } else {
  7423. bio->bi_end_io = btrfs_endio_direct_read;
  7424. dip->subio_endio = btrfs_subio_endio_read;
  7425. }
  7426. /*
  7427. * Reset the range for unsubmitted ordered extents (to a 0 length range)
  7428. * even if we fail to submit a bio, because in such case we do the
  7429. * corresponding error handling below and it must not be done a second
  7430. * time by btrfs_direct_IO().
  7431. */
  7432. if (write) {
  7433. struct btrfs_dio_data *dio_data = current->journal_info;
  7434. dio_data->unsubmitted_oe_range_end = dip->logical_offset +
  7435. dip->bytes;
  7436. dio_data->unsubmitted_oe_range_start =
  7437. dio_data->unsubmitted_oe_range_end;
  7438. }
  7439. ret = btrfs_submit_direct_hook(dip);
  7440. if (!ret)
  7441. return;
  7442. if (io_bio->end_io)
  7443. io_bio->end_io(io_bio, ret);
  7444. free_ordered:
  7445. /*
  7446. * If we arrived here it means either we failed to submit the dip
  7447. * or we either failed to clone the dio_bio or failed to allocate the
  7448. * dip. If we cloned the dio_bio and allocated the dip, we can just
  7449. * call bio_endio against our io_bio so that we get proper resource
  7450. * cleanup if we fail to submit the dip, otherwise, we must do the
  7451. * same as btrfs_endio_direct_[write|read] because we can't call these
  7452. * callbacks - they require an allocated dip and a clone of dio_bio.
  7453. */
  7454. if (bio && dip) {
  7455. bio_io_error(bio);
  7456. /*
  7457. * The end io callbacks free our dip, do the final put on bio
  7458. * and all the cleanup and final put for dio_bio (through
  7459. * dio_end_io()).
  7460. */
  7461. dip = NULL;
  7462. bio = NULL;
  7463. } else {
  7464. if (write)
  7465. __endio_write_update_ordered(inode,
  7466. file_offset,
  7467. dio_bio->bi_iter.bi_size,
  7468. false);
  7469. else
  7470. unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
  7471. file_offset + dio_bio->bi_iter.bi_size - 1);
  7472. dio_bio->bi_status = BLK_STS_IOERR;
  7473. /*
  7474. * Releases and cleans up our dio_bio, no need to bio_put()
  7475. * nor bio_endio()/bio_io_error() against dio_bio.
  7476. */
  7477. dio_end_io(dio_bio);
  7478. }
  7479. if (bio)
  7480. bio_put(bio);
  7481. kfree(dip);
  7482. }
  7483. static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
  7484. const struct iov_iter *iter, loff_t offset)
  7485. {
  7486. int seg;
  7487. int i;
  7488. unsigned int blocksize_mask = fs_info->sectorsize - 1;
  7489. ssize_t retval = -EINVAL;
  7490. if (offset & blocksize_mask)
  7491. goto out;
  7492. if (iov_iter_alignment(iter) & blocksize_mask)
  7493. goto out;
  7494. /* If this is a write we don't need to check anymore */
  7495. if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
  7496. return 0;
  7497. /*
  7498. * Check to make sure we don't have duplicate iov_base's in this
  7499. * iovec, if so return EINVAL, otherwise we'll get csum errors
  7500. * when reading back.
  7501. */
  7502. for (seg = 0; seg < iter->nr_segs; seg++) {
  7503. for (i = seg + 1; i < iter->nr_segs; i++) {
  7504. if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
  7505. goto out;
  7506. }
  7507. }
  7508. retval = 0;
  7509. out:
  7510. return retval;
  7511. }
  7512. static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
  7513. {
  7514. struct file *file = iocb->ki_filp;
  7515. struct inode *inode = file->f_mapping->host;
  7516. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  7517. struct btrfs_dio_data dio_data = { 0 };
  7518. struct extent_changeset *data_reserved = NULL;
  7519. loff_t offset = iocb->ki_pos;
  7520. size_t count = 0;
  7521. int flags = 0;
  7522. bool wakeup = true;
  7523. bool relock = false;
  7524. ssize_t ret;
  7525. if (check_direct_IO(fs_info, iter, offset))
  7526. return 0;
  7527. inode_dio_begin(inode);
  7528. /*
  7529. * The generic stuff only does filemap_write_and_wait_range, which
  7530. * isn't enough if we've written compressed pages to this area, so
  7531. * we need to flush the dirty pages again to make absolutely sure
  7532. * that any outstanding dirty pages are on disk.
  7533. */
  7534. count = iov_iter_count(iter);
  7535. if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
  7536. &BTRFS_I(inode)->runtime_flags))
  7537. filemap_fdatawrite_range(inode->i_mapping, offset,
  7538. offset + count - 1);
  7539. if (iov_iter_rw(iter) == WRITE) {
  7540. /*
  7541. * If the write DIO is beyond the EOF, we need update
  7542. * the isize, but it is protected by i_mutex. So we can
  7543. * not unlock the i_mutex at this case.
  7544. */
  7545. if (offset + count <= inode->i_size) {
  7546. dio_data.overwrite = 1;
  7547. inode_unlock(inode);
  7548. relock = true;
  7549. } else if (iocb->ki_flags & IOCB_NOWAIT) {
  7550. ret = -EAGAIN;
  7551. goto out;
  7552. }
  7553. ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
  7554. offset, count);
  7555. if (ret)
  7556. goto out;
  7557. /*
  7558. * We need to know how many extents we reserved so that we can
  7559. * do the accounting properly if we go over the number we
  7560. * originally calculated. Abuse current->journal_info for this.
  7561. */
  7562. dio_data.reserve = round_up(count,
  7563. fs_info->sectorsize);
  7564. dio_data.unsubmitted_oe_range_start = (u64)offset;
  7565. dio_data.unsubmitted_oe_range_end = (u64)offset;
  7566. current->journal_info = &dio_data;
  7567. down_read(&BTRFS_I(inode)->dio_sem);
  7568. } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
  7569. &BTRFS_I(inode)->runtime_flags)) {
  7570. inode_dio_end(inode);
  7571. flags = DIO_LOCKING | DIO_SKIP_HOLES;
  7572. wakeup = false;
  7573. }
  7574. ret = __blockdev_direct_IO(iocb, inode,
  7575. fs_info->fs_devices->latest_bdev,
  7576. iter, btrfs_get_blocks_direct, NULL,
  7577. btrfs_submit_direct, flags);
  7578. if (iov_iter_rw(iter) == WRITE) {
  7579. up_read(&BTRFS_I(inode)->dio_sem);
  7580. current->journal_info = NULL;
  7581. if (ret < 0 && ret != -EIOCBQUEUED) {
  7582. if (dio_data.reserve)
  7583. btrfs_delalloc_release_space(inode, data_reserved,
  7584. offset, dio_data.reserve, true);
  7585. /*
  7586. * On error we might have left some ordered extents
  7587. * without submitting corresponding bios for them, so
  7588. * cleanup them up to avoid other tasks getting them
  7589. * and waiting for them to complete forever.
  7590. */
  7591. if (dio_data.unsubmitted_oe_range_start <
  7592. dio_data.unsubmitted_oe_range_end)
  7593. __endio_write_update_ordered(inode,
  7594. dio_data.unsubmitted_oe_range_start,
  7595. dio_data.unsubmitted_oe_range_end -
  7596. dio_data.unsubmitted_oe_range_start,
  7597. false);
  7598. } else if (ret >= 0 && (size_t)ret < count)
  7599. btrfs_delalloc_release_space(inode, data_reserved,
  7600. offset, count - (size_t)ret, true);
  7601. btrfs_delalloc_release_extents(BTRFS_I(inode), count, false);
  7602. }
  7603. out:
  7604. if (wakeup)
  7605. inode_dio_end(inode);
  7606. if (relock)
  7607. inode_lock(inode);
  7608. extent_changeset_free(data_reserved);
  7609. return ret;
  7610. }
  7611. #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
  7612. static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  7613. __u64 start, __u64 len)
  7614. {
  7615. int ret;
  7616. ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
  7617. if (ret)
  7618. return ret;
  7619. return extent_fiemap(inode, fieinfo, start, len);
  7620. }
  7621. int btrfs_readpage(struct file *file, struct page *page)
  7622. {
  7623. struct extent_io_tree *tree;
  7624. tree = &BTRFS_I(page->mapping->host)->io_tree;
  7625. return extent_read_full_page(tree, page, btrfs_get_extent, 0);
  7626. }
  7627. static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
  7628. {
  7629. struct inode *inode = page->mapping->host;
  7630. int ret;
  7631. if (current->flags & PF_MEMALLOC) {
  7632. redirty_page_for_writepage(wbc, page);
  7633. unlock_page(page);
  7634. return 0;
  7635. }
  7636. /*
  7637. * If we are under memory pressure we will call this directly from the
  7638. * VM, we need to make sure we have the inode referenced for the ordered
  7639. * extent. If not just return like we didn't do anything.
  7640. */
  7641. if (!igrab(inode)) {
  7642. redirty_page_for_writepage(wbc, page);
  7643. return AOP_WRITEPAGE_ACTIVATE;
  7644. }
  7645. ret = extent_write_full_page(page, wbc);
  7646. btrfs_add_delayed_iput(inode);
  7647. return ret;
  7648. }
  7649. static int btrfs_writepages(struct address_space *mapping,
  7650. struct writeback_control *wbc)
  7651. {
  7652. return extent_writepages(mapping, wbc);
  7653. }
  7654. static int
  7655. btrfs_readpages(struct file *file, struct address_space *mapping,
  7656. struct list_head *pages, unsigned nr_pages)
  7657. {
  7658. return extent_readpages(mapping, pages, nr_pages);
  7659. }
  7660. static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  7661. {
  7662. int ret = try_release_extent_mapping(page, gfp_flags);
  7663. if (ret == 1) {
  7664. ClearPagePrivate(page);
  7665. set_page_private(page, 0);
  7666. put_page(page);
  7667. }
  7668. return ret;
  7669. }
  7670. static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  7671. {
  7672. if (PageWriteback(page) || PageDirty(page))
  7673. return 0;
  7674. return __btrfs_releasepage(page, gfp_flags);
  7675. }
  7676. static void btrfs_invalidatepage(struct page *page, unsigned int offset,
  7677. unsigned int length)
  7678. {
  7679. struct inode *inode = page->mapping->host;
  7680. struct extent_io_tree *tree;
  7681. struct btrfs_ordered_extent *ordered;
  7682. struct extent_state *cached_state = NULL;
  7683. u64 page_start = page_offset(page);
  7684. u64 page_end = page_start + PAGE_SIZE - 1;
  7685. u64 start;
  7686. u64 end;
  7687. int inode_evicting = inode->i_state & I_FREEING;
  7688. /*
  7689. * we have the page locked, so new writeback can't start,
  7690. * and the dirty bit won't be cleared while we are here.
  7691. *
  7692. * Wait for IO on this page so that we can safely clear
  7693. * the PagePrivate2 bit and do ordered accounting
  7694. */
  7695. wait_on_page_writeback(page);
  7696. tree = &BTRFS_I(inode)->io_tree;
  7697. if (offset) {
  7698. btrfs_releasepage(page, GFP_NOFS);
  7699. return;
  7700. }
  7701. if (!inode_evicting)
  7702. lock_extent_bits(tree, page_start, page_end, &cached_state);
  7703. again:
  7704. start = page_start;
  7705. ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
  7706. page_end - start + 1);
  7707. if (ordered) {
  7708. end = min(page_end, ordered->file_offset + ordered->len - 1);
  7709. /*
  7710. * IO on this page will never be started, so we need
  7711. * to account for any ordered extents now
  7712. */
  7713. if (!inode_evicting)
  7714. clear_extent_bit(tree, start, end,
  7715. EXTENT_DIRTY | EXTENT_DELALLOC |
  7716. EXTENT_DELALLOC_NEW |
  7717. EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
  7718. EXTENT_DEFRAG, 1, 0, &cached_state);
  7719. /*
  7720. * whoever cleared the private bit is responsible
  7721. * for the finish_ordered_io
  7722. */
  7723. if (TestClearPagePrivate2(page)) {
  7724. struct btrfs_ordered_inode_tree *tree;
  7725. u64 new_len;
  7726. tree = &BTRFS_I(inode)->ordered_tree;
  7727. spin_lock_irq(&tree->lock);
  7728. set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
  7729. new_len = start - ordered->file_offset;
  7730. if (new_len < ordered->truncated_len)
  7731. ordered->truncated_len = new_len;
  7732. spin_unlock_irq(&tree->lock);
  7733. if (btrfs_dec_test_ordered_pending(inode, &ordered,
  7734. start,
  7735. end - start + 1, 1))
  7736. btrfs_finish_ordered_io(ordered);
  7737. }
  7738. btrfs_put_ordered_extent(ordered);
  7739. if (!inode_evicting) {
  7740. cached_state = NULL;
  7741. lock_extent_bits(tree, start, end,
  7742. &cached_state);
  7743. }
  7744. start = end + 1;
  7745. if (start < page_end)
  7746. goto again;
  7747. }
  7748. /*
  7749. * Qgroup reserved space handler
  7750. * Page here will be either
  7751. * 1) Already written to disk
  7752. * In this case, its reserved space is released from data rsv map
  7753. * and will be freed by delayed_ref handler finally.
  7754. * So even we call qgroup_free_data(), it won't decrease reserved
  7755. * space.
  7756. * 2) Not written to disk
  7757. * This means the reserved space should be freed here. However,
  7758. * if a truncate invalidates the page (by clearing PageDirty)
  7759. * and the page is accounted for while allocating extent
  7760. * in btrfs_check_data_free_space() we let delayed_ref to
  7761. * free the entire extent.
  7762. */
  7763. if (PageDirty(page))
  7764. btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
  7765. if (!inode_evicting) {
  7766. clear_extent_bit(tree, page_start, page_end,
  7767. EXTENT_LOCKED | EXTENT_DIRTY |
  7768. EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
  7769. EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
  7770. &cached_state);
  7771. __btrfs_releasepage(page, GFP_NOFS);
  7772. }
  7773. ClearPageChecked(page);
  7774. if (PagePrivate(page)) {
  7775. ClearPagePrivate(page);
  7776. set_page_private(page, 0);
  7777. put_page(page);
  7778. }
  7779. }
  7780. /*
  7781. * btrfs_page_mkwrite() is not allowed to change the file size as it gets
  7782. * called from a page fault handler when a page is first dirtied. Hence we must
  7783. * be careful to check for EOF conditions here. We set the page up correctly
  7784. * for a written page which means we get ENOSPC checking when writing into
  7785. * holes and correct delalloc and unwritten extent mapping on filesystems that
  7786. * support these features.
  7787. *
  7788. * We are not allowed to take the i_mutex here so we have to play games to
  7789. * protect against truncate races as the page could now be beyond EOF. Because
  7790. * truncate_setsize() writes the inode size before removing pages, once we have
  7791. * the page lock we can determine safely if the page is beyond EOF. If it is not
  7792. * beyond EOF, then the page is guaranteed safe against truncation until we
  7793. * unlock the page.
  7794. */
  7795. vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
  7796. {
  7797. struct page *page = vmf->page;
  7798. struct inode *inode = file_inode(vmf->vma->vm_file);
  7799. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  7800. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  7801. struct btrfs_ordered_extent *ordered;
  7802. struct extent_state *cached_state = NULL;
  7803. struct extent_changeset *data_reserved = NULL;
  7804. char *kaddr;
  7805. unsigned long zero_start;
  7806. loff_t size;
  7807. vm_fault_t ret;
  7808. int ret2;
  7809. int reserved = 0;
  7810. u64 reserved_space;
  7811. u64 page_start;
  7812. u64 page_end;
  7813. u64 end;
  7814. reserved_space = PAGE_SIZE;
  7815. sb_start_pagefault(inode->i_sb);
  7816. page_start = page_offset(page);
  7817. page_end = page_start + PAGE_SIZE - 1;
  7818. end = page_end;
  7819. /*
  7820. * Reserving delalloc space after obtaining the page lock can lead to
  7821. * deadlock. For example, if a dirty page is locked by this function
  7822. * and the call to btrfs_delalloc_reserve_space() ends up triggering
  7823. * dirty page write out, then the btrfs_writepage() function could
  7824. * end up waiting indefinitely to get a lock on the page currently
  7825. * being processed by btrfs_page_mkwrite() function.
  7826. */
  7827. ret2 = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
  7828. reserved_space);
  7829. if (!ret2) {
  7830. ret2 = file_update_time(vmf->vma->vm_file);
  7831. reserved = 1;
  7832. }
  7833. if (ret2) {
  7834. ret = vmf_error(ret2);
  7835. if (reserved)
  7836. goto out;
  7837. goto out_noreserve;
  7838. }
  7839. ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
  7840. again:
  7841. lock_page(page);
  7842. size = i_size_read(inode);
  7843. if ((page->mapping != inode->i_mapping) ||
  7844. (page_start >= size)) {
  7845. /* page got truncated out from underneath us */
  7846. goto out_unlock;
  7847. }
  7848. wait_on_page_writeback(page);
  7849. lock_extent_bits(io_tree, page_start, page_end, &cached_state);
  7850. set_page_extent_mapped(page);
  7851. /*
  7852. * we can't set the delalloc bits if there are pending ordered
  7853. * extents. Drop our locks and wait for them to finish
  7854. */
  7855. ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
  7856. PAGE_SIZE);
  7857. if (ordered) {
  7858. unlock_extent_cached(io_tree, page_start, page_end,
  7859. &cached_state);
  7860. unlock_page(page);
  7861. btrfs_start_ordered_extent(inode, ordered, 1);
  7862. btrfs_put_ordered_extent(ordered);
  7863. goto again;
  7864. }
  7865. if (page->index == ((size - 1) >> PAGE_SHIFT)) {
  7866. reserved_space = round_up(size - page_start,
  7867. fs_info->sectorsize);
  7868. if (reserved_space < PAGE_SIZE) {
  7869. end = page_start + reserved_space - 1;
  7870. btrfs_delalloc_release_space(inode, data_reserved,
  7871. page_start, PAGE_SIZE - reserved_space,
  7872. true);
  7873. }
  7874. }
  7875. /*
  7876. * page_mkwrite gets called when the page is firstly dirtied after it's
  7877. * faulted in, but write(2) could also dirty a page and set delalloc
  7878. * bits, thus in this case for space account reason, we still need to
  7879. * clear any delalloc bits within this page range since we have to
  7880. * reserve data&meta space before lock_page() (see above comments).
  7881. */
  7882. clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
  7883. EXTENT_DIRTY | EXTENT_DELALLOC |
  7884. EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
  7885. 0, 0, &cached_state);
  7886. ret2 = btrfs_set_extent_delalloc(inode, page_start, end, 0,
  7887. &cached_state, 0);
  7888. if (ret2) {
  7889. unlock_extent_cached(io_tree, page_start, page_end,
  7890. &cached_state);
  7891. ret = VM_FAULT_SIGBUS;
  7892. goto out_unlock;
  7893. }
  7894. ret2 = 0;
  7895. /* page is wholly or partially inside EOF */
  7896. if (page_start + PAGE_SIZE > size)
  7897. zero_start = size & ~PAGE_MASK;
  7898. else
  7899. zero_start = PAGE_SIZE;
  7900. if (zero_start != PAGE_SIZE) {
  7901. kaddr = kmap(page);
  7902. memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
  7903. flush_dcache_page(page);
  7904. kunmap(page);
  7905. }
  7906. ClearPageChecked(page);
  7907. set_page_dirty(page);
  7908. SetPageUptodate(page);
  7909. BTRFS_I(inode)->last_trans = fs_info->generation;
  7910. BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
  7911. BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
  7912. unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
  7913. if (!ret2) {
  7914. btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, true);
  7915. sb_end_pagefault(inode->i_sb);
  7916. extent_changeset_free(data_reserved);
  7917. return VM_FAULT_LOCKED;
  7918. }
  7919. out_unlock:
  7920. unlock_page(page);
  7921. out:
  7922. btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, (ret != 0));
  7923. btrfs_delalloc_release_space(inode, data_reserved, page_start,
  7924. reserved_space, (ret != 0));
  7925. out_noreserve:
  7926. sb_end_pagefault(inode->i_sb);
  7927. extent_changeset_free(data_reserved);
  7928. return ret;
  7929. }
  7930. static int btrfs_truncate(struct inode *inode, bool skip_writeback)
  7931. {
  7932. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  7933. struct btrfs_root *root = BTRFS_I(inode)->root;
  7934. struct btrfs_block_rsv *rsv;
  7935. int ret;
  7936. struct btrfs_trans_handle *trans;
  7937. u64 mask = fs_info->sectorsize - 1;
  7938. u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
  7939. if (!skip_writeback) {
  7940. ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
  7941. (u64)-1);
  7942. if (ret)
  7943. return ret;
  7944. }
  7945. /*
  7946. * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
  7947. * things going on here:
  7948. *
  7949. * 1) We need to reserve space to update our inode.
  7950. *
  7951. * 2) We need to have something to cache all the space that is going to
  7952. * be free'd up by the truncate operation, but also have some slack
  7953. * space reserved in case it uses space during the truncate (thank you
  7954. * very much snapshotting).
  7955. *
  7956. * And we need these to be separate. The fact is we can use a lot of
  7957. * space doing the truncate, and we have no earthly idea how much space
  7958. * we will use, so we need the truncate reservation to be separate so it
  7959. * doesn't end up using space reserved for updating the inode. We also
  7960. * need to be able to stop the transaction and start a new one, which
  7961. * means we need to be able to update the inode several times, and we
  7962. * have no idea of knowing how many times that will be, so we can't just
  7963. * reserve 1 item for the entirety of the operation, so that has to be
  7964. * done separately as well.
  7965. *
  7966. * So that leaves us with
  7967. *
  7968. * 1) rsv - for the truncate reservation, which we will steal from the
  7969. * transaction reservation.
  7970. * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
  7971. * updating the inode.
  7972. */
  7973. rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
  7974. if (!rsv)
  7975. return -ENOMEM;
  7976. rsv->size = min_size;
  7977. rsv->failfast = 1;
  7978. /*
  7979. * 1 for the truncate slack space
  7980. * 1 for updating the inode.
  7981. */
  7982. trans = btrfs_start_transaction(root, 2);
  7983. if (IS_ERR(trans)) {
  7984. ret = PTR_ERR(trans);
  7985. goto out;
  7986. }
  7987. /* Migrate the slack space for the truncate to our reserve */
  7988. ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
  7989. min_size, 0);
  7990. BUG_ON(ret);
  7991. /*
  7992. * So if we truncate and then write and fsync we normally would just
  7993. * write the extents that changed, which is a problem if we need to
  7994. * first truncate that entire inode. So set this flag so we write out
  7995. * all of the extents in the inode to the sync log so we're completely
  7996. * safe.
  7997. */
  7998. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
  7999. trans->block_rsv = rsv;
  8000. while (1) {
  8001. ret = btrfs_truncate_inode_items(trans, root, inode,
  8002. inode->i_size,
  8003. BTRFS_EXTENT_DATA_KEY);
  8004. trans->block_rsv = &fs_info->trans_block_rsv;
  8005. if (ret != -ENOSPC && ret != -EAGAIN)
  8006. break;
  8007. ret = btrfs_update_inode(trans, root, inode);
  8008. if (ret)
  8009. break;
  8010. btrfs_end_transaction(trans);
  8011. btrfs_btree_balance_dirty(fs_info);
  8012. trans = btrfs_start_transaction(root, 2);
  8013. if (IS_ERR(trans)) {
  8014. ret = PTR_ERR(trans);
  8015. trans = NULL;
  8016. break;
  8017. }
  8018. btrfs_block_rsv_release(fs_info, rsv, -1);
  8019. ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
  8020. rsv, min_size, 0);
  8021. BUG_ON(ret); /* shouldn't happen */
  8022. trans->block_rsv = rsv;
  8023. }
  8024. /*
  8025. * We can't call btrfs_truncate_block inside a trans handle as we could
  8026. * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
  8027. * we've truncated everything except the last little bit, and can do
  8028. * btrfs_truncate_block and then update the disk_i_size.
  8029. */
  8030. if (ret == NEED_TRUNCATE_BLOCK) {
  8031. btrfs_end_transaction(trans);
  8032. btrfs_btree_balance_dirty(fs_info);
  8033. ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
  8034. if (ret)
  8035. goto out;
  8036. trans = btrfs_start_transaction(root, 1);
  8037. if (IS_ERR(trans)) {
  8038. ret = PTR_ERR(trans);
  8039. goto out;
  8040. }
  8041. btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
  8042. }
  8043. if (trans) {
  8044. int ret2;
  8045. trans->block_rsv = &fs_info->trans_block_rsv;
  8046. ret2 = btrfs_update_inode(trans, root, inode);
  8047. if (ret2 && !ret)
  8048. ret = ret2;
  8049. ret2 = btrfs_end_transaction(trans);
  8050. if (ret2 && !ret)
  8051. ret = ret2;
  8052. btrfs_btree_balance_dirty(fs_info);
  8053. }
  8054. out:
  8055. btrfs_free_block_rsv(fs_info, rsv);
  8056. return ret;
  8057. }
  8058. /*
  8059. * create a new subvolume directory/inode (helper for the ioctl).
  8060. */
  8061. int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
  8062. struct btrfs_root *new_root,
  8063. struct btrfs_root *parent_root,
  8064. u64 new_dirid)
  8065. {
  8066. struct inode *inode;
  8067. int err;
  8068. u64 index = 0;
  8069. inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
  8070. new_dirid, new_dirid,
  8071. S_IFDIR | (~current_umask() & S_IRWXUGO),
  8072. &index);
  8073. if (IS_ERR(inode))
  8074. return PTR_ERR(inode);
  8075. inode->i_op = &btrfs_dir_inode_operations;
  8076. inode->i_fop = &btrfs_dir_file_operations;
  8077. set_nlink(inode, 1);
  8078. btrfs_i_size_write(BTRFS_I(inode), 0);
  8079. unlock_new_inode(inode);
  8080. err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
  8081. if (err)
  8082. btrfs_err(new_root->fs_info,
  8083. "error inheriting subvolume %llu properties: %d",
  8084. new_root->root_key.objectid, err);
  8085. err = btrfs_update_inode(trans, new_root, inode);
  8086. iput(inode);
  8087. return err;
  8088. }
  8089. struct inode *btrfs_alloc_inode(struct super_block *sb)
  8090. {
  8091. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  8092. struct btrfs_inode *ei;
  8093. struct inode *inode;
  8094. ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
  8095. if (!ei)
  8096. return NULL;
  8097. ei->root = NULL;
  8098. ei->generation = 0;
  8099. ei->last_trans = 0;
  8100. ei->last_sub_trans = 0;
  8101. ei->logged_trans = 0;
  8102. ei->delalloc_bytes = 0;
  8103. ei->new_delalloc_bytes = 0;
  8104. ei->defrag_bytes = 0;
  8105. ei->disk_i_size = 0;
  8106. ei->flags = 0;
  8107. ei->csum_bytes = 0;
  8108. ei->index_cnt = (u64)-1;
  8109. ei->dir_index = 0;
  8110. ei->last_unlink_trans = 0;
  8111. ei->last_log_commit = 0;
  8112. spin_lock_init(&ei->lock);
  8113. ei->outstanding_extents = 0;
  8114. if (sb->s_magic != BTRFS_TEST_MAGIC)
  8115. btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
  8116. BTRFS_BLOCK_RSV_DELALLOC);
  8117. ei->runtime_flags = 0;
  8118. ei->prop_compress = BTRFS_COMPRESS_NONE;
  8119. ei->defrag_compress = BTRFS_COMPRESS_NONE;
  8120. ei->delayed_node = NULL;
  8121. ei->i_otime.tv_sec = 0;
  8122. ei->i_otime.tv_nsec = 0;
  8123. inode = &ei->vfs_inode;
  8124. extent_map_tree_init(&ei->extent_tree);
  8125. extent_io_tree_init(&ei->io_tree, inode);
  8126. extent_io_tree_init(&ei->io_failure_tree, inode);
  8127. ei->io_tree.track_uptodate = 1;
  8128. ei->io_failure_tree.track_uptodate = 1;
  8129. atomic_set(&ei->sync_writers, 0);
  8130. mutex_init(&ei->log_mutex);
  8131. mutex_init(&ei->delalloc_mutex);
  8132. btrfs_ordered_inode_tree_init(&ei->ordered_tree);
  8133. INIT_LIST_HEAD(&ei->delalloc_inodes);
  8134. INIT_LIST_HEAD(&ei->delayed_iput);
  8135. RB_CLEAR_NODE(&ei->rb_node);
  8136. init_rwsem(&ei->dio_sem);
  8137. return inode;
  8138. }
  8139. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  8140. void btrfs_test_destroy_inode(struct inode *inode)
  8141. {
  8142. btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
  8143. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  8144. }
  8145. #endif
  8146. static void btrfs_i_callback(struct rcu_head *head)
  8147. {
  8148. struct inode *inode = container_of(head, struct inode, i_rcu);
  8149. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  8150. }
  8151. void btrfs_destroy_inode(struct inode *inode)
  8152. {
  8153. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  8154. struct btrfs_ordered_extent *ordered;
  8155. struct btrfs_root *root = BTRFS_I(inode)->root;
  8156. WARN_ON(!hlist_empty(&inode->i_dentry));
  8157. WARN_ON(inode->i_data.nrpages);
  8158. WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
  8159. WARN_ON(BTRFS_I(inode)->block_rsv.size);
  8160. WARN_ON(BTRFS_I(inode)->outstanding_extents);
  8161. WARN_ON(BTRFS_I(inode)->delalloc_bytes);
  8162. WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
  8163. WARN_ON(BTRFS_I(inode)->csum_bytes);
  8164. WARN_ON(BTRFS_I(inode)->defrag_bytes);
  8165. /*
  8166. * This can happen where we create an inode, but somebody else also
  8167. * created the same inode and we need to destroy the one we already
  8168. * created.
  8169. */
  8170. if (!root)
  8171. goto free;
  8172. while (1) {
  8173. ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
  8174. if (!ordered)
  8175. break;
  8176. else {
  8177. btrfs_err(fs_info,
  8178. "found ordered extent %llu %llu on inode cleanup",
  8179. ordered->file_offset, ordered->len);
  8180. btrfs_remove_ordered_extent(inode, ordered);
  8181. btrfs_put_ordered_extent(ordered);
  8182. btrfs_put_ordered_extent(ordered);
  8183. }
  8184. }
  8185. btrfs_qgroup_check_reserved_leak(inode);
  8186. inode_tree_del(inode);
  8187. btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
  8188. free:
  8189. call_rcu(&inode->i_rcu, btrfs_i_callback);
  8190. }
  8191. int btrfs_drop_inode(struct inode *inode)
  8192. {
  8193. struct btrfs_root *root = BTRFS_I(inode)->root;
  8194. if (root == NULL)
  8195. return 1;
  8196. /* the snap/subvol tree is on deleting */
  8197. if (btrfs_root_refs(&root->root_item) == 0)
  8198. return 1;
  8199. else
  8200. return generic_drop_inode(inode);
  8201. }
  8202. static void init_once(void *foo)
  8203. {
  8204. struct btrfs_inode *ei = (struct btrfs_inode *) foo;
  8205. inode_init_once(&ei->vfs_inode);
  8206. }
  8207. void __cold btrfs_destroy_cachep(void)
  8208. {
  8209. /*
  8210. * Make sure all delayed rcu free inodes are flushed before we
  8211. * destroy cache.
  8212. */
  8213. rcu_barrier();
  8214. kmem_cache_destroy(btrfs_inode_cachep);
  8215. kmem_cache_destroy(btrfs_trans_handle_cachep);
  8216. kmem_cache_destroy(btrfs_path_cachep);
  8217. kmem_cache_destroy(btrfs_free_space_cachep);
  8218. }
  8219. int __init btrfs_init_cachep(void)
  8220. {
  8221. btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
  8222. sizeof(struct btrfs_inode), 0,
  8223. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
  8224. init_once);
  8225. if (!btrfs_inode_cachep)
  8226. goto fail;
  8227. btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
  8228. sizeof(struct btrfs_trans_handle), 0,
  8229. SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
  8230. if (!btrfs_trans_handle_cachep)
  8231. goto fail;
  8232. btrfs_path_cachep = kmem_cache_create("btrfs_path",
  8233. sizeof(struct btrfs_path), 0,
  8234. SLAB_MEM_SPREAD, NULL);
  8235. if (!btrfs_path_cachep)
  8236. goto fail;
  8237. btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
  8238. sizeof(struct btrfs_free_space), 0,
  8239. SLAB_MEM_SPREAD, NULL);
  8240. if (!btrfs_free_space_cachep)
  8241. goto fail;
  8242. return 0;
  8243. fail:
  8244. btrfs_destroy_cachep();
  8245. return -ENOMEM;
  8246. }
  8247. static int btrfs_getattr(const struct path *path, struct kstat *stat,
  8248. u32 request_mask, unsigned int flags)
  8249. {
  8250. u64 delalloc_bytes;
  8251. struct inode *inode = d_inode(path->dentry);
  8252. u32 blocksize = inode->i_sb->s_blocksize;
  8253. u32 bi_flags = BTRFS_I(inode)->flags;
  8254. stat->result_mask |= STATX_BTIME;
  8255. stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
  8256. stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
  8257. if (bi_flags & BTRFS_INODE_APPEND)
  8258. stat->attributes |= STATX_ATTR_APPEND;
  8259. if (bi_flags & BTRFS_INODE_COMPRESS)
  8260. stat->attributes |= STATX_ATTR_COMPRESSED;
  8261. if (bi_flags & BTRFS_INODE_IMMUTABLE)
  8262. stat->attributes |= STATX_ATTR_IMMUTABLE;
  8263. if (bi_flags & BTRFS_INODE_NODUMP)
  8264. stat->attributes |= STATX_ATTR_NODUMP;
  8265. stat->attributes_mask |= (STATX_ATTR_APPEND |
  8266. STATX_ATTR_COMPRESSED |
  8267. STATX_ATTR_IMMUTABLE |
  8268. STATX_ATTR_NODUMP);
  8269. generic_fillattr(inode, stat);
  8270. stat->dev = BTRFS_I(inode)->root->anon_dev;
  8271. spin_lock(&BTRFS_I(inode)->lock);
  8272. delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
  8273. spin_unlock(&BTRFS_I(inode)->lock);
  8274. stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
  8275. ALIGN(delalloc_bytes, blocksize)) >> 9;
  8276. return 0;
  8277. }
  8278. static int btrfs_rename_exchange(struct inode *old_dir,
  8279. struct dentry *old_dentry,
  8280. struct inode *new_dir,
  8281. struct dentry *new_dentry)
  8282. {
  8283. struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
  8284. struct btrfs_trans_handle *trans;
  8285. struct btrfs_root *root = BTRFS_I(old_dir)->root;
  8286. struct btrfs_root *dest = BTRFS_I(new_dir)->root;
  8287. struct inode *new_inode = new_dentry->d_inode;
  8288. struct inode *old_inode = old_dentry->d_inode;
  8289. struct timespec64 ctime = current_time(old_inode);
  8290. struct dentry *parent;
  8291. u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
  8292. u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
  8293. u64 old_idx = 0;
  8294. u64 new_idx = 0;
  8295. u64 root_objectid;
  8296. int ret;
  8297. int ret2;
  8298. bool root_log_pinned = false;
  8299. bool dest_log_pinned = false;
  8300. /* we only allow rename subvolume link between subvolumes */
  8301. if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
  8302. return -EXDEV;
  8303. /* close the race window with snapshot create/destroy ioctl */
  8304. if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
  8305. down_read(&fs_info->subvol_sem);
  8306. if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
  8307. down_read(&fs_info->subvol_sem);
  8308. /*
  8309. * We want to reserve the absolute worst case amount of items. So if
  8310. * both inodes are subvols and we need to unlink them then that would
  8311. * require 4 item modifications, but if they are both normal inodes it
  8312. * would require 5 item modifications, so we'll assume their normal
  8313. * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
  8314. * should cover the worst case number of items we'll modify.
  8315. */
  8316. trans = btrfs_start_transaction(root, 12);
  8317. if (IS_ERR(trans)) {
  8318. ret = PTR_ERR(trans);
  8319. goto out_notrans;
  8320. }
  8321. /*
  8322. * We need to find a free sequence number both in the source and
  8323. * in the destination directory for the exchange.
  8324. */
  8325. ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
  8326. if (ret)
  8327. goto out_fail;
  8328. ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
  8329. if (ret)
  8330. goto out_fail;
  8331. BTRFS_I(old_inode)->dir_index = 0ULL;
  8332. BTRFS_I(new_inode)->dir_index = 0ULL;
  8333. /* Reference for the source. */
  8334. if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
  8335. /* force full log commit if subvolume involved. */
  8336. btrfs_set_log_full_commit(fs_info, trans);
  8337. } else {
  8338. btrfs_pin_log_trans(root);
  8339. root_log_pinned = true;
  8340. ret = btrfs_insert_inode_ref(trans, dest,
  8341. new_dentry->d_name.name,
  8342. new_dentry->d_name.len,
  8343. old_ino,
  8344. btrfs_ino(BTRFS_I(new_dir)),
  8345. old_idx);
  8346. if (ret)
  8347. goto out_fail;
  8348. }
  8349. /* And now for the dest. */
  8350. if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
  8351. /* force full log commit if subvolume involved. */
  8352. btrfs_set_log_full_commit(fs_info, trans);
  8353. } else {
  8354. btrfs_pin_log_trans(dest);
  8355. dest_log_pinned = true;
  8356. ret = btrfs_insert_inode_ref(trans, root,
  8357. old_dentry->d_name.name,
  8358. old_dentry->d_name.len,
  8359. new_ino,
  8360. btrfs_ino(BTRFS_I(old_dir)),
  8361. new_idx);
  8362. if (ret)
  8363. goto out_fail;
  8364. }
  8365. /* Update inode version and ctime/mtime. */
  8366. inode_inc_iversion(old_dir);
  8367. inode_inc_iversion(new_dir);
  8368. inode_inc_iversion(old_inode);
  8369. inode_inc_iversion(new_inode);
  8370. old_dir->i_ctime = old_dir->i_mtime = ctime;
  8371. new_dir->i_ctime = new_dir->i_mtime = ctime;
  8372. old_inode->i_ctime = ctime;
  8373. new_inode->i_ctime = ctime;
  8374. if (old_dentry->d_parent != new_dentry->d_parent) {
  8375. btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
  8376. BTRFS_I(old_inode), 1);
  8377. btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
  8378. BTRFS_I(new_inode), 1);
  8379. }
  8380. /* src is a subvolume */
  8381. if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
  8382. root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
  8383. ret = btrfs_unlink_subvol(trans, root, old_dir,
  8384. root_objectid,
  8385. old_dentry->d_name.name,
  8386. old_dentry->d_name.len);
  8387. } else { /* src is an inode */
  8388. ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
  8389. BTRFS_I(old_dentry->d_inode),
  8390. old_dentry->d_name.name,
  8391. old_dentry->d_name.len);
  8392. if (!ret)
  8393. ret = btrfs_update_inode(trans, root, old_inode);
  8394. }
  8395. if (ret) {
  8396. btrfs_abort_transaction(trans, ret);
  8397. goto out_fail;
  8398. }
  8399. /* dest is a subvolume */
  8400. if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
  8401. root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
  8402. ret = btrfs_unlink_subvol(trans, dest, new_dir,
  8403. root_objectid,
  8404. new_dentry->d_name.name,
  8405. new_dentry->d_name.len);
  8406. } else { /* dest is an inode */
  8407. ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
  8408. BTRFS_I(new_dentry->d_inode),
  8409. new_dentry->d_name.name,
  8410. new_dentry->d_name.len);
  8411. if (!ret)
  8412. ret = btrfs_update_inode(trans, dest, new_inode);
  8413. }
  8414. if (ret) {
  8415. btrfs_abort_transaction(trans, ret);
  8416. goto out_fail;
  8417. }
  8418. ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
  8419. new_dentry->d_name.name,
  8420. new_dentry->d_name.len, 0, old_idx);
  8421. if (ret) {
  8422. btrfs_abort_transaction(trans, ret);
  8423. goto out_fail;
  8424. }
  8425. ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
  8426. old_dentry->d_name.name,
  8427. old_dentry->d_name.len, 0, new_idx);
  8428. if (ret) {
  8429. btrfs_abort_transaction(trans, ret);
  8430. goto out_fail;
  8431. }
  8432. if (old_inode->i_nlink == 1)
  8433. BTRFS_I(old_inode)->dir_index = old_idx;
  8434. if (new_inode->i_nlink == 1)
  8435. BTRFS_I(new_inode)->dir_index = new_idx;
  8436. if (root_log_pinned) {
  8437. parent = new_dentry->d_parent;
  8438. btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
  8439. parent);
  8440. btrfs_end_log_trans(root);
  8441. root_log_pinned = false;
  8442. }
  8443. if (dest_log_pinned) {
  8444. parent = old_dentry->d_parent;
  8445. btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
  8446. parent);
  8447. btrfs_end_log_trans(dest);
  8448. dest_log_pinned = false;
  8449. }
  8450. out_fail:
  8451. /*
  8452. * If we have pinned a log and an error happened, we unpin tasks
  8453. * trying to sync the log and force them to fallback to a transaction
  8454. * commit if the log currently contains any of the inodes involved in
  8455. * this rename operation (to ensure we do not persist a log with an
  8456. * inconsistent state for any of these inodes or leading to any
  8457. * inconsistencies when replayed). If the transaction was aborted, the
  8458. * abortion reason is propagated to userspace when attempting to commit
  8459. * the transaction. If the log does not contain any of these inodes, we
  8460. * allow the tasks to sync it.
  8461. */
  8462. if (ret && (root_log_pinned || dest_log_pinned)) {
  8463. if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
  8464. btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
  8465. btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
  8466. (new_inode &&
  8467. btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
  8468. btrfs_set_log_full_commit(fs_info, trans);
  8469. if (root_log_pinned) {
  8470. btrfs_end_log_trans(root);
  8471. root_log_pinned = false;
  8472. }
  8473. if (dest_log_pinned) {
  8474. btrfs_end_log_trans(dest);
  8475. dest_log_pinned = false;
  8476. }
  8477. }
  8478. ret2 = btrfs_end_transaction(trans);
  8479. ret = ret ? ret : ret2;
  8480. out_notrans:
  8481. if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
  8482. up_read(&fs_info->subvol_sem);
  8483. if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
  8484. up_read(&fs_info->subvol_sem);
  8485. return ret;
  8486. }
  8487. static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
  8488. struct btrfs_root *root,
  8489. struct inode *dir,
  8490. struct dentry *dentry)
  8491. {
  8492. int ret;
  8493. struct inode *inode;
  8494. u64 objectid;
  8495. u64 index;
  8496. ret = btrfs_find_free_ino(root, &objectid);
  8497. if (ret)
  8498. return ret;
  8499. inode = btrfs_new_inode(trans, root, dir,
  8500. dentry->d_name.name,
  8501. dentry->d_name.len,
  8502. btrfs_ino(BTRFS_I(dir)),
  8503. objectid,
  8504. S_IFCHR | WHITEOUT_MODE,
  8505. &index);
  8506. if (IS_ERR(inode)) {
  8507. ret = PTR_ERR(inode);
  8508. return ret;
  8509. }
  8510. inode->i_op = &btrfs_special_inode_operations;
  8511. init_special_inode(inode, inode->i_mode,
  8512. WHITEOUT_DEV);
  8513. ret = btrfs_init_inode_security(trans, inode, dir,
  8514. &dentry->d_name);
  8515. if (ret)
  8516. goto out;
  8517. ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
  8518. BTRFS_I(inode), 0, index);
  8519. if (ret)
  8520. goto out;
  8521. ret = btrfs_update_inode(trans, root, inode);
  8522. out:
  8523. unlock_new_inode(inode);
  8524. if (ret)
  8525. inode_dec_link_count(inode);
  8526. iput(inode);
  8527. return ret;
  8528. }
  8529. static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  8530. struct inode *new_dir, struct dentry *new_dentry,
  8531. unsigned int flags)
  8532. {
  8533. struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
  8534. struct btrfs_trans_handle *trans;
  8535. unsigned int trans_num_items;
  8536. struct btrfs_root *root = BTRFS_I(old_dir)->root;
  8537. struct btrfs_root *dest = BTRFS_I(new_dir)->root;
  8538. struct inode *new_inode = d_inode(new_dentry);
  8539. struct inode *old_inode = d_inode(old_dentry);
  8540. u64 index = 0;
  8541. u64 root_objectid;
  8542. int ret;
  8543. u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
  8544. bool log_pinned = false;
  8545. if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
  8546. return -EPERM;
  8547. /* we only allow rename subvolume link between subvolumes */
  8548. if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
  8549. return -EXDEV;
  8550. if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
  8551. (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
  8552. return -ENOTEMPTY;
  8553. if (S_ISDIR(old_inode->i_mode) && new_inode &&
  8554. new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
  8555. return -ENOTEMPTY;
  8556. /* check for collisions, even if the name isn't there */
  8557. ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
  8558. new_dentry->d_name.name,
  8559. new_dentry->d_name.len);
  8560. if (ret) {
  8561. if (ret == -EEXIST) {
  8562. /* we shouldn't get
  8563. * eexist without a new_inode */
  8564. if (WARN_ON(!new_inode)) {
  8565. return ret;
  8566. }
  8567. } else {
  8568. /* maybe -EOVERFLOW */
  8569. return ret;
  8570. }
  8571. }
  8572. ret = 0;
  8573. /*
  8574. * we're using rename to replace one file with another. Start IO on it
  8575. * now so we don't add too much work to the end of the transaction
  8576. */
  8577. if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
  8578. filemap_flush(old_inode->i_mapping);
  8579. /* close the racy window with snapshot create/destroy ioctl */
  8580. if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
  8581. down_read(&fs_info->subvol_sem);
  8582. /*
  8583. * We want to reserve the absolute worst case amount of items. So if
  8584. * both inodes are subvols and we need to unlink them then that would
  8585. * require 4 item modifications, but if they are both normal inodes it
  8586. * would require 5 item modifications, so we'll assume they are normal
  8587. * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
  8588. * should cover the worst case number of items we'll modify.
  8589. * If our rename has the whiteout flag, we need more 5 units for the
  8590. * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
  8591. * when selinux is enabled).
  8592. */
  8593. trans_num_items = 11;
  8594. if (flags & RENAME_WHITEOUT)
  8595. trans_num_items += 5;
  8596. trans = btrfs_start_transaction(root, trans_num_items);
  8597. if (IS_ERR(trans)) {
  8598. ret = PTR_ERR(trans);
  8599. goto out_notrans;
  8600. }
  8601. if (dest != root)
  8602. btrfs_record_root_in_trans(trans, dest);
  8603. ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
  8604. if (ret)
  8605. goto out_fail;
  8606. BTRFS_I(old_inode)->dir_index = 0ULL;
  8607. if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  8608. /* force full log commit if subvolume involved. */
  8609. btrfs_set_log_full_commit(fs_info, trans);
  8610. } else {
  8611. btrfs_pin_log_trans(root);
  8612. log_pinned = true;
  8613. ret = btrfs_insert_inode_ref(trans, dest,
  8614. new_dentry->d_name.name,
  8615. new_dentry->d_name.len,
  8616. old_ino,
  8617. btrfs_ino(BTRFS_I(new_dir)), index);
  8618. if (ret)
  8619. goto out_fail;
  8620. }
  8621. inode_inc_iversion(old_dir);
  8622. inode_inc_iversion(new_dir);
  8623. inode_inc_iversion(old_inode);
  8624. old_dir->i_ctime = old_dir->i_mtime =
  8625. new_dir->i_ctime = new_dir->i_mtime =
  8626. old_inode->i_ctime = current_time(old_dir);
  8627. if (old_dentry->d_parent != new_dentry->d_parent)
  8628. btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
  8629. BTRFS_I(old_inode), 1);
  8630. if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  8631. root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
  8632. ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
  8633. old_dentry->d_name.name,
  8634. old_dentry->d_name.len);
  8635. } else {
  8636. ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
  8637. BTRFS_I(d_inode(old_dentry)),
  8638. old_dentry->d_name.name,
  8639. old_dentry->d_name.len);
  8640. if (!ret)
  8641. ret = btrfs_update_inode(trans, root, old_inode);
  8642. }
  8643. if (ret) {
  8644. btrfs_abort_transaction(trans, ret);
  8645. goto out_fail;
  8646. }
  8647. if (new_inode) {
  8648. inode_inc_iversion(new_inode);
  8649. new_inode->i_ctime = current_time(new_inode);
  8650. if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
  8651. BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
  8652. root_objectid = BTRFS_I(new_inode)->location.objectid;
  8653. ret = btrfs_unlink_subvol(trans, dest, new_dir,
  8654. root_objectid,
  8655. new_dentry->d_name.name,
  8656. new_dentry->d_name.len);
  8657. BUG_ON(new_inode->i_nlink == 0);
  8658. } else {
  8659. ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
  8660. BTRFS_I(d_inode(new_dentry)),
  8661. new_dentry->d_name.name,
  8662. new_dentry->d_name.len);
  8663. }
  8664. if (!ret && new_inode->i_nlink == 0)
  8665. ret = btrfs_orphan_add(trans,
  8666. BTRFS_I(d_inode(new_dentry)));
  8667. if (ret) {
  8668. btrfs_abort_transaction(trans, ret);
  8669. goto out_fail;
  8670. }
  8671. }
  8672. ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
  8673. new_dentry->d_name.name,
  8674. new_dentry->d_name.len, 0, index);
  8675. if (ret) {
  8676. btrfs_abort_transaction(trans, ret);
  8677. goto out_fail;
  8678. }
  8679. if (old_inode->i_nlink == 1)
  8680. BTRFS_I(old_inode)->dir_index = index;
  8681. if (log_pinned) {
  8682. struct dentry *parent = new_dentry->d_parent;
  8683. btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
  8684. parent);
  8685. btrfs_end_log_trans(root);
  8686. log_pinned = false;
  8687. }
  8688. if (flags & RENAME_WHITEOUT) {
  8689. ret = btrfs_whiteout_for_rename(trans, root, old_dir,
  8690. old_dentry);
  8691. if (ret) {
  8692. btrfs_abort_transaction(trans, ret);
  8693. goto out_fail;
  8694. }
  8695. }
  8696. out_fail:
  8697. /*
  8698. * If we have pinned the log and an error happened, we unpin tasks
  8699. * trying to sync the log and force them to fallback to a transaction
  8700. * commit if the log currently contains any of the inodes involved in
  8701. * this rename operation (to ensure we do not persist a log with an
  8702. * inconsistent state for any of these inodes or leading to any
  8703. * inconsistencies when replayed). If the transaction was aborted, the
  8704. * abortion reason is propagated to userspace when attempting to commit
  8705. * the transaction. If the log does not contain any of these inodes, we
  8706. * allow the tasks to sync it.
  8707. */
  8708. if (ret && log_pinned) {
  8709. if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
  8710. btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
  8711. btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
  8712. (new_inode &&
  8713. btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
  8714. btrfs_set_log_full_commit(fs_info, trans);
  8715. btrfs_end_log_trans(root);
  8716. log_pinned = false;
  8717. }
  8718. btrfs_end_transaction(trans);
  8719. out_notrans:
  8720. if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
  8721. up_read(&fs_info->subvol_sem);
  8722. return ret;
  8723. }
  8724. static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
  8725. struct inode *new_dir, struct dentry *new_dentry,
  8726. unsigned int flags)
  8727. {
  8728. if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
  8729. return -EINVAL;
  8730. if (flags & RENAME_EXCHANGE)
  8731. return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
  8732. new_dentry);
  8733. return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
  8734. }
  8735. struct btrfs_delalloc_work {
  8736. struct inode *inode;
  8737. struct completion completion;
  8738. struct list_head list;
  8739. struct btrfs_work work;
  8740. };
  8741. static void btrfs_run_delalloc_work(struct btrfs_work *work)
  8742. {
  8743. struct btrfs_delalloc_work *delalloc_work;
  8744. struct inode *inode;
  8745. delalloc_work = container_of(work, struct btrfs_delalloc_work,
  8746. work);
  8747. inode = delalloc_work->inode;
  8748. filemap_flush(inode->i_mapping);
  8749. if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
  8750. &BTRFS_I(inode)->runtime_flags))
  8751. filemap_flush(inode->i_mapping);
  8752. iput(inode);
  8753. complete(&delalloc_work->completion);
  8754. }
  8755. static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
  8756. {
  8757. struct btrfs_delalloc_work *work;
  8758. work = kmalloc(sizeof(*work), GFP_NOFS);
  8759. if (!work)
  8760. return NULL;
  8761. init_completion(&work->completion);
  8762. INIT_LIST_HEAD(&work->list);
  8763. work->inode = inode;
  8764. WARN_ON_ONCE(!inode);
  8765. btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
  8766. btrfs_run_delalloc_work, NULL, NULL);
  8767. return work;
  8768. }
  8769. /*
  8770. * some fairly slow code that needs optimization. This walks the list
  8771. * of all the inodes with pending delalloc and forces them to disk.
  8772. */
  8773. static int start_delalloc_inodes(struct btrfs_root *root, int nr)
  8774. {
  8775. struct btrfs_inode *binode;
  8776. struct inode *inode;
  8777. struct btrfs_delalloc_work *work, *next;
  8778. struct list_head works;
  8779. struct list_head splice;
  8780. int ret = 0;
  8781. INIT_LIST_HEAD(&works);
  8782. INIT_LIST_HEAD(&splice);
  8783. mutex_lock(&root->delalloc_mutex);
  8784. spin_lock(&root->delalloc_lock);
  8785. list_splice_init(&root->delalloc_inodes, &splice);
  8786. while (!list_empty(&splice)) {
  8787. binode = list_entry(splice.next, struct btrfs_inode,
  8788. delalloc_inodes);
  8789. list_move_tail(&binode->delalloc_inodes,
  8790. &root->delalloc_inodes);
  8791. inode = igrab(&binode->vfs_inode);
  8792. if (!inode) {
  8793. cond_resched_lock(&root->delalloc_lock);
  8794. continue;
  8795. }
  8796. spin_unlock(&root->delalloc_lock);
  8797. work = btrfs_alloc_delalloc_work(inode);
  8798. if (!work) {
  8799. iput(inode);
  8800. ret = -ENOMEM;
  8801. goto out;
  8802. }
  8803. list_add_tail(&work->list, &works);
  8804. btrfs_queue_work(root->fs_info->flush_workers,
  8805. &work->work);
  8806. ret++;
  8807. if (nr != -1 && ret >= nr)
  8808. goto out;
  8809. cond_resched();
  8810. spin_lock(&root->delalloc_lock);
  8811. }
  8812. spin_unlock(&root->delalloc_lock);
  8813. out:
  8814. list_for_each_entry_safe(work, next, &works, list) {
  8815. list_del_init(&work->list);
  8816. wait_for_completion(&work->completion);
  8817. kfree(work);
  8818. }
  8819. if (!list_empty(&splice)) {
  8820. spin_lock(&root->delalloc_lock);
  8821. list_splice_tail(&splice, &root->delalloc_inodes);
  8822. spin_unlock(&root->delalloc_lock);
  8823. }
  8824. mutex_unlock(&root->delalloc_mutex);
  8825. return ret;
  8826. }
  8827. int btrfs_start_delalloc_inodes(struct btrfs_root *root)
  8828. {
  8829. struct btrfs_fs_info *fs_info = root->fs_info;
  8830. int ret;
  8831. if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
  8832. return -EROFS;
  8833. ret = start_delalloc_inodes(root, -1);
  8834. if (ret > 0)
  8835. ret = 0;
  8836. return ret;
  8837. }
  8838. int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr)
  8839. {
  8840. struct btrfs_root *root;
  8841. struct list_head splice;
  8842. int ret;
  8843. if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
  8844. return -EROFS;
  8845. INIT_LIST_HEAD(&splice);
  8846. mutex_lock(&fs_info->delalloc_root_mutex);
  8847. spin_lock(&fs_info->delalloc_root_lock);
  8848. list_splice_init(&fs_info->delalloc_roots, &splice);
  8849. while (!list_empty(&splice) && nr) {
  8850. root = list_first_entry(&splice, struct btrfs_root,
  8851. delalloc_root);
  8852. root = btrfs_grab_fs_root(root);
  8853. BUG_ON(!root);
  8854. list_move_tail(&root->delalloc_root,
  8855. &fs_info->delalloc_roots);
  8856. spin_unlock(&fs_info->delalloc_root_lock);
  8857. ret = start_delalloc_inodes(root, nr);
  8858. btrfs_put_fs_root(root);
  8859. if (ret < 0)
  8860. goto out;
  8861. if (nr != -1) {
  8862. nr -= ret;
  8863. WARN_ON(nr < 0);
  8864. }
  8865. spin_lock(&fs_info->delalloc_root_lock);
  8866. }
  8867. spin_unlock(&fs_info->delalloc_root_lock);
  8868. ret = 0;
  8869. out:
  8870. if (!list_empty(&splice)) {
  8871. spin_lock(&fs_info->delalloc_root_lock);
  8872. list_splice_tail(&splice, &fs_info->delalloc_roots);
  8873. spin_unlock(&fs_info->delalloc_root_lock);
  8874. }
  8875. mutex_unlock(&fs_info->delalloc_root_mutex);
  8876. return ret;
  8877. }
  8878. static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
  8879. const char *symname)
  8880. {
  8881. struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
  8882. struct btrfs_trans_handle *trans;
  8883. struct btrfs_root *root = BTRFS_I(dir)->root;
  8884. struct btrfs_path *path;
  8885. struct btrfs_key key;
  8886. struct inode *inode = NULL;
  8887. int err;
  8888. int drop_inode = 0;
  8889. u64 objectid;
  8890. u64 index = 0;
  8891. int name_len;
  8892. int datasize;
  8893. unsigned long ptr;
  8894. struct btrfs_file_extent_item *ei;
  8895. struct extent_buffer *leaf;
  8896. name_len = strlen(symname);
  8897. if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
  8898. return -ENAMETOOLONG;
  8899. /*
  8900. * 2 items for inode item and ref
  8901. * 2 items for dir items
  8902. * 1 item for updating parent inode item
  8903. * 1 item for the inline extent item
  8904. * 1 item for xattr if selinux is on
  8905. */
  8906. trans = btrfs_start_transaction(root, 7);
  8907. if (IS_ERR(trans))
  8908. return PTR_ERR(trans);
  8909. err = btrfs_find_free_ino(root, &objectid);
  8910. if (err)
  8911. goto out_unlock;
  8912. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  8913. dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
  8914. objectid, S_IFLNK|S_IRWXUGO, &index);
  8915. if (IS_ERR(inode)) {
  8916. err = PTR_ERR(inode);
  8917. goto out_unlock;
  8918. }
  8919. /*
  8920. * If the active LSM wants to access the inode during
  8921. * d_instantiate it needs these. Smack checks to see
  8922. * if the filesystem supports xattrs by looking at the
  8923. * ops vector.
  8924. */
  8925. inode->i_fop = &btrfs_file_operations;
  8926. inode->i_op = &btrfs_file_inode_operations;
  8927. inode->i_mapping->a_ops = &btrfs_aops;
  8928. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  8929. err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
  8930. if (err)
  8931. goto out_unlock_inode;
  8932. path = btrfs_alloc_path();
  8933. if (!path) {
  8934. err = -ENOMEM;
  8935. goto out_unlock_inode;
  8936. }
  8937. key.objectid = btrfs_ino(BTRFS_I(inode));
  8938. key.offset = 0;
  8939. key.type = BTRFS_EXTENT_DATA_KEY;
  8940. datasize = btrfs_file_extent_calc_inline_size(name_len);
  8941. err = btrfs_insert_empty_item(trans, root, path, &key,
  8942. datasize);
  8943. if (err) {
  8944. btrfs_free_path(path);
  8945. goto out_unlock_inode;
  8946. }
  8947. leaf = path->nodes[0];
  8948. ei = btrfs_item_ptr(leaf, path->slots[0],
  8949. struct btrfs_file_extent_item);
  8950. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  8951. btrfs_set_file_extent_type(leaf, ei,
  8952. BTRFS_FILE_EXTENT_INLINE);
  8953. btrfs_set_file_extent_encryption(leaf, ei, 0);
  8954. btrfs_set_file_extent_compression(leaf, ei, 0);
  8955. btrfs_set_file_extent_other_encoding(leaf, ei, 0);
  8956. btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
  8957. ptr = btrfs_file_extent_inline_start(ei);
  8958. write_extent_buffer(leaf, symname, ptr, name_len);
  8959. btrfs_mark_buffer_dirty(leaf);
  8960. btrfs_free_path(path);
  8961. inode->i_op = &btrfs_symlink_inode_operations;
  8962. inode_nohighmem(inode);
  8963. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  8964. inode_set_bytes(inode, name_len);
  8965. btrfs_i_size_write(BTRFS_I(inode), name_len);
  8966. err = btrfs_update_inode(trans, root, inode);
  8967. /*
  8968. * Last step, add directory indexes for our symlink inode. This is the
  8969. * last step to avoid extra cleanup of these indexes if an error happens
  8970. * elsewhere above.
  8971. */
  8972. if (!err)
  8973. err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
  8974. BTRFS_I(inode), 0, index);
  8975. if (err) {
  8976. drop_inode = 1;
  8977. goto out_unlock_inode;
  8978. }
  8979. d_instantiate_new(dentry, inode);
  8980. out_unlock:
  8981. btrfs_end_transaction(trans);
  8982. if (drop_inode) {
  8983. inode_dec_link_count(inode);
  8984. iput(inode);
  8985. }
  8986. btrfs_btree_balance_dirty(fs_info);
  8987. return err;
  8988. out_unlock_inode:
  8989. drop_inode = 1;
  8990. unlock_new_inode(inode);
  8991. goto out_unlock;
  8992. }
  8993. static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
  8994. u64 start, u64 num_bytes, u64 min_size,
  8995. loff_t actual_len, u64 *alloc_hint,
  8996. struct btrfs_trans_handle *trans)
  8997. {
  8998. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  8999. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  9000. struct extent_map *em;
  9001. struct btrfs_root *root = BTRFS_I(inode)->root;
  9002. struct btrfs_key ins;
  9003. u64 cur_offset = start;
  9004. u64 i_size;
  9005. u64 cur_bytes;
  9006. u64 last_alloc = (u64)-1;
  9007. int ret = 0;
  9008. bool own_trans = true;
  9009. u64 end = start + num_bytes - 1;
  9010. if (trans)
  9011. own_trans = false;
  9012. while (num_bytes > 0) {
  9013. if (own_trans) {
  9014. trans = btrfs_start_transaction(root, 3);
  9015. if (IS_ERR(trans)) {
  9016. ret = PTR_ERR(trans);
  9017. break;
  9018. }
  9019. }
  9020. cur_bytes = min_t(u64, num_bytes, SZ_256M);
  9021. cur_bytes = max(cur_bytes, min_size);
  9022. /*
  9023. * If we are severely fragmented we could end up with really
  9024. * small allocations, so if the allocator is returning small
  9025. * chunks lets make its job easier by only searching for those
  9026. * sized chunks.
  9027. */
  9028. cur_bytes = min(cur_bytes, last_alloc);
  9029. ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
  9030. min_size, 0, *alloc_hint, &ins, 1, 0);
  9031. if (ret) {
  9032. if (own_trans)
  9033. btrfs_end_transaction(trans);
  9034. break;
  9035. }
  9036. btrfs_dec_block_group_reservations(fs_info, ins.objectid);
  9037. last_alloc = ins.offset;
  9038. ret = insert_reserved_file_extent(trans, inode,
  9039. cur_offset, ins.objectid,
  9040. ins.offset, ins.offset,
  9041. ins.offset, 0, 0, 0,
  9042. BTRFS_FILE_EXTENT_PREALLOC);
  9043. if (ret) {
  9044. btrfs_free_reserved_extent(fs_info, ins.objectid,
  9045. ins.offset, 0);
  9046. btrfs_abort_transaction(trans, ret);
  9047. if (own_trans)
  9048. btrfs_end_transaction(trans);
  9049. break;
  9050. }
  9051. btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
  9052. cur_offset + ins.offset -1, 0);
  9053. em = alloc_extent_map();
  9054. if (!em) {
  9055. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  9056. &BTRFS_I(inode)->runtime_flags);
  9057. goto next;
  9058. }
  9059. em->start = cur_offset;
  9060. em->orig_start = cur_offset;
  9061. em->len = ins.offset;
  9062. em->block_start = ins.objectid;
  9063. em->block_len = ins.offset;
  9064. em->orig_block_len = ins.offset;
  9065. em->ram_bytes = ins.offset;
  9066. em->bdev = fs_info->fs_devices->latest_bdev;
  9067. set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
  9068. em->generation = trans->transid;
  9069. while (1) {
  9070. write_lock(&em_tree->lock);
  9071. ret = add_extent_mapping(em_tree, em, 1);
  9072. write_unlock(&em_tree->lock);
  9073. if (ret != -EEXIST)
  9074. break;
  9075. btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
  9076. cur_offset + ins.offset - 1,
  9077. 0);
  9078. }
  9079. free_extent_map(em);
  9080. next:
  9081. num_bytes -= ins.offset;
  9082. cur_offset += ins.offset;
  9083. *alloc_hint = ins.objectid + ins.offset;
  9084. inode_inc_iversion(inode);
  9085. inode->i_ctime = current_time(inode);
  9086. BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
  9087. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  9088. (actual_len > inode->i_size) &&
  9089. (cur_offset > inode->i_size)) {
  9090. if (cur_offset > actual_len)
  9091. i_size = actual_len;
  9092. else
  9093. i_size = cur_offset;
  9094. i_size_write(inode, i_size);
  9095. btrfs_ordered_update_i_size(inode, i_size, NULL);
  9096. }
  9097. ret = btrfs_update_inode(trans, root, inode);
  9098. if (ret) {
  9099. btrfs_abort_transaction(trans, ret);
  9100. if (own_trans)
  9101. btrfs_end_transaction(trans);
  9102. break;
  9103. }
  9104. if (own_trans)
  9105. btrfs_end_transaction(trans);
  9106. }
  9107. if (cur_offset < end)
  9108. btrfs_free_reserved_data_space(inode, NULL, cur_offset,
  9109. end - cur_offset + 1);
  9110. return ret;
  9111. }
  9112. int btrfs_prealloc_file_range(struct inode *inode, int mode,
  9113. u64 start, u64 num_bytes, u64 min_size,
  9114. loff_t actual_len, u64 *alloc_hint)
  9115. {
  9116. return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
  9117. min_size, actual_len, alloc_hint,
  9118. NULL);
  9119. }
  9120. int btrfs_prealloc_file_range_trans(struct inode *inode,
  9121. struct btrfs_trans_handle *trans, int mode,
  9122. u64 start, u64 num_bytes, u64 min_size,
  9123. loff_t actual_len, u64 *alloc_hint)
  9124. {
  9125. return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
  9126. min_size, actual_len, alloc_hint, trans);
  9127. }
  9128. static int btrfs_set_page_dirty(struct page *page)
  9129. {
  9130. return __set_page_dirty_nobuffers(page);
  9131. }
  9132. static int btrfs_permission(struct inode *inode, int mask)
  9133. {
  9134. struct btrfs_root *root = BTRFS_I(inode)->root;
  9135. umode_t mode = inode->i_mode;
  9136. if (mask & MAY_WRITE &&
  9137. (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
  9138. if (btrfs_root_readonly(root))
  9139. return -EROFS;
  9140. if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
  9141. return -EACCES;
  9142. }
  9143. return generic_permission(inode, mask);
  9144. }
  9145. static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
  9146. {
  9147. struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
  9148. struct btrfs_trans_handle *trans;
  9149. struct btrfs_root *root = BTRFS_I(dir)->root;
  9150. struct inode *inode = NULL;
  9151. u64 objectid;
  9152. u64 index;
  9153. int ret = 0;
  9154. /*
  9155. * 5 units required for adding orphan entry
  9156. */
  9157. trans = btrfs_start_transaction(root, 5);
  9158. if (IS_ERR(trans))
  9159. return PTR_ERR(trans);
  9160. ret = btrfs_find_free_ino(root, &objectid);
  9161. if (ret)
  9162. goto out;
  9163. inode = btrfs_new_inode(trans, root, dir, NULL, 0,
  9164. btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
  9165. if (IS_ERR(inode)) {
  9166. ret = PTR_ERR(inode);
  9167. inode = NULL;
  9168. goto out;
  9169. }
  9170. inode->i_fop = &btrfs_file_operations;
  9171. inode->i_op = &btrfs_file_inode_operations;
  9172. inode->i_mapping->a_ops = &btrfs_aops;
  9173. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  9174. ret = btrfs_init_inode_security(trans, inode, dir, NULL);
  9175. if (ret)
  9176. goto out_inode;
  9177. ret = btrfs_update_inode(trans, root, inode);
  9178. if (ret)
  9179. goto out_inode;
  9180. ret = btrfs_orphan_add(trans, BTRFS_I(inode));
  9181. if (ret)
  9182. goto out_inode;
  9183. /*
  9184. * We set number of links to 0 in btrfs_new_inode(), and here we set
  9185. * it to 1 because d_tmpfile() will issue a warning if the count is 0,
  9186. * through:
  9187. *
  9188. * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
  9189. */
  9190. set_nlink(inode, 1);
  9191. unlock_new_inode(inode);
  9192. d_tmpfile(dentry, inode);
  9193. mark_inode_dirty(inode);
  9194. out:
  9195. btrfs_end_transaction(trans);
  9196. if (ret)
  9197. iput(inode);
  9198. btrfs_btree_balance_dirty(fs_info);
  9199. return ret;
  9200. out_inode:
  9201. unlock_new_inode(inode);
  9202. goto out;
  9203. }
  9204. __attribute__((const))
  9205. static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror)
  9206. {
  9207. return -EAGAIN;
  9208. }
  9209. static void btrfs_check_extent_io_range(void *private_data, const char *caller,
  9210. u64 start, u64 end)
  9211. {
  9212. struct inode *inode = private_data;
  9213. u64 isize;
  9214. isize = i_size_read(inode);
  9215. if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
  9216. btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
  9217. "%s: ino %llu isize %llu odd range [%llu,%llu]",
  9218. caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
  9219. }
  9220. }
  9221. void btrfs_set_range_writeback(void *private_data, u64 start, u64 end)
  9222. {
  9223. struct inode *inode = private_data;
  9224. unsigned long index = start >> PAGE_SHIFT;
  9225. unsigned long end_index = end >> PAGE_SHIFT;
  9226. struct page *page;
  9227. while (index <= end_index) {
  9228. page = find_get_page(inode->i_mapping, index);
  9229. ASSERT(page); /* Pages should be in the extent_io_tree */
  9230. set_page_writeback(page);
  9231. put_page(page);
  9232. index++;
  9233. }
  9234. }
  9235. static const struct inode_operations btrfs_dir_inode_operations = {
  9236. .getattr = btrfs_getattr,
  9237. .lookup = btrfs_lookup,
  9238. .create = btrfs_create,
  9239. .unlink = btrfs_unlink,
  9240. .link = btrfs_link,
  9241. .mkdir = btrfs_mkdir,
  9242. .rmdir = btrfs_rmdir,
  9243. .rename = btrfs_rename2,
  9244. .symlink = btrfs_symlink,
  9245. .setattr = btrfs_setattr,
  9246. .mknod = btrfs_mknod,
  9247. .listxattr = btrfs_listxattr,
  9248. .permission = btrfs_permission,
  9249. .get_acl = btrfs_get_acl,
  9250. .set_acl = btrfs_set_acl,
  9251. .update_time = btrfs_update_time,
  9252. .tmpfile = btrfs_tmpfile,
  9253. };
  9254. static const struct inode_operations btrfs_dir_ro_inode_operations = {
  9255. .lookup = btrfs_lookup,
  9256. .permission = btrfs_permission,
  9257. .update_time = btrfs_update_time,
  9258. };
  9259. static const struct file_operations btrfs_dir_file_operations = {
  9260. .llseek = generic_file_llseek,
  9261. .read = generic_read_dir,
  9262. .iterate_shared = btrfs_real_readdir,
  9263. .open = btrfs_opendir,
  9264. .unlocked_ioctl = btrfs_ioctl,
  9265. #ifdef CONFIG_COMPAT
  9266. .compat_ioctl = btrfs_compat_ioctl,
  9267. #endif
  9268. .release = btrfs_release_file,
  9269. .fsync = btrfs_sync_file,
  9270. };
  9271. static const struct extent_io_ops btrfs_extent_io_ops = {
  9272. /* mandatory callbacks */
  9273. .submit_bio_hook = btrfs_submit_bio_hook,
  9274. .readpage_end_io_hook = btrfs_readpage_end_io_hook,
  9275. .merge_bio_hook = btrfs_merge_bio_hook,
  9276. .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
  9277. .set_range_writeback = btrfs_set_range_writeback,
  9278. /* optional callbacks */
  9279. .fill_delalloc = run_delalloc_range,
  9280. .writepage_end_io_hook = btrfs_writepage_end_io_hook,
  9281. .writepage_start_hook = btrfs_writepage_start_hook,
  9282. .set_bit_hook = btrfs_set_bit_hook,
  9283. .clear_bit_hook = btrfs_clear_bit_hook,
  9284. .merge_extent_hook = btrfs_merge_extent_hook,
  9285. .split_extent_hook = btrfs_split_extent_hook,
  9286. .check_extent_io_range = btrfs_check_extent_io_range,
  9287. };
  9288. /*
  9289. * btrfs doesn't support the bmap operation because swapfiles
  9290. * use bmap to make a mapping of extents in the file. They assume
  9291. * these extents won't change over the life of the file and they
  9292. * use the bmap result to do IO directly to the drive.
  9293. *
  9294. * the btrfs bmap call would return logical addresses that aren't
  9295. * suitable for IO and they also will change frequently as COW
  9296. * operations happen. So, swapfile + btrfs == corruption.
  9297. *
  9298. * For now we're avoiding this by dropping bmap.
  9299. */
  9300. static const struct address_space_operations btrfs_aops = {
  9301. .readpage = btrfs_readpage,
  9302. .writepage = btrfs_writepage,
  9303. .writepages = btrfs_writepages,
  9304. .readpages = btrfs_readpages,
  9305. .direct_IO = btrfs_direct_IO,
  9306. .invalidatepage = btrfs_invalidatepage,
  9307. .releasepage = btrfs_releasepage,
  9308. .set_page_dirty = btrfs_set_page_dirty,
  9309. .error_remove_page = generic_error_remove_page,
  9310. };
  9311. static const struct address_space_operations btrfs_symlink_aops = {
  9312. .readpage = btrfs_readpage,
  9313. .writepage = btrfs_writepage,
  9314. .invalidatepage = btrfs_invalidatepage,
  9315. .releasepage = btrfs_releasepage,
  9316. };
  9317. static const struct inode_operations btrfs_file_inode_operations = {
  9318. .getattr = btrfs_getattr,
  9319. .setattr = btrfs_setattr,
  9320. .listxattr = btrfs_listxattr,
  9321. .permission = btrfs_permission,
  9322. .fiemap = btrfs_fiemap,
  9323. .get_acl = btrfs_get_acl,
  9324. .set_acl = btrfs_set_acl,
  9325. .update_time = btrfs_update_time,
  9326. };
  9327. static const struct inode_operations btrfs_special_inode_operations = {
  9328. .getattr = btrfs_getattr,
  9329. .setattr = btrfs_setattr,
  9330. .permission = btrfs_permission,
  9331. .listxattr = btrfs_listxattr,
  9332. .get_acl = btrfs_get_acl,
  9333. .set_acl = btrfs_set_acl,
  9334. .update_time = btrfs_update_time,
  9335. };
  9336. static const struct inode_operations btrfs_symlink_inode_operations = {
  9337. .get_link = page_get_link,
  9338. .getattr = btrfs_getattr,
  9339. .setattr = btrfs_setattr,
  9340. .permission = btrfs_permission,
  9341. .listxattr = btrfs_listxattr,
  9342. .update_time = btrfs_update_time,
  9343. };
  9344. const struct dentry_operations btrfs_dentry_operations = {
  9345. .d_delete = btrfs_dentry_delete,
  9346. };