inode.c 288 KB

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