inode.c 287 KB

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