inode.c 284 KB

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