inode.c 269 KB

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