inode.c 266 KB

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