inode.c 231 KB

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