inode.c 268 KB

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