inode.c 286 KB

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