inode.c 283 KB

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