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