inode.c 283 KB

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