inode.c 197 KB

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