inode.c 253 KB

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