inode.c 199 KB

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