inode.c 228 KB

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