inode.c 197 KB

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