inode.c 197 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451
  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. trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
  1596. ClearPagePrivate2(page);
  1597. return btrfs_finish_ordered_io(page->mapping->host, start, end);
  1598. }
  1599. /*
  1600. * When IO fails, either with EIO or csum verification fails, we
  1601. * try other mirrors that might have a good copy of the data. This
  1602. * io_failure_record is used to record state as we go through all the
  1603. * mirrors. If another mirror has good data, the page is set up to date
  1604. * and things continue. If a good mirror can't be found, the original
  1605. * bio end_io callback is called to indicate things have failed.
  1606. */
  1607. struct io_failure_record {
  1608. struct page *page;
  1609. u64 start;
  1610. u64 len;
  1611. u64 logical;
  1612. unsigned long bio_flags;
  1613. int last_mirror;
  1614. };
  1615. static int btrfs_io_failed_hook(struct bio *failed_bio,
  1616. struct page *page, u64 start, u64 end,
  1617. struct extent_state *state)
  1618. {
  1619. struct io_failure_record *failrec = NULL;
  1620. u64 private;
  1621. struct extent_map *em;
  1622. struct inode *inode = page->mapping->host;
  1623. struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
  1624. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  1625. struct bio *bio;
  1626. int num_copies;
  1627. int ret;
  1628. int rw;
  1629. u64 logical;
  1630. ret = get_state_private(failure_tree, start, &private);
  1631. if (ret) {
  1632. failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
  1633. if (!failrec)
  1634. return -ENOMEM;
  1635. failrec->start = start;
  1636. failrec->len = end - start + 1;
  1637. failrec->last_mirror = 0;
  1638. failrec->bio_flags = 0;
  1639. read_lock(&em_tree->lock);
  1640. em = lookup_extent_mapping(em_tree, start, failrec->len);
  1641. if (em->start > start || em->start + em->len < start) {
  1642. free_extent_map(em);
  1643. em = NULL;
  1644. }
  1645. read_unlock(&em_tree->lock);
  1646. if (!em || IS_ERR(em)) {
  1647. kfree(failrec);
  1648. return -EIO;
  1649. }
  1650. logical = start - em->start;
  1651. logical = em->block_start + logical;
  1652. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  1653. logical = em->block_start;
  1654. failrec->bio_flags = EXTENT_BIO_COMPRESSED;
  1655. extent_set_compress_type(&failrec->bio_flags,
  1656. em->compress_type);
  1657. }
  1658. failrec->logical = logical;
  1659. free_extent_map(em);
  1660. set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
  1661. EXTENT_DIRTY, GFP_NOFS);
  1662. set_state_private(failure_tree, start,
  1663. (u64)(unsigned long)failrec);
  1664. } else {
  1665. failrec = (struct io_failure_record *)(unsigned long)private;
  1666. }
  1667. num_copies = btrfs_num_copies(
  1668. &BTRFS_I(inode)->root->fs_info->mapping_tree,
  1669. failrec->logical, failrec->len);
  1670. failrec->last_mirror++;
  1671. if (!state) {
  1672. spin_lock(&BTRFS_I(inode)->io_tree.lock);
  1673. state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
  1674. failrec->start,
  1675. EXTENT_LOCKED);
  1676. if (state && state->start != failrec->start)
  1677. state = NULL;
  1678. spin_unlock(&BTRFS_I(inode)->io_tree.lock);
  1679. }
  1680. if (!state || failrec->last_mirror > num_copies) {
  1681. set_state_private(failure_tree, failrec->start, 0);
  1682. clear_extent_bits(failure_tree, failrec->start,
  1683. failrec->start + failrec->len - 1,
  1684. EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
  1685. kfree(failrec);
  1686. return -EIO;
  1687. }
  1688. bio = bio_alloc(GFP_NOFS, 1);
  1689. bio->bi_private = state;
  1690. bio->bi_end_io = failed_bio->bi_end_io;
  1691. bio->bi_sector = failrec->logical >> 9;
  1692. bio->bi_bdev = failed_bio->bi_bdev;
  1693. bio->bi_size = 0;
  1694. bio_add_page(bio, page, failrec->len, start - page_offset(page));
  1695. if (failed_bio->bi_rw & REQ_WRITE)
  1696. rw = WRITE;
  1697. else
  1698. rw = READ;
  1699. BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
  1700. failrec->last_mirror,
  1701. failrec->bio_flags, 0);
  1702. return 0;
  1703. }
  1704. /*
  1705. * each time an IO finishes, we do a fast check in the IO failure tree
  1706. * to see if we need to process or clean up an io_failure_record
  1707. */
  1708. static int btrfs_clean_io_failures(struct inode *inode, u64 start)
  1709. {
  1710. u64 private;
  1711. u64 private_failure;
  1712. struct io_failure_record *failure;
  1713. int ret;
  1714. private = 0;
  1715. if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
  1716. (u64)-1, 1, EXTENT_DIRTY, 0)) {
  1717. ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
  1718. start, &private_failure);
  1719. if (ret == 0) {
  1720. failure = (struct io_failure_record *)(unsigned long)
  1721. private_failure;
  1722. set_state_private(&BTRFS_I(inode)->io_failure_tree,
  1723. failure->start, 0);
  1724. clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
  1725. failure->start,
  1726. failure->start + failure->len - 1,
  1727. EXTENT_DIRTY | EXTENT_LOCKED,
  1728. GFP_NOFS);
  1729. kfree(failure);
  1730. }
  1731. }
  1732. return 0;
  1733. }
  1734. /*
  1735. * when reads are done, we need to check csums to verify the data is correct
  1736. * if there's a match, we allow the bio to finish. If not, we go through
  1737. * the io_failure_record routines to find good copies
  1738. */
  1739. static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
  1740. struct extent_state *state)
  1741. {
  1742. size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
  1743. struct inode *inode = page->mapping->host;
  1744. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  1745. char *kaddr;
  1746. u64 private = ~(u32)0;
  1747. int ret;
  1748. struct btrfs_root *root = BTRFS_I(inode)->root;
  1749. u32 csum = ~(u32)0;
  1750. if (PageChecked(page)) {
  1751. ClearPageChecked(page);
  1752. goto good;
  1753. }
  1754. if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
  1755. return 0;
  1756. if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
  1757. test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
  1758. clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
  1759. GFP_NOFS);
  1760. return 0;
  1761. }
  1762. if (state && state->start == start) {
  1763. private = state->private;
  1764. ret = 0;
  1765. } else {
  1766. ret = get_state_private(io_tree, start, &private);
  1767. }
  1768. kaddr = kmap_atomic(page, KM_USER0);
  1769. if (ret)
  1770. goto zeroit;
  1771. csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
  1772. btrfs_csum_final(csum, (char *)&csum);
  1773. if (csum != private)
  1774. goto zeroit;
  1775. kunmap_atomic(kaddr, KM_USER0);
  1776. good:
  1777. /* if the io failure tree for this inode is non-empty,
  1778. * check to see if we've recovered from a failed IO
  1779. */
  1780. btrfs_clean_io_failures(inode, start);
  1781. return 0;
  1782. zeroit:
  1783. if (printk_ratelimit()) {
  1784. printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
  1785. "private %llu\n", page->mapping->host->i_ino,
  1786. (unsigned long long)start, csum,
  1787. (unsigned long long)private);
  1788. }
  1789. memset(kaddr + offset, 1, end - start + 1);
  1790. flush_dcache_page(page);
  1791. kunmap_atomic(kaddr, KM_USER0);
  1792. if (private == 0)
  1793. return 0;
  1794. return -EIO;
  1795. }
  1796. struct delayed_iput {
  1797. struct list_head list;
  1798. struct inode *inode;
  1799. };
  1800. void btrfs_add_delayed_iput(struct inode *inode)
  1801. {
  1802. struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
  1803. struct delayed_iput *delayed;
  1804. if (atomic_add_unless(&inode->i_count, -1, 1))
  1805. return;
  1806. delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
  1807. delayed->inode = inode;
  1808. spin_lock(&fs_info->delayed_iput_lock);
  1809. list_add_tail(&delayed->list, &fs_info->delayed_iputs);
  1810. spin_unlock(&fs_info->delayed_iput_lock);
  1811. }
  1812. void btrfs_run_delayed_iputs(struct btrfs_root *root)
  1813. {
  1814. LIST_HEAD(list);
  1815. struct btrfs_fs_info *fs_info = root->fs_info;
  1816. struct delayed_iput *delayed;
  1817. int empty;
  1818. spin_lock(&fs_info->delayed_iput_lock);
  1819. empty = list_empty(&fs_info->delayed_iputs);
  1820. spin_unlock(&fs_info->delayed_iput_lock);
  1821. if (empty)
  1822. return;
  1823. down_read(&root->fs_info->cleanup_work_sem);
  1824. spin_lock(&fs_info->delayed_iput_lock);
  1825. list_splice_init(&fs_info->delayed_iputs, &list);
  1826. spin_unlock(&fs_info->delayed_iput_lock);
  1827. while (!list_empty(&list)) {
  1828. delayed = list_entry(list.next, struct delayed_iput, list);
  1829. list_del(&delayed->list);
  1830. iput(delayed->inode);
  1831. kfree(delayed);
  1832. }
  1833. up_read(&root->fs_info->cleanup_work_sem);
  1834. }
  1835. /*
  1836. * calculate extra metadata reservation when snapshotting a subvolume
  1837. * contains orphan files.
  1838. */
  1839. void btrfs_orphan_pre_snapshot(struct btrfs_trans_handle *trans,
  1840. struct btrfs_pending_snapshot *pending,
  1841. u64 *bytes_to_reserve)
  1842. {
  1843. struct btrfs_root *root;
  1844. struct btrfs_block_rsv *block_rsv;
  1845. u64 num_bytes;
  1846. int index;
  1847. root = pending->root;
  1848. if (!root->orphan_block_rsv || list_empty(&root->orphan_list))
  1849. return;
  1850. block_rsv = root->orphan_block_rsv;
  1851. /* orphan block reservation for the snapshot */
  1852. num_bytes = block_rsv->size;
  1853. /*
  1854. * after the snapshot is created, COWing tree blocks may use more
  1855. * space than it frees. So we should make sure there is enough
  1856. * reserved space.
  1857. */
  1858. index = trans->transid & 0x1;
  1859. if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) {
  1860. num_bytes += block_rsv->size -
  1861. (block_rsv->reserved + block_rsv->freed[index]);
  1862. }
  1863. *bytes_to_reserve += num_bytes;
  1864. }
  1865. void btrfs_orphan_post_snapshot(struct btrfs_trans_handle *trans,
  1866. struct btrfs_pending_snapshot *pending)
  1867. {
  1868. struct btrfs_root *root = pending->root;
  1869. struct btrfs_root *snap = pending->snap;
  1870. struct btrfs_block_rsv *block_rsv;
  1871. u64 num_bytes;
  1872. int index;
  1873. int ret;
  1874. if (!root->orphan_block_rsv || list_empty(&root->orphan_list))
  1875. return;
  1876. /* refill source subvolume's orphan block reservation */
  1877. block_rsv = root->orphan_block_rsv;
  1878. index = trans->transid & 0x1;
  1879. if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) {
  1880. num_bytes = block_rsv->size -
  1881. (block_rsv->reserved + block_rsv->freed[index]);
  1882. ret = btrfs_block_rsv_migrate(&pending->block_rsv,
  1883. root->orphan_block_rsv,
  1884. num_bytes);
  1885. BUG_ON(ret);
  1886. }
  1887. /* setup orphan block reservation for the snapshot */
  1888. block_rsv = btrfs_alloc_block_rsv(snap);
  1889. BUG_ON(!block_rsv);
  1890. btrfs_add_durable_block_rsv(root->fs_info, block_rsv);
  1891. snap->orphan_block_rsv = block_rsv;
  1892. num_bytes = root->orphan_block_rsv->size;
  1893. ret = btrfs_block_rsv_migrate(&pending->block_rsv,
  1894. block_rsv, num_bytes);
  1895. BUG_ON(ret);
  1896. #if 0
  1897. /* insert orphan item for the snapshot */
  1898. WARN_ON(!root->orphan_item_inserted);
  1899. ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
  1900. snap->root_key.objectid);
  1901. BUG_ON(ret);
  1902. snap->orphan_item_inserted = 1;
  1903. #endif
  1904. }
  1905. enum btrfs_orphan_cleanup_state {
  1906. ORPHAN_CLEANUP_STARTED = 1,
  1907. ORPHAN_CLEANUP_DONE = 2,
  1908. };
  1909. /*
  1910. * This is called in transaction commmit time. If there are no orphan
  1911. * files in the subvolume, it removes orphan item and frees block_rsv
  1912. * structure.
  1913. */
  1914. void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
  1915. struct btrfs_root *root)
  1916. {
  1917. int ret;
  1918. if (!list_empty(&root->orphan_list) ||
  1919. root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
  1920. return;
  1921. if (root->orphan_item_inserted &&
  1922. btrfs_root_refs(&root->root_item) > 0) {
  1923. ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
  1924. root->root_key.objectid);
  1925. BUG_ON(ret);
  1926. root->orphan_item_inserted = 0;
  1927. }
  1928. if (root->orphan_block_rsv) {
  1929. WARN_ON(root->orphan_block_rsv->size > 0);
  1930. btrfs_free_block_rsv(root, root->orphan_block_rsv);
  1931. root->orphan_block_rsv = NULL;
  1932. }
  1933. }
  1934. /*
  1935. * This creates an orphan entry for the given inode in case something goes
  1936. * wrong in the middle of an unlink/truncate.
  1937. *
  1938. * NOTE: caller of this function should reserve 5 units of metadata for
  1939. * this function.
  1940. */
  1941. int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
  1942. {
  1943. struct btrfs_root *root = BTRFS_I(inode)->root;
  1944. struct btrfs_block_rsv *block_rsv = NULL;
  1945. int reserve = 0;
  1946. int insert = 0;
  1947. int ret;
  1948. if (!root->orphan_block_rsv) {
  1949. block_rsv = btrfs_alloc_block_rsv(root);
  1950. BUG_ON(!block_rsv);
  1951. }
  1952. spin_lock(&root->orphan_lock);
  1953. if (!root->orphan_block_rsv) {
  1954. root->orphan_block_rsv = block_rsv;
  1955. } else if (block_rsv) {
  1956. btrfs_free_block_rsv(root, block_rsv);
  1957. block_rsv = NULL;
  1958. }
  1959. if (list_empty(&BTRFS_I(inode)->i_orphan)) {
  1960. list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
  1961. #if 0
  1962. /*
  1963. * For proper ENOSPC handling, we should do orphan
  1964. * cleanup when mounting. But this introduces backward
  1965. * compatibility issue.
  1966. */
  1967. if (!xchg(&root->orphan_item_inserted, 1))
  1968. insert = 2;
  1969. else
  1970. insert = 1;
  1971. #endif
  1972. insert = 1;
  1973. } else {
  1974. WARN_ON(!BTRFS_I(inode)->orphan_meta_reserved);
  1975. }
  1976. if (!BTRFS_I(inode)->orphan_meta_reserved) {
  1977. BTRFS_I(inode)->orphan_meta_reserved = 1;
  1978. reserve = 1;
  1979. }
  1980. spin_unlock(&root->orphan_lock);
  1981. if (block_rsv)
  1982. btrfs_add_durable_block_rsv(root->fs_info, block_rsv);
  1983. /* grab metadata reservation from transaction handle */
  1984. if (reserve) {
  1985. ret = btrfs_orphan_reserve_metadata(trans, inode);
  1986. BUG_ON(ret);
  1987. }
  1988. /* insert an orphan item to track this unlinked/truncated file */
  1989. if (insert >= 1) {
  1990. ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
  1991. BUG_ON(ret);
  1992. }
  1993. /* insert an orphan item to track subvolume contains orphan files */
  1994. if (insert >= 2) {
  1995. ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
  1996. root->root_key.objectid);
  1997. BUG_ON(ret);
  1998. }
  1999. return 0;
  2000. }
  2001. /*
  2002. * We have done the truncate/delete so we can go ahead and remove the orphan
  2003. * item for this particular inode.
  2004. */
  2005. int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
  2006. {
  2007. struct btrfs_root *root = BTRFS_I(inode)->root;
  2008. int delete_item = 0;
  2009. int release_rsv = 0;
  2010. int ret = 0;
  2011. spin_lock(&root->orphan_lock);
  2012. if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
  2013. list_del_init(&BTRFS_I(inode)->i_orphan);
  2014. delete_item = 1;
  2015. }
  2016. if (BTRFS_I(inode)->orphan_meta_reserved) {
  2017. BTRFS_I(inode)->orphan_meta_reserved = 0;
  2018. release_rsv = 1;
  2019. }
  2020. spin_unlock(&root->orphan_lock);
  2021. if (trans && delete_item) {
  2022. ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
  2023. BUG_ON(ret);
  2024. }
  2025. if (release_rsv)
  2026. btrfs_orphan_release_metadata(inode);
  2027. return 0;
  2028. }
  2029. /*
  2030. * this cleans up any orphans that may be left on the list from the last use
  2031. * of this root.
  2032. */
  2033. int btrfs_orphan_cleanup(struct btrfs_root *root)
  2034. {
  2035. struct btrfs_path *path;
  2036. struct extent_buffer *leaf;
  2037. struct btrfs_key key, found_key;
  2038. struct btrfs_trans_handle *trans;
  2039. struct inode *inode;
  2040. int ret = 0, nr_unlink = 0, nr_truncate = 0;
  2041. if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
  2042. return 0;
  2043. path = btrfs_alloc_path();
  2044. if (!path) {
  2045. ret = -ENOMEM;
  2046. goto out;
  2047. }
  2048. path->reada = -1;
  2049. key.objectid = BTRFS_ORPHAN_OBJECTID;
  2050. btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
  2051. key.offset = (u64)-1;
  2052. while (1) {
  2053. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2054. if (ret < 0)
  2055. goto out;
  2056. /*
  2057. * if ret == 0 means we found what we were searching for, which
  2058. * is weird, but possible, so only screw with path if we didnt
  2059. * find the key and see if we have stuff that matches
  2060. */
  2061. if (ret > 0) {
  2062. ret = 0;
  2063. if (path->slots[0] == 0)
  2064. break;
  2065. path->slots[0]--;
  2066. }
  2067. /* pull out the item */
  2068. leaf = path->nodes[0];
  2069. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2070. /* make sure the item matches what we want */
  2071. if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
  2072. break;
  2073. if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
  2074. break;
  2075. /* release the path since we're done with it */
  2076. btrfs_release_path(root, path);
  2077. /*
  2078. * this is where we are basically btrfs_lookup, without the
  2079. * crossing root thing. we store the inode number in the
  2080. * offset of the orphan item.
  2081. */
  2082. found_key.objectid = found_key.offset;
  2083. found_key.type = BTRFS_INODE_ITEM_KEY;
  2084. found_key.offset = 0;
  2085. inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
  2086. if (IS_ERR(inode)) {
  2087. ret = PTR_ERR(inode);
  2088. goto out;
  2089. }
  2090. /*
  2091. * add this inode to the orphan list so btrfs_orphan_del does
  2092. * the proper thing when we hit it
  2093. */
  2094. spin_lock(&root->orphan_lock);
  2095. list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
  2096. spin_unlock(&root->orphan_lock);
  2097. /*
  2098. * if this is a bad inode, means we actually succeeded in
  2099. * removing the inode, but not the orphan record, which means
  2100. * we need to manually delete the orphan since iput will just
  2101. * do a destroy_inode
  2102. */
  2103. if (is_bad_inode(inode)) {
  2104. trans = btrfs_start_transaction(root, 0);
  2105. if (IS_ERR(trans)) {
  2106. ret = PTR_ERR(trans);
  2107. goto out;
  2108. }
  2109. btrfs_orphan_del(trans, inode);
  2110. btrfs_end_transaction(trans, root);
  2111. iput(inode);
  2112. continue;
  2113. }
  2114. /* if we have links, this was a truncate, lets do that */
  2115. if (inode->i_nlink) {
  2116. if (!S_ISREG(inode->i_mode)) {
  2117. WARN_ON(1);
  2118. iput(inode);
  2119. continue;
  2120. }
  2121. nr_truncate++;
  2122. ret = btrfs_truncate(inode);
  2123. } else {
  2124. nr_unlink++;
  2125. }
  2126. /* this will do delete_inode and everything for us */
  2127. iput(inode);
  2128. if (ret)
  2129. goto out;
  2130. }
  2131. root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
  2132. if (root->orphan_block_rsv)
  2133. btrfs_block_rsv_release(root, root->orphan_block_rsv,
  2134. (u64)-1);
  2135. if (root->orphan_block_rsv || root->orphan_item_inserted) {
  2136. trans = btrfs_join_transaction(root, 1);
  2137. if (!IS_ERR(trans))
  2138. btrfs_end_transaction(trans, root);
  2139. }
  2140. if (nr_unlink)
  2141. printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
  2142. if (nr_truncate)
  2143. printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
  2144. out:
  2145. if (ret)
  2146. printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
  2147. btrfs_free_path(path);
  2148. return ret;
  2149. }
  2150. /*
  2151. * very simple check to peek ahead in the leaf looking for xattrs. If we
  2152. * don't find any xattrs, we know there can't be any acls.
  2153. *
  2154. * slot is the slot the inode is in, objectid is the objectid of the inode
  2155. */
  2156. static noinline int acls_after_inode_item(struct extent_buffer *leaf,
  2157. int slot, u64 objectid)
  2158. {
  2159. u32 nritems = btrfs_header_nritems(leaf);
  2160. struct btrfs_key found_key;
  2161. int scanned = 0;
  2162. slot++;
  2163. while (slot < nritems) {
  2164. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  2165. /* we found a different objectid, there must not be acls */
  2166. if (found_key.objectid != objectid)
  2167. return 0;
  2168. /* we found an xattr, assume we've got an acl */
  2169. if (found_key.type == BTRFS_XATTR_ITEM_KEY)
  2170. return 1;
  2171. /*
  2172. * we found a key greater than an xattr key, there can't
  2173. * be any acls later on
  2174. */
  2175. if (found_key.type > BTRFS_XATTR_ITEM_KEY)
  2176. return 0;
  2177. slot++;
  2178. scanned++;
  2179. /*
  2180. * it goes inode, inode backrefs, xattrs, extents,
  2181. * so if there are a ton of hard links to an inode there can
  2182. * be a lot of backrefs. Don't waste time searching too hard,
  2183. * this is just an optimization
  2184. */
  2185. if (scanned >= 8)
  2186. break;
  2187. }
  2188. /* we hit the end of the leaf before we found an xattr or
  2189. * something larger than an xattr. We have to assume the inode
  2190. * has acls
  2191. */
  2192. return 1;
  2193. }
  2194. /*
  2195. * read an inode from the btree into the in-memory inode
  2196. */
  2197. static void btrfs_read_locked_inode(struct inode *inode)
  2198. {
  2199. struct btrfs_path *path;
  2200. struct extent_buffer *leaf;
  2201. struct btrfs_inode_item *inode_item;
  2202. struct btrfs_timespec *tspec;
  2203. struct btrfs_root *root = BTRFS_I(inode)->root;
  2204. struct btrfs_key location;
  2205. int maybe_acls;
  2206. u64 alloc_group_block;
  2207. u32 rdev;
  2208. int ret;
  2209. path = btrfs_alloc_path();
  2210. BUG_ON(!path);
  2211. memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
  2212. ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
  2213. if (ret)
  2214. goto make_bad;
  2215. leaf = path->nodes[0];
  2216. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  2217. struct btrfs_inode_item);
  2218. inode->i_mode = btrfs_inode_mode(leaf, inode_item);
  2219. inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
  2220. inode->i_uid = btrfs_inode_uid(leaf, inode_item);
  2221. inode->i_gid = btrfs_inode_gid(leaf, inode_item);
  2222. btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
  2223. tspec = btrfs_inode_atime(inode_item);
  2224. inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  2225. inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  2226. tspec = btrfs_inode_mtime(inode_item);
  2227. inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  2228. inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  2229. tspec = btrfs_inode_ctime(inode_item);
  2230. inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  2231. inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  2232. inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
  2233. BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
  2234. BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
  2235. inode->i_generation = BTRFS_I(inode)->generation;
  2236. inode->i_rdev = 0;
  2237. rdev = btrfs_inode_rdev(leaf, inode_item);
  2238. BTRFS_I(inode)->index_cnt = (u64)-1;
  2239. BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
  2240. alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
  2241. /*
  2242. * try to precache a NULL acl entry for files that don't have
  2243. * any xattrs or acls
  2244. */
  2245. maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino);
  2246. if (!maybe_acls)
  2247. cache_no_acl(inode);
  2248. BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
  2249. alloc_group_block, 0);
  2250. btrfs_free_path(path);
  2251. inode_item = NULL;
  2252. switch (inode->i_mode & S_IFMT) {
  2253. case S_IFREG:
  2254. inode->i_mapping->a_ops = &btrfs_aops;
  2255. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  2256. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  2257. inode->i_fop = &btrfs_file_operations;
  2258. inode->i_op = &btrfs_file_inode_operations;
  2259. break;
  2260. case S_IFDIR:
  2261. inode->i_fop = &btrfs_dir_file_operations;
  2262. if (root == root->fs_info->tree_root)
  2263. inode->i_op = &btrfs_dir_ro_inode_operations;
  2264. else
  2265. inode->i_op = &btrfs_dir_inode_operations;
  2266. break;
  2267. case S_IFLNK:
  2268. inode->i_op = &btrfs_symlink_inode_operations;
  2269. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  2270. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  2271. break;
  2272. default:
  2273. inode->i_op = &btrfs_special_inode_operations;
  2274. init_special_inode(inode, inode->i_mode, rdev);
  2275. break;
  2276. }
  2277. btrfs_update_iflags(inode);
  2278. return;
  2279. make_bad:
  2280. btrfs_free_path(path);
  2281. make_bad_inode(inode);
  2282. }
  2283. /*
  2284. * given a leaf and an inode, copy the inode fields into the leaf
  2285. */
  2286. static void fill_inode_item(struct btrfs_trans_handle *trans,
  2287. struct extent_buffer *leaf,
  2288. struct btrfs_inode_item *item,
  2289. struct inode *inode)
  2290. {
  2291. btrfs_set_inode_uid(leaf, item, inode->i_uid);
  2292. btrfs_set_inode_gid(leaf, item, inode->i_gid);
  2293. btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
  2294. btrfs_set_inode_mode(leaf, item, inode->i_mode);
  2295. btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
  2296. btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
  2297. inode->i_atime.tv_sec);
  2298. btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
  2299. inode->i_atime.tv_nsec);
  2300. btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
  2301. inode->i_mtime.tv_sec);
  2302. btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
  2303. inode->i_mtime.tv_nsec);
  2304. btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
  2305. inode->i_ctime.tv_sec);
  2306. btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
  2307. inode->i_ctime.tv_nsec);
  2308. btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
  2309. btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
  2310. btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
  2311. btrfs_set_inode_transid(leaf, item, trans->transid);
  2312. btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
  2313. btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
  2314. btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
  2315. }
  2316. /*
  2317. * copy everything in the in-memory inode into the btree.
  2318. */
  2319. noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
  2320. struct btrfs_root *root, struct inode *inode)
  2321. {
  2322. struct btrfs_inode_item *inode_item;
  2323. struct btrfs_path *path;
  2324. struct extent_buffer *leaf;
  2325. int ret;
  2326. path = btrfs_alloc_path();
  2327. BUG_ON(!path);
  2328. path->leave_spinning = 1;
  2329. ret = btrfs_lookup_inode(trans, root, path,
  2330. &BTRFS_I(inode)->location, 1);
  2331. if (ret) {
  2332. if (ret > 0)
  2333. ret = -ENOENT;
  2334. goto failed;
  2335. }
  2336. btrfs_unlock_up_safe(path, 1);
  2337. leaf = path->nodes[0];
  2338. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  2339. struct btrfs_inode_item);
  2340. fill_inode_item(trans, leaf, inode_item, inode);
  2341. btrfs_mark_buffer_dirty(leaf);
  2342. btrfs_set_inode_last_trans(trans, inode);
  2343. ret = 0;
  2344. failed:
  2345. btrfs_free_path(path);
  2346. return ret;
  2347. }
  2348. /*
  2349. * unlink helper that gets used here in inode.c and in the tree logging
  2350. * recovery code. It remove a link in a directory with a given name, and
  2351. * also drops the back refs in the inode to the directory
  2352. */
  2353. int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
  2354. struct btrfs_root *root,
  2355. struct inode *dir, struct inode *inode,
  2356. const char *name, int name_len)
  2357. {
  2358. struct btrfs_path *path;
  2359. int ret = 0;
  2360. struct extent_buffer *leaf;
  2361. struct btrfs_dir_item *di;
  2362. struct btrfs_key key;
  2363. u64 index;
  2364. path = btrfs_alloc_path();
  2365. if (!path) {
  2366. ret = -ENOMEM;
  2367. goto out;
  2368. }
  2369. path->leave_spinning = 1;
  2370. di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
  2371. name, name_len, -1);
  2372. if (IS_ERR(di)) {
  2373. ret = PTR_ERR(di);
  2374. goto err;
  2375. }
  2376. if (!di) {
  2377. ret = -ENOENT;
  2378. goto err;
  2379. }
  2380. leaf = path->nodes[0];
  2381. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  2382. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  2383. if (ret)
  2384. goto err;
  2385. btrfs_release_path(root, path);
  2386. ret = btrfs_del_inode_ref(trans, root, name, name_len,
  2387. inode->i_ino,
  2388. dir->i_ino, &index);
  2389. if (ret) {
  2390. printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
  2391. "inode %lu parent %lu\n", name_len, name,
  2392. inode->i_ino, dir->i_ino);
  2393. goto err;
  2394. }
  2395. di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
  2396. index, name, name_len, -1);
  2397. if (IS_ERR(di)) {
  2398. ret = PTR_ERR(di);
  2399. goto err;
  2400. }
  2401. if (!di) {
  2402. ret = -ENOENT;
  2403. goto err;
  2404. }
  2405. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  2406. btrfs_release_path(root, path);
  2407. ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
  2408. inode, dir->i_ino);
  2409. BUG_ON(ret != 0 && ret != -ENOENT);
  2410. ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
  2411. dir, index);
  2412. if (ret == -ENOENT)
  2413. ret = 0;
  2414. err:
  2415. btrfs_free_path(path);
  2416. if (ret)
  2417. goto out;
  2418. btrfs_i_size_write(dir, dir->i_size - name_len * 2);
  2419. inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  2420. btrfs_update_inode(trans, root, dir);
  2421. btrfs_drop_nlink(inode);
  2422. ret = btrfs_update_inode(trans, root, inode);
  2423. out:
  2424. return ret;
  2425. }
  2426. /* helper to check if there is any shared block in the path */
  2427. static int check_path_shared(struct btrfs_root *root,
  2428. struct btrfs_path *path)
  2429. {
  2430. struct extent_buffer *eb;
  2431. int level;
  2432. u64 refs = 1;
  2433. for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
  2434. int ret;
  2435. if (!path->nodes[level])
  2436. break;
  2437. eb = path->nodes[level];
  2438. if (!btrfs_block_can_be_shared(root, eb))
  2439. continue;
  2440. ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
  2441. &refs, NULL);
  2442. if (refs > 1)
  2443. return 1;
  2444. }
  2445. return 0;
  2446. }
  2447. /*
  2448. * helper to start transaction for unlink and rmdir.
  2449. *
  2450. * unlink and rmdir are special in btrfs, they do not always free space.
  2451. * so in enospc case, we should make sure they will free space before
  2452. * allowing them to use the global metadata reservation.
  2453. */
  2454. static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
  2455. struct dentry *dentry)
  2456. {
  2457. struct btrfs_trans_handle *trans;
  2458. struct btrfs_root *root = BTRFS_I(dir)->root;
  2459. struct btrfs_path *path;
  2460. struct btrfs_inode_ref *ref;
  2461. struct btrfs_dir_item *di;
  2462. struct inode *inode = dentry->d_inode;
  2463. u64 index;
  2464. int check_link = 1;
  2465. int err = -ENOSPC;
  2466. int ret;
  2467. trans = btrfs_start_transaction(root, 10);
  2468. if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
  2469. return trans;
  2470. if (inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
  2471. return ERR_PTR(-ENOSPC);
  2472. /* check if there is someone else holds reference */
  2473. if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
  2474. return ERR_PTR(-ENOSPC);
  2475. if (atomic_read(&inode->i_count) > 2)
  2476. return ERR_PTR(-ENOSPC);
  2477. if (xchg(&root->fs_info->enospc_unlink, 1))
  2478. return ERR_PTR(-ENOSPC);
  2479. path = btrfs_alloc_path();
  2480. if (!path) {
  2481. root->fs_info->enospc_unlink = 0;
  2482. return ERR_PTR(-ENOMEM);
  2483. }
  2484. trans = btrfs_start_transaction(root, 0);
  2485. if (IS_ERR(trans)) {
  2486. btrfs_free_path(path);
  2487. root->fs_info->enospc_unlink = 0;
  2488. return trans;
  2489. }
  2490. path->skip_locking = 1;
  2491. path->search_commit_root = 1;
  2492. ret = btrfs_lookup_inode(trans, root, path,
  2493. &BTRFS_I(dir)->location, 0);
  2494. if (ret < 0) {
  2495. err = ret;
  2496. goto out;
  2497. }
  2498. if (ret == 0) {
  2499. if (check_path_shared(root, path))
  2500. goto out;
  2501. } else {
  2502. check_link = 0;
  2503. }
  2504. btrfs_release_path(root, path);
  2505. ret = btrfs_lookup_inode(trans, root, path,
  2506. &BTRFS_I(inode)->location, 0);
  2507. if (ret < 0) {
  2508. err = ret;
  2509. goto out;
  2510. }
  2511. if (ret == 0) {
  2512. if (check_path_shared(root, path))
  2513. goto out;
  2514. } else {
  2515. check_link = 0;
  2516. }
  2517. btrfs_release_path(root, path);
  2518. if (ret == 0 && S_ISREG(inode->i_mode)) {
  2519. ret = btrfs_lookup_file_extent(trans, root, path,
  2520. inode->i_ino, (u64)-1, 0);
  2521. if (ret < 0) {
  2522. err = ret;
  2523. goto out;
  2524. }
  2525. BUG_ON(ret == 0);
  2526. if (check_path_shared(root, path))
  2527. goto out;
  2528. btrfs_release_path(root, path);
  2529. }
  2530. if (!check_link) {
  2531. err = 0;
  2532. goto out;
  2533. }
  2534. di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
  2535. dentry->d_name.name, dentry->d_name.len, 0);
  2536. if (IS_ERR(di)) {
  2537. err = PTR_ERR(di);
  2538. goto out;
  2539. }
  2540. if (di) {
  2541. if (check_path_shared(root, path))
  2542. goto out;
  2543. } else {
  2544. err = 0;
  2545. goto out;
  2546. }
  2547. btrfs_release_path(root, path);
  2548. ref = btrfs_lookup_inode_ref(trans, root, path,
  2549. dentry->d_name.name, dentry->d_name.len,
  2550. inode->i_ino, dir->i_ino, 0);
  2551. if (IS_ERR(ref)) {
  2552. err = PTR_ERR(ref);
  2553. goto out;
  2554. }
  2555. BUG_ON(!ref);
  2556. if (check_path_shared(root, path))
  2557. goto out;
  2558. index = btrfs_inode_ref_index(path->nodes[0], ref);
  2559. btrfs_release_path(root, path);
  2560. di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, index,
  2561. dentry->d_name.name, dentry->d_name.len, 0);
  2562. if (IS_ERR(di)) {
  2563. err = PTR_ERR(di);
  2564. goto out;
  2565. }
  2566. BUG_ON(ret == -ENOENT);
  2567. if (check_path_shared(root, path))
  2568. goto out;
  2569. err = 0;
  2570. out:
  2571. btrfs_free_path(path);
  2572. if (err) {
  2573. btrfs_end_transaction(trans, root);
  2574. root->fs_info->enospc_unlink = 0;
  2575. return ERR_PTR(err);
  2576. }
  2577. trans->block_rsv = &root->fs_info->global_block_rsv;
  2578. return trans;
  2579. }
  2580. static void __unlink_end_trans(struct btrfs_trans_handle *trans,
  2581. struct btrfs_root *root)
  2582. {
  2583. if (trans->block_rsv == &root->fs_info->global_block_rsv) {
  2584. BUG_ON(!root->fs_info->enospc_unlink);
  2585. root->fs_info->enospc_unlink = 0;
  2586. }
  2587. btrfs_end_transaction_throttle(trans, root);
  2588. }
  2589. static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
  2590. {
  2591. struct btrfs_root *root = BTRFS_I(dir)->root;
  2592. struct btrfs_trans_handle *trans;
  2593. struct inode *inode = dentry->d_inode;
  2594. int ret;
  2595. unsigned long nr = 0;
  2596. trans = __unlink_start_trans(dir, dentry);
  2597. if (IS_ERR(trans))
  2598. return PTR_ERR(trans);
  2599. btrfs_set_trans_block_group(trans, dir);
  2600. btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
  2601. ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
  2602. dentry->d_name.name, dentry->d_name.len);
  2603. BUG_ON(ret);
  2604. if (inode->i_nlink == 0) {
  2605. ret = btrfs_orphan_add(trans, inode);
  2606. BUG_ON(ret);
  2607. }
  2608. nr = trans->blocks_used;
  2609. __unlink_end_trans(trans, root);
  2610. btrfs_btree_balance_dirty(root, nr);
  2611. return ret;
  2612. }
  2613. int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
  2614. struct btrfs_root *root,
  2615. struct inode *dir, u64 objectid,
  2616. const char *name, int name_len)
  2617. {
  2618. struct btrfs_path *path;
  2619. struct extent_buffer *leaf;
  2620. struct btrfs_dir_item *di;
  2621. struct btrfs_key key;
  2622. u64 index;
  2623. int ret;
  2624. path = btrfs_alloc_path();
  2625. if (!path)
  2626. return -ENOMEM;
  2627. di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
  2628. name, name_len, -1);
  2629. BUG_ON(!di || IS_ERR(di));
  2630. leaf = path->nodes[0];
  2631. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  2632. WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
  2633. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  2634. BUG_ON(ret);
  2635. btrfs_release_path(root, path);
  2636. ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
  2637. objectid, root->root_key.objectid,
  2638. dir->i_ino, &index, name, name_len);
  2639. if (ret < 0) {
  2640. BUG_ON(ret != -ENOENT);
  2641. di = btrfs_search_dir_index_item(root, path, dir->i_ino,
  2642. name, name_len);
  2643. BUG_ON(!di || IS_ERR(di));
  2644. leaf = path->nodes[0];
  2645. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  2646. btrfs_release_path(root, path);
  2647. index = key.offset;
  2648. }
  2649. di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
  2650. index, name, name_len, -1);
  2651. BUG_ON(!di || IS_ERR(di));
  2652. leaf = path->nodes[0];
  2653. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  2654. WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
  2655. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  2656. BUG_ON(ret);
  2657. btrfs_release_path(root, path);
  2658. btrfs_i_size_write(dir, dir->i_size - name_len * 2);
  2659. dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  2660. ret = btrfs_update_inode(trans, root, dir);
  2661. BUG_ON(ret);
  2662. btrfs_free_path(path);
  2663. return 0;
  2664. }
  2665. static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
  2666. {
  2667. struct inode *inode = dentry->d_inode;
  2668. int err = 0;
  2669. struct btrfs_root *root = BTRFS_I(dir)->root;
  2670. struct btrfs_trans_handle *trans;
  2671. unsigned long nr = 0;
  2672. if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
  2673. inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
  2674. return -ENOTEMPTY;
  2675. trans = __unlink_start_trans(dir, dentry);
  2676. if (IS_ERR(trans))
  2677. return PTR_ERR(trans);
  2678. btrfs_set_trans_block_group(trans, dir);
  2679. if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
  2680. err = btrfs_unlink_subvol(trans, root, dir,
  2681. BTRFS_I(inode)->location.objectid,
  2682. dentry->d_name.name,
  2683. dentry->d_name.len);
  2684. goto out;
  2685. }
  2686. err = btrfs_orphan_add(trans, inode);
  2687. if (err)
  2688. goto out;
  2689. /* now the directory is empty */
  2690. err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
  2691. dentry->d_name.name, dentry->d_name.len);
  2692. if (!err)
  2693. btrfs_i_size_write(inode, 0);
  2694. out:
  2695. nr = trans->blocks_used;
  2696. __unlink_end_trans(trans, root);
  2697. btrfs_btree_balance_dirty(root, nr);
  2698. return err;
  2699. }
  2700. #if 0
  2701. /*
  2702. * when truncating bytes in a file, it is possible to avoid reading
  2703. * the leaves that contain only checksum items. This can be the
  2704. * majority of the IO required to delete a large file, but it must
  2705. * be done carefully.
  2706. *
  2707. * The keys in the level just above the leaves are checked to make sure
  2708. * the lowest key in a given leaf is a csum key, and starts at an offset
  2709. * after the new size.
  2710. *
  2711. * Then the key for the next leaf is checked to make sure it also has
  2712. * a checksum item for the same file. If it does, we know our target leaf
  2713. * contains only checksum items, and it can be safely freed without reading
  2714. * it.
  2715. *
  2716. * This is just an optimization targeted at large files. It may do
  2717. * nothing. It will return 0 unless things went badly.
  2718. */
  2719. static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
  2720. struct btrfs_root *root,
  2721. struct btrfs_path *path,
  2722. struct inode *inode, u64 new_size)
  2723. {
  2724. struct btrfs_key key;
  2725. int ret;
  2726. int nritems;
  2727. struct btrfs_key found_key;
  2728. struct btrfs_key other_key;
  2729. struct btrfs_leaf_ref *ref;
  2730. u64 leaf_gen;
  2731. u64 leaf_start;
  2732. path->lowest_level = 1;
  2733. key.objectid = inode->i_ino;
  2734. key.type = BTRFS_CSUM_ITEM_KEY;
  2735. key.offset = new_size;
  2736. again:
  2737. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  2738. if (ret < 0)
  2739. goto out;
  2740. if (path->nodes[1] == NULL) {
  2741. ret = 0;
  2742. goto out;
  2743. }
  2744. ret = 0;
  2745. btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
  2746. nritems = btrfs_header_nritems(path->nodes[1]);
  2747. if (!nritems)
  2748. goto out;
  2749. if (path->slots[1] >= nritems)
  2750. goto next_node;
  2751. /* did we find a key greater than anything we want to delete? */
  2752. if (found_key.objectid > inode->i_ino ||
  2753. (found_key.objectid == inode->i_ino && found_key.type > key.type))
  2754. goto out;
  2755. /* we check the next key in the node to make sure the leave contains
  2756. * only checksum items. This comparison doesn't work if our
  2757. * leaf is the last one in the node
  2758. */
  2759. if (path->slots[1] + 1 >= nritems) {
  2760. next_node:
  2761. /* search forward from the last key in the node, this
  2762. * will bring us into the next node in the tree
  2763. */
  2764. btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
  2765. /* unlikely, but we inc below, so check to be safe */
  2766. if (found_key.offset == (u64)-1)
  2767. goto out;
  2768. /* search_forward needs a path with locks held, do the
  2769. * search again for the original key. It is possible
  2770. * this will race with a balance and return a path that
  2771. * we could modify, but this drop is just an optimization
  2772. * and is allowed to miss some leaves.
  2773. */
  2774. btrfs_release_path(root, path);
  2775. found_key.offset++;
  2776. /* setup a max key for search_forward */
  2777. other_key.offset = (u64)-1;
  2778. other_key.type = key.type;
  2779. other_key.objectid = key.objectid;
  2780. path->keep_locks = 1;
  2781. ret = btrfs_search_forward(root, &found_key, &other_key,
  2782. path, 0, 0);
  2783. path->keep_locks = 0;
  2784. if (ret || found_key.objectid != key.objectid ||
  2785. found_key.type != key.type) {
  2786. ret = 0;
  2787. goto out;
  2788. }
  2789. key.offset = found_key.offset;
  2790. btrfs_release_path(root, path);
  2791. cond_resched();
  2792. goto again;
  2793. }
  2794. /* we know there's one more slot after us in the tree,
  2795. * read that key so we can verify it is also a checksum item
  2796. */
  2797. btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
  2798. if (found_key.objectid < inode->i_ino)
  2799. goto next_key;
  2800. if (found_key.type != key.type || found_key.offset < new_size)
  2801. goto next_key;
  2802. /*
  2803. * if the key for the next leaf isn't a csum key from this objectid,
  2804. * we can't be sure there aren't good items inside this leaf.
  2805. * Bail out
  2806. */
  2807. if (other_key.objectid != inode->i_ino || other_key.type != key.type)
  2808. goto out;
  2809. leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
  2810. leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
  2811. /*
  2812. * it is safe to delete this leaf, it contains only
  2813. * csum items from this inode at an offset >= new_size
  2814. */
  2815. ret = btrfs_del_leaf(trans, root, path, leaf_start);
  2816. BUG_ON(ret);
  2817. if (root->ref_cows && leaf_gen < trans->transid) {
  2818. ref = btrfs_alloc_leaf_ref(root, 0);
  2819. if (ref) {
  2820. ref->root_gen = root->root_key.offset;
  2821. ref->bytenr = leaf_start;
  2822. ref->owner = 0;
  2823. ref->generation = leaf_gen;
  2824. ref->nritems = 0;
  2825. btrfs_sort_leaf_ref(ref);
  2826. ret = btrfs_add_leaf_ref(root, ref, 0);
  2827. WARN_ON(ret);
  2828. btrfs_free_leaf_ref(root, ref);
  2829. } else {
  2830. WARN_ON(1);
  2831. }
  2832. }
  2833. next_key:
  2834. btrfs_release_path(root, path);
  2835. if (other_key.objectid == inode->i_ino &&
  2836. other_key.type == key.type && other_key.offset > key.offset) {
  2837. key.offset = other_key.offset;
  2838. cond_resched();
  2839. goto again;
  2840. }
  2841. ret = 0;
  2842. out:
  2843. /* fixup any changes we've made to the path */
  2844. path->lowest_level = 0;
  2845. path->keep_locks = 0;
  2846. btrfs_release_path(root, path);
  2847. return ret;
  2848. }
  2849. #endif
  2850. /*
  2851. * this can truncate away extent items, csum items and directory items.
  2852. * It starts at a high offset and removes keys until it can't find
  2853. * any higher than new_size
  2854. *
  2855. * csum items that cross the new i_size are truncated to the new size
  2856. * as well.
  2857. *
  2858. * min_type is the minimum key type to truncate down to. If set to 0, this
  2859. * will kill all the items on this inode, including the INODE_ITEM_KEY.
  2860. */
  2861. int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
  2862. struct btrfs_root *root,
  2863. struct inode *inode,
  2864. u64 new_size, u32 min_type)
  2865. {
  2866. struct btrfs_path *path;
  2867. struct extent_buffer *leaf;
  2868. struct btrfs_file_extent_item *fi;
  2869. struct btrfs_key key;
  2870. struct btrfs_key found_key;
  2871. u64 extent_start = 0;
  2872. u64 extent_num_bytes = 0;
  2873. u64 extent_offset = 0;
  2874. u64 item_end = 0;
  2875. u64 mask = root->sectorsize - 1;
  2876. u32 found_type = (u8)-1;
  2877. int found_extent;
  2878. int del_item;
  2879. int pending_del_nr = 0;
  2880. int pending_del_slot = 0;
  2881. int extent_type = -1;
  2882. int encoding;
  2883. int ret;
  2884. int err = 0;
  2885. BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
  2886. if (root->ref_cows || root == root->fs_info->tree_root)
  2887. btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
  2888. path = btrfs_alloc_path();
  2889. BUG_ON(!path);
  2890. path->reada = -1;
  2891. key.objectid = inode->i_ino;
  2892. key.offset = (u64)-1;
  2893. key.type = (u8)-1;
  2894. search_again:
  2895. path->leave_spinning = 1;
  2896. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  2897. if (ret < 0) {
  2898. err = ret;
  2899. goto out;
  2900. }
  2901. if (ret > 0) {
  2902. /* there are no items in the tree for us to truncate, we're
  2903. * done
  2904. */
  2905. if (path->slots[0] == 0)
  2906. goto out;
  2907. path->slots[0]--;
  2908. }
  2909. while (1) {
  2910. fi = NULL;
  2911. leaf = path->nodes[0];
  2912. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2913. found_type = btrfs_key_type(&found_key);
  2914. encoding = 0;
  2915. if (found_key.objectid != inode->i_ino)
  2916. break;
  2917. if (found_type < min_type)
  2918. break;
  2919. item_end = found_key.offset;
  2920. if (found_type == BTRFS_EXTENT_DATA_KEY) {
  2921. fi = btrfs_item_ptr(leaf, path->slots[0],
  2922. struct btrfs_file_extent_item);
  2923. extent_type = btrfs_file_extent_type(leaf, fi);
  2924. encoding = btrfs_file_extent_compression(leaf, fi);
  2925. encoding |= btrfs_file_extent_encryption(leaf, fi);
  2926. encoding |= btrfs_file_extent_other_encoding(leaf, fi);
  2927. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  2928. item_end +=
  2929. btrfs_file_extent_num_bytes(leaf, fi);
  2930. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  2931. item_end += btrfs_file_extent_inline_len(leaf,
  2932. fi);
  2933. }
  2934. item_end--;
  2935. }
  2936. if (found_type > min_type) {
  2937. del_item = 1;
  2938. } else {
  2939. if (item_end < new_size)
  2940. break;
  2941. if (found_key.offset >= new_size)
  2942. del_item = 1;
  2943. else
  2944. del_item = 0;
  2945. }
  2946. found_extent = 0;
  2947. /* FIXME, shrink the extent if the ref count is only 1 */
  2948. if (found_type != BTRFS_EXTENT_DATA_KEY)
  2949. goto delete;
  2950. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  2951. u64 num_dec;
  2952. extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
  2953. if (!del_item && !encoding) {
  2954. u64 orig_num_bytes =
  2955. btrfs_file_extent_num_bytes(leaf, fi);
  2956. extent_num_bytes = new_size -
  2957. found_key.offset + root->sectorsize - 1;
  2958. extent_num_bytes = extent_num_bytes &
  2959. ~((u64)root->sectorsize - 1);
  2960. btrfs_set_file_extent_num_bytes(leaf, fi,
  2961. extent_num_bytes);
  2962. num_dec = (orig_num_bytes -
  2963. extent_num_bytes);
  2964. if (root->ref_cows && extent_start != 0)
  2965. inode_sub_bytes(inode, num_dec);
  2966. btrfs_mark_buffer_dirty(leaf);
  2967. } else {
  2968. extent_num_bytes =
  2969. btrfs_file_extent_disk_num_bytes(leaf,
  2970. fi);
  2971. extent_offset = found_key.offset -
  2972. btrfs_file_extent_offset(leaf, fi);
  2973. /* FIXME blocksize != 4096 */
  2974. num_dec = btrfs_file_extent_num_bytes(leaf, fi);
  2975. if (extent_start != 0) {
  2976. found_extent = 1;
  2977. if (root->ref_cows)
  2978. inode_sub_bytes(inode, num_dec);
  2979. }
  2980. }
  2981. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  2982. /*
  2983. * we can't truncate inline items that have had
  2984. * special encodings
  2985. */
  2986. if (!del_item &&
  2987. btrfs_file_extent_compression(leaf, fi) == 0 &&
  2988. btrfs_file_extent_encryption(leaf, fi) == 0 &&
  2989. btrfs_file_extent_other_encoding(leaf, fi) == 0) {
  2990. u32 size = new_size - found_key.offset;
  2991. if (root->ref_cows) {
  2992. inode_sub_bytes(inode, item_end + 1 -
  2993. new_size);
  2994. }
  2995. size =
  2996. btrfs_file_extent_calc_inline_size(size);
  2997. ret = btrfs_truncate_item(trans, root, path,
  2998. size, 1);
  2999. BUG_ON(ret);
  3000. } else if (root->ref_cows) {
  3001. inode_sub_bytes(inode, item_end + 1 -
  3002. found_key.offset);
  3003. }
  3004. }
  3005. delete:
  3006. if (del_item) {
  3007. if (!pending_del_nr) {
  3008. /* no pending yet, add ourselves */
  3009. pending_del_slot = path->slots[0];
  3010. pending_del_nr = 1;
  3011. } else if (pending_del_nr &&
  3012. path->slots[0] + 1 == pending_del_slot) {
  3013. /* hop on the pending chunk */
  3014. pending_del_nr++;
  3015. pending_del_slot = path->slots[0];
  3016. } else {
  3017. BUG();
  3018. }
  3019. } else {
  3020. break;
  3021. }
  3022. if (found_extent && (root->ref_cows ||
  3023. root == root->fs_info->tree_root)) {
  3024. btrfs_set_path_blocking(path);
  3025. ret = btrfs_free_extent(trans, root, extent_start,
  3026. extent_num_bytes, 0,
  3027. btrfs_header_owner(leaf),
  3028. inode->i_ino, extent_offset);
  3029. BUG_ON(ret);
  3030. }
  3031. if (found_type == BTRFS_INODE_ITEM_KEY)
  3032. break;
  3033. if (path->slots[0] == 0 ||
  3034. path->slots[0] != pending_del_slot) {
  3035. if (root->ref_cows) {
  3036. err = -EAGAIN;
  3037. goto out;
  3038. }
  3039. if (pending_del_nr) {
  3040. ret = btrfs_del_items(trans, root, path,
  3041. pending_del_slot,
  3042. pending_del_nr);
  3043. BUG_ON(ret);
  3044. pending_del_nr = 0;
  3045. }
  3046. btrfs_release_path(root, path);
  3047. goto search_again;
  3048. } else {
  3049. path->slots[0]--;
  3050. }
  3051. }
  3052. out:
  3053. if (pending_del_nr) {
  3054. ret = btrfs_del_items(trans, root, path, pending_del_slot,
  3055. pending_del_nr);
  3056. BUG_ON(ret);
  3057. }
  3058. btrfs_free_path(path);
  3059. return err;
  3060. }
  3061. /*
  3062. * taken from block_truncate_page, but does cow as it zeros out
  3063. * any bytes left in the last page in the file.
  3064. */
  3065. static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
  3066. {
  3067. struct inode *inode = mapping->host;
  3068. struct btrfs_root *root = BTRFS_I(inode)->root;
  3069. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  3070. struct btrfs_ordered_extent *ordered;
  3071. struct extent_state *cached_state = NULL;
  3072. char *kaddr;
  3073. u32 blocksize = root->sectorsize;
  3074. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  3075. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  3076. struct page *page;
  3077. int ret = 0;
  3078. u64 page_start;
  3079. u64 page_end;
  3080. if ((offset & (blocksize - 1)) == 0)
  3081. goto out;
  3082. ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
  3083. if (ret)
  3084. goto out;
  3085. ret = -ENOMEM;
  3086. again:
  3087. page = grab_cache_page(mapping, index);
  3088. if (!page) {
  3089. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  3090. goto out;
  3091. }
  3092. page_start = page_offset(page);
  3093. page_end = page_start + PAGE_CACHE_SIZE - 1;
  3094. if (!PageUptodate(page)) {
  3095. ret = btrfs_readpage(NULL, page);
  3096. lock_page(page);
  3097. if (page->mapping != mapping) {
  3098. unlock_page(page);
  3099. page_cache_release(page);
  3100. goto again;
  3101. }
  3102. if (!PageUptodate(page)) {
  3103. ret = -EIO;
  3104. goto out_unlock;
  3105. }
  3106. }
  3107. wait_on_page_writeback(page);
  3108. lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
  3109. GFP_NOFS);
  3110. set_page_extent_mapped(page);
  3111. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  3112. if (ordered) {
  3113. unlock_extent_cached(io_tree, page_start, page_end,
  3114. &cached_state, GFP_NOFS);
  3115. unlock_page(page);
  3116. page_cache_release(page);
  3117. btrfs_start_ordered_extent(inode, ordered, 1);
  3118. btrfs_put_ordered_extent(ordered);
  3119. goto again;
  3120. }
  3121. clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
  3122. EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
  3123. 0, 0, &cached_state, GFP_NOFS);
  3124. ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
  3125. &cached_state);
  3126. if (ret) {
  3127. unlock_extent_cached(io_tree, page_start, page_end,
  3128. &cached_state, GFP_NOFS);
  3129. goto out_unlock;
  3130. }
  3131. ret = 0;
  3132. if (offset != PAGE_CACHE_SIZE) {
  3133. kaddr = kmap(page);
  3134. memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
  3135. flush_dcache_page(page);
  3136. kunmap(page);
  3137. }
  3138. ClearPageChecked(page);
  3139. set_page_dirty(page);
  3140. unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
  3141. GFP_NOFS);
  3142. out_unlock:
  3143. if (ret)
  3144. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  3145. unlock_page(page);
  3146. page_cache_release(page);
  3147. out:
  3148. return ret;
  3149. }
  3150. /*
  3151. * This function puts in dummy file extents for the area we're creating a hole
  3152. * for. So if we are truncating this file to a larger size we need to insert
  3153. * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
  3154. * the range between oldsize and size
  3155. */
  3156. int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
  3157. {
  3158. struct btrfs_trans_handle *trans;
  3159. struct btrfs_root *root = BTRFS_I(inode)->root;
  3160. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  3161. struct extent_map *em = NULL;
  3162. struct extent_state *cached_state = NULL;
  3163. u64 mask = root->sectorsize - 1;
  3164. u64 hole_start = (oldsize + mask) & ~mask;
  3165. u64 block_end = (size + mask) & ~mask;
  3166. u64 last_byte;
  3167. u64 cur_offset;
  3168. u64 hole_size;
  3169. int err = 0;
  3170. if (size <= hole_start)
  3171. return 0;
  3172. while (1) {
  3173. struct btrfs_ordered_extent *ordered;
  3174. btrfs_wait_ordered_range(inode, hole_start,
  3175. block_end - hole_start);
  3176. lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
  3177. &cached_state, GFP_NOFS);
  3178. ordered = btrfs_lookup_ordered_extent(inode, hole_start);
  3179. if (!ordered)
  3180. break;
  3181. unlock_extent_cached(io_tree, hole_start, block_end - 1,
  3182. &cached_state, GFP_NOFS);
  3183. btrfs_put_ordered_extent(ordered);
  3184. }
  3185. cur_offset = hole_start;
  3186. while (1) {
  3187. em = btrfs_get_extent(inode, NULL, 0, cur_offset,
  3188. block_end - cur_offset, 0);
  3189. BUG_ON(IS_ERR(em) || !em);
  3190. last_byte = min(extent_map_end(em), block_end);
  3191. last_byte = (last_byte + mask) & ~mask;
  3192. if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
  3193. u64 hint_byte = 0;
  3194. hole_size = last_byte - cur_offset;
  3195. trans = btrfs_start_transaction(root, 2);
  3196. if (IS_ERR(trans)) {
  3197. err = PTR_ERR(trans);
  3198. break;
  3199. }
  3200. btrfs_set_trans_block_group(trans, inode);
  3201. err = btrfs_drop_extents(trans, inode, cur_offset,
  3202. cur_offset + hole_size,
  3203. &hint_byte, 1);
  3204. if (err)
  3205. break;
  3206. err = btrfs_insert_file_extent(trans, root,
  3207. inode->i_ino, cur_offset, 0,
  3208. 0, hole_size, 0, hole_size,
  3209. 0, 0, 0);
  3210. if (err)
  3211. break;
  3212. btrfs_drop_extent_cache(inode, hole_start,
  3213. last_byte - 1, 0);
  3214. btrfs_end_transaction(trans, root);
  3215. }
  3216. free_extent_map(em);
  3217. em = NULL;
  3218. cur_offset = last_byte;
  3219. if (cur_offset >= block_end)
  3220. break;
  3221. }
  3222. free_extent_map(em);
  3223. unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
  3224. GFP_NOFS);
  3225. return err;
  3226. }
  3227. static int btrfs_setsize(struct inode *inode, loff_t newsize)
  3228. {
  3229. loff_t oldsize = i_size_read(inode);
  3230. int ret;
  3231. if (newsize == oldsize)
  3232. return 0;
  3233. if (newsize > oldsize) {
  3234. i_size_write(inode, newsize);
  3235. btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
  3236. truncate_pagecache(inode, oldsize, newsize);
  3237. ret = btrfs_cont_expand(inode, oldsize, newsize);
  3238. if (ret) {
  3239. btrfs_setsize(inode, oldsize);
  3240. return ret;
  3241. }
  3242. mark_inode_dirty(inode);
  3243. } else {
  3244. /*
  3245. * We're truncating a file that used to have good data down to
  3246. * zero. Make sure it gets into the ordered flush list so that
  3247. * any new writes get down to disk quickly.
  3248. */
  3249. if (newsize == 0)
  3250. BTRFS_I(inode)->ordered_data_close = 1;
  3251. /* we don't support swapfiles, so vmtruncate shouldn't fail */
  3252. truncate_setsize(inode, newsize);
  3253. ret = btrfs_truncate(inode);
  3254. }
  3255. return ret;
  3256. }
  3257. static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
  3258. {
  3259. struct inode *inode = dentry->d_inode;
  3260. struct btrfs_root *root = BTRFS_I(inode)->root;
  3261. int err;
  3262. if (btrfs_root_readonly(root))
  3263. return -EROFS;
  3264. err = inode_change_ok(inode, attr);
  3265. if (err)
  3266. return err;
  3267. if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
  3268. err = btrfs_setsize(inode, attr->ia_size);
  3269. if (err)
  3270. return err;
  3271. }
  3272. if (attr->ia_valid) {
  3273. setattr_copy(inode, attr);
  3274. mark_inode_dirty(inode);
  3275. if (attr->ia_valid & ATTR_MODE)
  3276. err = btrfs_acl_chmod(inode);
  3277. }
  3278. return err;
  3279. }
  3280. void btrfs_evict_inode(struct inode *inode)
  3281. {
  3282. struct btrfs_trans_handle *trans;
  3283. struct btrfs_root *root = BTRFS_I(inode)->root;
  3284. unsigned long nr;
  3285. int ret;
  3286. trace_btrfs_inode_evict(inode);
  3287. truncate_inode_pages(&inode->i_data, 0);
  3288. if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
  3289. root == root->fs_info->tree_root))
  3290. goto no_delete;
  3291. if (is_bad_inode(inode)) {
  3292. btrfs_orphan_del(NULL, inode);
  3293. goto no_delete;
  3294. }
  3295. /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
  3296. btrfs_wait_ordered_range(inode, 0, (u64)-1);
  3297. if (root->fs_info->log_root_recovering) {
  3298. BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan));
  3299. goto no_delete;
  3300. }
  3301. if (inode->i_nlink > 0) {
  3302. BUG_ON(btrfs_root_refs(&root->root_item) != 0);
  3303. goto no_delete;
  3304. }
  3305. btrfs_i_size_write(inode, 0);
  3306. while (1) {
  3307. trans = btrfs_start_transaction(root, 0);
  3308. BUG_ON(IS_ERR(trans));
  3309. btrfs_set_trans_block_group(trans, inode);
  3310. trans->block_rsv = root->orphan_block_rsv;
  3311. ret = btrfs_block_rsv_check(trans, root,
  3312. root->orphan_block_rsv, 0, 5);
  3313. if (ret) {
  3314. BUG_ON(ret != -EAGAIN);
  3315. ret = btrfs_commit_transaction(trans, root);
  3316. BUG_ON(ret);
  3317. continue;
  3318. }
  3319. ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
  3320. if (ret != -EAGAIN)
  3321. break;
  3322. nr = trans->blocks_used;
  3323. btrfs_end_transaction(trans, root);
  3324. trans = NULL;
  3325. btrfs_btree_balance_dirty(root, nr);
  3326. }
  3327. if (ret == 0) {
  3328. ret = btrfs_orphan_del(trans, inode);
  3329. BUG_ON(ret);
  3330. }
  3331. nr = trans->blocks_used;
  3332. btrfs_end_transaction(trans, root);
  3333. btrfs_btree_balance_dirty(root, nr);
  3334. no_delete:
  3335. end_writeback(inode);
  3336. return;
  3337. }
  3338. /*
  3339. * this returns the key found in the dir entry in the location pointer.
  3340. * If no dir entries were found, location->objectid is 0.
  3341. */
  3342. static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
  3343. struct btrfs_key *location)
  3344. {
  3345. const char *name = dentry->d_name.name;
  3346. int namelen = dentry->d_name.len;
  3347. struct btrfs_dir_item *di;
  3348. struct btrfs_path *path;
  3349. struct btrfs_root *root = BTRFS_I(dir)->root;
  3350. int ret = 0;
  3351. path = btrfs_alloc_path();
  3352. BUG_ON(!path);
  3353. di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
  3354. namelen, 0);
  3355. if (IS_ERR(di))
  3356. ret = PTR_ERR(di);
  3357. if (!di || IS_ERR(di))
  3358. goto out_err;
  3359. btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
  3360. out:
  3361. btrfs_free_path(path);
  3362. return ret;
  3363. out_err:
  3364. location->objectid = 0;
  3365. goto out;
  3366. }
  3367. /*
  3368. * when we hit a tree root in a directory, the btrfs part of the inode
  3369. * needs to be changed to reflect the root directory of the tree root. This
  3370. * is kind of like crossing a mount point.
  3371. */
  3372. static int fixup_tree_root_location(struct btrfs_root *root,
  3373. struct inode *dir,
  3374. struct dentry *dentry,
  3375. struct btrfs_key *location,
  3376. struct btrfs_root **sub_root)
  3377. {
  3378. struct btrfs_path *path;
  3379. struct btrfs_root *new_root;
  3380. struct btrfs_root_ref *ref;
  3381. struct extent_buffer *leaf;
  3382. int ret;
  3383. int err = 0;
  3384. path = btrfs_alloc_path();
  3385. if (!path) {
  3386. err = -ENOMEM;
  3387. goto out;
  3388. }
  3389. err = -ENOENT;
  3390. ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
  3391. BTRFS_I(dir)->root->root_key.objectid,
  3392. location->objectid);
  3393. if (ret) {
  3394. if (ret < 0)
  3395. err = ret;
  3396. goto out;
  3397. }
  3398. leaf = path->nodes[0];
  3399. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
  3400. if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino ||
  3401. btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
  3402. goto out;
  3403. ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
  3404. (unsigned long)(ref + 1),
  3405. dentry->d_name.len);
  3406. if (ret)
  3407. goto out;
  3408. btrfs_release_path(root->fs_info->tree_root, path);
  3409. new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
  3410. if (IS_ERR(new_root)) {
  3411. err = PTR_ERR(new_root);
  3412. goto out;
  3413. }
  3414. if (btrfs_root_refs(&new_root->root_item) == 0) {
  3415. err = -ENOENT;
  3416. goto out;
  3417. }
  3418. *sub_root = new_root;
  3419. location->objectid = btrfs_root_dirid(&new_root->root_item);
  3420. location->type = BTRFS_INODE_ITEM_KEY;
  3421. location->offset = 0;
  3422. err = 0;
  3423. out:
  3424. btrfs_free_path(path);
  3425. return err;
  3426. }
  3427. static void inode_tree_add(struct inode *inode)
  3428. {
  3429. struct btrfs_root *root = BTRFS_I(inode)->root;
  3430. struct btrfs_inode *entry;
  3431. struct rb_node **p;
  3432. struct rb_node *parent;
  3433. again:
  3434. p = &root->inode_tree.rb_node;
  3435. parent = NULL;
  3436. if (inode_unhashed(inode))
  3437. return;
  3438. spin_lock(&root->inode_lock);
  3439. while (*p) {
  3440. parent = *p;
  3441. entry = rb_entry(parent, struct btrfs_inode, rb_node);
  3442. if (inode->i_ino < entry->vfs_inode.i_ino)
  3443. p = &parent->rb_left;
  3444. else if (inode->i_ino > entry->vfs_inode.i_ino)
  3445. p = &parent->rb_right;
  3446. else {
  3447. WARN_ON(!(entry->vfs_inode.i_state &
  3448. (I_WILL_FREE | I_FREEING)));
  3449. rb_erase(parent, &root->inode_tree);
  3450. RB_CLEAR_NODE(parent);
  3451. spin_unlock(&root->inode_lock);
  3452. goto again;
  3453. }
  3454. }
  3455. rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
  3456. rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
  3457. spin_unlock(&root->inode_lock);
  3458. }
  3459. static void inode_tree_del(struct inode *inode)
  3460. {
  3461. struct btrfs_root *root = BTRFS_I(inode)->root;
  3462. int empty = 0;
  3463. spin_lock(&root->inode_lock);
  3464. if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
  3465. rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
  3466. RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
  3467. empty = RB_EMPTY_ROOT(&root->inode_tree);
  3468. }
  3469. spin_unlock(&root->inode_lock);
  3470. /*
  3471. * Free space cache has inodes in the tree root, but the tree root has a
  3472. * root_refs of 0, so this could end up dropping the tree root as a
  3473. * snapshot, so we need the extra !root->fs_info->tree_root check to
  3474. * make sure we don't drop it.
  3475. */
  3476. if (empty && btrfs_root_refs(&root->root_item) == 0 &&
  3477. root != root->fs_info->tree_root) {
  3478. synchronize_srcu(&root->fs_info->subvol_srcu);
  3479. spin_lock(&root->inode_lock);
  3480. empty = RB_EMPTY_ROOT(&root->inode_tree);
  3481. spin_unlock(&root->inode_lock);
  3482. if (empty)
  3483. btrfs_add_dead_root(root);
  3484. }
  3485. }
  3486. int btrfs_invalidate_inodes(struct btrfs_root *root)
  3487. {
  3488. struct rb_node *node;
  3489. struct rb_node *prev;
  3490. struct btrfs_inode *entry;
  3491. struct inode *inode;
  3492. u64 objectid = 0;
  3493. WARN_ON(btrfs_root_refs(&root->root_item) != 0);
  3494. spin_lock(&root->inode_lock);
  3495. again:
  3496. node = root->inode_tree.rb_node;
  3497. prev = NULL;
  3498. while (node) {
  3499. prev = node;
  3500. entry = rb_entry(node, struct btrfs_inode, rb_node);
  3501. if (objectid < entry->vfs_inode.i_ino)
  3502. node = node->rb_left;
  3503. else if (objectid > entry->vfs_inode.i_ino)
  3504. node = node->rb_right;
  3505. else
  3506. break;
  3507. }
  3508. if (!node) {
  3509. while (prev) {
  3510. entry = rb_entry(prev, struct btrfs_inode, rb_node);
  3511. if (objectid <= entry->vfs_inode.i_ino) {
  3512. node = prev;
  3513. break;
  3514. }
  3515. prev = rb_next(prev);
  3516. }
  3517. }
  3518. while (node) {
  3519. entry = rb_entry(node, struct btrfs_inode, rb_node);
  3520. objectid = entry->vfs_inode.i_ino + 1;
  3521. inode = igrab(&entry->vfs_inode);
  3522. if (inode) {
  3523. spin_unlock(&root->inode_lock);
  3524. if (atomic_read(&inode->i_count) > 1)
  3525. d_prune_aliases(inode);
  3526. /*
  3527. * btrfs_drop_inode will have it removed from
  3528. * the inode cache when its usage count
  3529. * hits zero.
  3530. */
  3531. iput(inode);
  3532. cond_resched();
  3533. spin_lock(&root->inode_lock);
  3534. goto again;
  3535. }
  3536. if (cond_resched_lock(&root->inode_lock))
  3537. goto again;
  3538. node = rb_next(node);
  3539. }
  3540. spin_unlock(&root->inode_lock);
  3541. return 0;
  3542. }
  3543. static int btrfs_init_locked_inode(struct inode *inode, void *p)
  3544. {
  3545. struct btrfs_iget_args *args = p;
  3546. inode->i_ino = args->ino;
  3547. BTRFS_I(inode)->root = args->root;
  3548. btrfs_set_inode_space_info(args->root, inode);
  3549. return 0;
  3550. }
  3551. static int btrfs_find_actor(struct inode *inode, void *opaque)
  3552. {
  3553. struct btrfs_iget_args *args = opaque;
  3554. return args->ino == inode->i_ino &&
  3555. args->root == BTRFS_I(inode)->root;
  3556. }
  3557. static struct inode *btrfs_iget_locked(struct super_block *s,
  3558. u64 objectid,
  3559. struct btrfs_root *root)
  3560. {
  3561. struct inode *inode;
  3562. struct btrfs_iget_args args;
  3563. args.ino = objectid;
  3564. args.root = root;
  3565. inode = iget5_locked(s, objectid, btrfs_find_actor,
  3566. btrfs_init_locked_inode,
  3567. (void *)&args);
  3568. return inode;
  3569. }
  3570. /* Get an inode object given its location and corresponding root.
  3571. * Returns in *is_new if the inode was read from disk
  3572. */
  3573. struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
  3574. struct btrfs_root *root, int *new)
  3575. {
  3576. struct inode *inode;
  3577. inode = btrfs_iget_locked(s, location->objectid, root);
  3578. if (!inode)
  3579. return ERR_PTR(-ENOMEM);
  3580. if (inode->i_state & I_NEW) {
  3581. BTRFS_I(inode)->root = root;
  3582. memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
  3583. btrfs_read_locked_inode(inode);
  3584. inode_tree_add(inode);
  3585. unlock_new_inode(inode);
  3586. if (new)
  3587. *new = 1;
  3588. }
  3589. return inode;
  3590. }
  3591. static struct inode *new_simple_dir(struct super_block *s,
  3592. struct btrfs_key *key,
  3593. struct btrfs_root *root)
  3594. {
  3595. struct inode *inode = new_inode(s);
  3596. if (!inode)
  3597. return ERR_PTR(-ENOMEM);
  3598. BTRFS_I(inode)->root = root;
  3599. memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
  3600. BTRFS_I(inode)->dummy_inode = 1;
  3601. inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
  3602. inode->i_op = &simple_dir_inode_operations;
  3603. inode->i_fop = &simple_dir_operations;
  3604. inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
  3605. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  3606. return inode;
  3607. }
  3608. struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
  3609. {
  3610. struct inode *inode;
  3611. struct btrfs_root *root = BTRFS_I(dir)->root;
  3612. struct btrfs_root *sub_root = root;
  3613. struct btrfs_key location;
  3614. int index;
  3615. int ret;
  3616. if (dentry->d_name.len > BTRFS_NAME_LEN)
  3617. return ERR_PTR(-ENAMETOOLONG);
  3618. ret = btrfs_inode_by_name(dir, dentry, &location);
  3619. if (ret < 0)
  3620. return ERR_PTR(ret);
  3621. if (location.objectid == 0)
  3622. return NULL;
  3623. if (location.type == BTRFS_INODE_ITEM_KEY) {
  3624. inode = btrfs_iget(dir->i_sb, &location, root, NULL);
  3625. return inode;
  3626. }
  3627. BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
  3628. index = srcu_read_lock(&root->fs_info->subvol_srcu);
  3629. ret = fixup_tree_root_location(root, dir, dentry,
  3630. &location, &sub_root);
  3631. if (ret < 0) {
  3632. if (ret != -ENOENT)
  3633. inode = ERR_PTR(ret);
  3634. else
  3635. inode = new_simple_dir(dir->i_sb, &location, sub_root);
  3636. } else {
  3637. inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
  3638. }
  3639. srcu_read_unlock(&root->fs_info->subvol_srcu, index);
  3640. if (!IS_ERR(inode) && root != sub_root) {
  3641. down_read(&root->fs_info->cleanup_work_sem);
  3642. if (!(inode->i_sb->s_flags & MS_RDONLY))
  3643. ret = btrfs_orphan_cleanup(sub_root);
  3644. up_read(&root->fs_info->cleanup_work_sem);
  3645. if (ret)
  3646. inode = ERR_PTR(ret);
  3647. }
  3648. return inode;
  3649. }
  3650. static int btrfs_dentry_delete(const struct dentry *dentry)
  3651. {
  3652. struct btrfs_root *root;
  3653. if (!dentry->d_inode && !IS_ROOT(dentry))
  3654. dentry = dentry->d_parent;
  3655. if (dentry->d_inode) {
  3656. root = BTRFS_I(dentry->d_inode)->root;
  3657. if (btrfs_root_refs(&root->root_item) == 0)
  3658. return 1;
  3659. }
  3660. return 0;
  3661. }
  3662. static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
  3663. struct nameidata *nd)
  3664. {
  3665. struct inode *inode;
  3666. inode = btrfs_lookup_dentry(dir, dentry);
  3667. if (IS_ERR(inode))
  3668. return ERR_CAST(inode);
  3669. return d_splice_alias(inode, dentry);
  3670. }
  3671. static unsigned char btrfs_filetype_table[] = {
  3672. DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
  3673. };
  3674. static int btrfs_real_readdir(struct file *filp, void *dirent,
  3675. filldir_t filldir)
  3676. {
  3677. struct inode *inode = filp->f_dentry->d_inode;
  3678. struct btrfs_root *root = BTRFS_I(inode)->root;
  3679. struct btrfs_item *item;
  3680. struct btrfs_dir_item *di;
  3681. struct btrfs_key key;
  3682. struct btrfs_key found_key;
  3683. struct btrfs_path *path;
  3684. int ret;
  3685. u32 nritems;
  3686. struct extent_buffer *leaf;
  3687. int slot;
  3688. int advance;
  3689. unsigned char d_type;
  3690. int over = 0;
  3691. u32 di_cur;
  3692. u32 di_total;
  3693. u32 di_len;
  3694. int key_type = BTRFS_DIR_INDEX_KEY;
  3695. char tmp_name[32];
  3696. char *name_ptr;
  3697. int name_len;
  3698. /* FIXME, use a real flag for deciding about the key type */
  3699. if (root->fs_info->tree_root == root)
  3700. key_type = BTRFS_DIR_ITEM_KEY;
  3701. /* special case for "." */
  3702. if (filp->f_pos == 0) {
  3703. over = filldir(dirent, ".", 1,
  3704. 1, inode->i_ino,
  3705. DT_DIR);
  3706. if (over)
  3707. return 0;
  3708. filp->f_pos = 1;
  3709. }
  3710. /* special case for .., just use the back ref */
  3711. if (filp->f_pos == 1) {
  3712. u64 pino = parent_ino(filp->f_path.dentry);
  3713. over = filldir(dirent, "..", 2,
  3714. 2, pino, DT_DIR);
  3715. if (over)
  3716. return 0;
  3717. filp->f_pos = 2;
  3718. }
  3719. path = btrfs_alloc_path();
  3720. path->reada = 2;
  3721. btrfs_set_key_type(&key, key_type);
  3722. key.offset = filp->f_pos;
  3723. key.objectid = inode->i_ino;
  3724. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3725. if (ret < 0)
  3726. goto err;
  3727. advance = 0;
  3728. while (1) {
  3729. leaf = path->nodes[0];
  3730. nritems = btrfs_header_nritems(leaf);
  3731. slot = path->slots[0];
  3732. if (advance || slot >= nritems) {
  3733. if (slot >= nritems - 1) {
  3734. ret = btrfs_next_leaf(root, path);
  3735. if (ret)
  3736. break;
  3737. leaf = path->nodes[0];
  3738. nritems = btrfs_header_nritems(leaf);
  3739. slot = path->slots[0];
  3740. } else {
  3741. slot++;
  3742. path->slots[0]++;
  3743. }
  3744. }
  3745. advance = 1;
  3746. item = btrfs_item_nr(leaf, slot);
  3747. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  3748. if (found_key.objectid != key.objectid)
  3749. break;
  3750. if (btrfs_key_type(&found_key) != key_type)
  3751. break;
  3752. if (found_key.offset < filp->f_pos)
  3753. continue;
  3754. filp->f_pos = found_key.offset;
  3755. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  3756. di_cur = 0;
  3757. di_total = btrfs_item_size(leaf, item);
  3758. while (di_cur < di_total) {
  3759. struct btrfs_key location;
  3760. if (verify_dir_item(root, leaf, di))
  3761. break;
  3762. name_len = btrfs_dir_name_len(leaf, di);
  3763. if (name_len <= sizeof(tmp_name)) {
  3764. name_ptr = tmp_name;
  3765. } else {
  3766. name_ptr = kmalloc(name_len, GFP_NOFS);
  3767. if (!name_ptr) {
  3768. ret = -ENOMEM;
  3769. goto err;
  3770. }
  3771. }
  3772. read_extent_buffer(leaf, name_ptr,
  3773. (unsigned long)(di + 1), name_len);
  3774. d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
  3775. btrfs_dir_item_key_to_cpu(leaf, di, &location);
  3776. /* is this a reference to our own snapshot? If so
  3777. * skip it
  3778. */
  3779. if (location.type == BTRFS_ROOT_ITEM_KEY &&
  3780. location.objectid == root->root_key.objectid) {
  3781. over = 0;
  3782. goto skip;
  3783. }
  3784. over = filldir(dirent, name_ptr, name_len,
  3785. found_key.offset, location.objectid,
  3786. d_type);
  3787. skip:
  3788. if (name_ptr != tmp_name)
  3789. kfree(name_ptr);
  3790. if (over)
  3791. goto nopos;
  3792. di_len = btrfs_dir_name_len(leaf, di) +
  3793. btrfs_dir_data_len(leaf, di) + sizeof(*di);
  3794. di_cur += di_len;
  3795. di = (struct btrfs_dir_item *)((char *)di + di_len);
  3796. }
  3797. }
  3798. /* Reached end of directory/root. Bump pos past the last item. */
  3799. if (key_type == BTRFS_DIR_INDEX_KEY)
  3800. /*
  3801. * 32-bit glibc will use getdents64, but then strtol -
  3802. * so the last number we can serve is this.
  3803. */
  3804. filp->f_pos = 0x7fffffff;
  3805. else
  3806. filp->f_pos++;
  3807. nopos:
  3808. ret = 0;
  3809. err:
  3810. btrfs_free_path(path);
  3811. return ret;
  3812. }
  3813. int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
  3814. {
  3815. struct btrfs_root *root = BTRFS_I(inode)->root;
  3816. struct btrfs_trans_handle *trans;
  3817. int ret = 0;
  3818. bool nolock = false;
  3819. if (BTRFS_I(inode)->dummy_inode)
  3820. return 0;
  3821. smp_mb();
  3822. nolock = (root->fs_info->closing && root == root->fs_info->tree_root);
  3823. if (wbc->sync_mode == WB_SYNC_ALL) {
  3824. if (nolock)
  3825. trans = btrfs_join_transaction_nolock(root, 1);
  3826. else
  3827. trans = btrfs_join_transaction(root, 1);
  3828. if (IS_ERR(trans))
  3829. return PTR_ERR(trans);
  3830. btrfs_set_trans_block_group(trans, inode);
  3831. if (nolock)
  3832. ret = btrfs_end_transaction_nolock(trans, root);
  3833. else
  3834. ret = btrfs_commit_transaction(trans, root);
  3835. }
  3836. return ret;
  3837. }
  3838. /*
  3839. * This is somewhat expensive, updating the tree every time the
  3840. * inode changes. But, it is most likely to find the inode in cache.
  3841. * FIXME, needs more benchmarking...there are no reasons other than performance
  3842. * to keep or drop this code.
  3843. */
  3844. void btrfs_dirty_inode(struct inode *inode)
  3845. {
  3846. struct btrfs_root *root = BTRFS_I(inode)->root;
  3847. struct btrfs_trans_handle *trans;
  3848. int ret;
  3849. if (BTRFS_I(inode)->dummy_inode)
  3850. return;
  3851. trans = btrfs_join_transaction(root, 1);
  3852. BUG_ON(IS_ERR(trans));
  3853. btrfs_set_trans_block_group(trans, inode);
  3854. ret = btrfs_update_inode(trans, root, inode);
  3855. if (ret && ret == -ENOSPC) {
  3856. /* whoops, lets try again with the full transaction */
  3857. btrfs_end_transaction(trans, root);
  3858. trans = btrfs_start_transaction(root, 1);
  3859. if (IS_ERR(trans)) {
  3860. if (printk_ratelimit()) {
  3861. printk(KERN_ERR "btrfs: fail to "
  3862. "dirty inode %lu error %ld\n",
  3863. inode->i_ino, PTR_ERR(trans));
  3864. }
  3865. return;
  3866. }
  3867. btrfs_set_trans_block_group(trans, inode);
  3868. ret = btrfs_update_inode(trans, root, inode);
  3869. if (ret) {
  3870. if (printk_ratelimit()) {
  3871. printk(KERN_ERR "btrfs: fail to "
  3872. "dirty inode %lu error %d\n",
  3873. inode->i_ino, ret);
  3874. }
  3875. }
  3876. }
  3877. btrfs_end_transaction(trans, root);
  3878. }
  3879. /*
  3880. * find the highest existing sequence number in a directory
  3881. * and then set the in-memory index_cnt variable to reflect
  3882. * free sequence numbers
  3883. */
  3884. static int btrfs_set_inode_index_count(struct inode *inode)
  3885. {
  3886. struct btrfs_root *root = BTRFS_I(inode)->root;
  3887. struct btrfs_key key, found_key;
  3888. struct btrfs_path *path;
  3889. struct extent_buffer *leaf;
  3890. int ret;
  3891. key.objectid = inode->i_ino;
  3892. btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
  3893. key.offset = (u64)-1;
  3894. path = btrfs_alloc_path();
  3895. if (!path)
  3896. return -ENOMEM;
  3897. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3898. if (ret < 0)
  3899. goto out;
  3900. /* FIXME: we should be able to handle this */
  3901. if (ret == 0)
  3902. goto out;
  3903. ret = 0;
  3904. /*
  3905. * MAGIC NUMBER EXPLANATION:
  3906. * since we search a directory based on f_pos we have to start at 2
  3907. * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
  3908. * else has to start at 2
  3909. */
  3910. if (path->slots[0] == 0) {
  3911. BTRFS_I(inode)->index_cnt = 2;
  3912. goto out;
  3913. }
  3914. path->slots[0]--;
  3915. leaf = path->nodes[0];
  3916. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3917. if (found_key.objectid != inode->i_ino ||
  3918. btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
  3919. BTRFS_I(inode)->index_cnt = 2;
  3920. goto out;
  3921. }
  3922. BTRFS_I(inode)->index_cnt = found_key.offset + 1;
  3923. out:
  3924. btrfs_free_path(path);
  3925. return ret;
  3926. }
  3927. /*
  3928. * helper to find a free sequence number in a given directory. This current
  3929. * code is very simple, later versions will do smarter things in the btree
  3930. */
  3931. int btrfs_set_inode_index(struct inode *dir, u64 *index)
  3932. {
  3933. int ret = 0;
  3934. if (BTRFS_I(dir)->index_cnt == (u64)-1) {
  3935. ret = btrfs_set_inode_index_count(dir);
  3936. if (ret)
  3937. return ret;
  3938. }
  3939. *index = BTRFS_I(dir)->index_cnt;
  3940. BTRFS_I(dir)->index_cnt++;
  3941. return ret;
  3942. }
  3943. static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
  3944. struct btrfs_root *root,
  3945. struct inode *dir,
  3946. const char *name, int name_len,
  3947. u64 ref_objectid, u64 objectid,
  3948. u64 alloc_hint, int mode, u64 *index)
  3949. {
  3950. struct inode *inode;
  3951. struct btrfs_inode_item *inode_item;
  3952. struct btrfs_key *location;
  3953. struct btrfs_path *path;
  3954. struct btrfs_inode_ref *ref;
  3955. struct btrfs_key key[2];
  3956. u32 sizes[2];
  3957. unsigned long ptr;
  3958. int ret;
  3959. int owner;
  3960. path = btrfs_alloc_path();
  3961. BUG_ON(!path);
  3962. inode = new_inode(root->fs_info->sb);
  3963. if (!inode)
  3964. return ERR_PTR(-ENOMEM);
  3965. if (dir) {
  3966. trace_btrfs_inode_request(dir);
  3967. ret = btrfs_set_inode_index(dir, index);
  3968. if (ret) {
  3969. iput(inode);
  3970. return ERR_PTR(ret);
  3971. }
  3972. }
  3973. /*
  3974. * index_cnt is ignored for everything but a dir,
  3975. * btrfs_get_inode_index_count has an explanation for the magic
  3976. * number
  3977. */
  3978. BTRFS_I(inode)->index_cnt = 2;
  3979. BTRFS_I(inode)->root = root;
  3980. BTRFS_I(inode)->generation = trans->transid;
  3981. inode->i_generation = BTRFS_I(inode)->generation;
  3982. btrfs_set_inode_space_info(root, inode);
  3983. if (mode & S_IFDIR)
  3984. owner = 0;
  3985. else
  3986. owner = 1;
  3987. BTRFS_I(inode)->block_group =
  3988. btrfs_find_block_group(root, 0, alloc_hint, owner);
  3989. key[0].objectid = objectid;
  3990. btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
  3991. key[0].offset = 0;
  3992. key[1].objectid = objectid;
  3993. btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
  3994. key[1].offset = ref_objectid;
  3995. sizes[0] = sizeof(struct btrfs_inode_item);
  3996. sizes[1] = name_len + sizeof(*ref);
  3997. path->leave_spinning = 1;
  3998. ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
  3999. if (ret != 0)
  4000. goto fail;
  4001. inode_init_owner(inode, dir, mode);
  4002. inode->i_ino = objectid;
  4003. inode_set_bytes(inode, 0);
  4004. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  4005. inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  4006. struct btrfs_inode_item);
  4007. fill_inode_item(trans, path->nodes[0], inode_item, inode);
  4008. ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
  4009. struct btrfs_inode_ref);
  4010. btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
  4011. btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
  4012. ptr = (unsigned long)(ref + 1);
  4013. write_extent_buffer(path->nodes[0], name, ptr, name_len);
  4014. btrfs_mark_buffer_dirty(path->nodes[0]);
  4015. btrfs_free_path(path);
  4016. location = &BTRFS_I(inode)->location;
  4017. location->objectid = objectid;
  4018. location->offset = 0;
  4019. btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
  4020. btrfs_inherit_iflags(inode, dir);
  4021. if ((mode & S_IFREG)) {
  4022. if (btrfs_test_opt(root, NODATASUM))
  4023. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
  4024. if (btrfs_test_opt(root, NODATACOW))
  4025. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
  4026. }
  4027. insert_inode_hash(inode);
  4028. inode_tree_add(inode);
  4029. trace_btrfs_inode_new(inode);
  4030. return inode;
  4031. fail:
  4032. if (dir)
  4033. BTRFS_I(dir)->index_cnt--;
  4034. btrfs_free_path(path);
  4035. iput(inode);
  4036. return ERR_PTR(ret);
  4037. }
  4038. static inline u8 btrfs_inode_type(struct inode *inode)
  4039. {
  4040. return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
  4041. }
  4042. /*
  4043. * utility function to add 'inode' into 'parent_inode' with
  4044. * a give name and a given sequence number.
  4045. * if 'add_backref' is true, also insert a backref from the
  4046. * inode to the parent directory.
  4047. */
  4048. int btrfs_add_link(struct btrfs_trans_handle *trans,
  4049. struct inode *parent_inode, struct inode *inode,
  4050. const char *name, int name_len, int add_backref, u64 index)
  4051. {
  4052. int ret = 0;
  4053. struct btrfs_key key;
  4054. struct btrfs_root *root = BTRFS_I(parent_inode)->root;
  4055. if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  4056. memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
  4057. } else {
  4058. key.objectid = inode->i_ino;
  4059. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  4060. key.offset = 0;
  4061. }
  4062. if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  4063. ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
  4064. key.objectid, root->root_key.objectid,
  4065. parent_inode->i_ino,
  4066. index, name, name_len);
  4067. } else if (add_backref) {
  4068. ret = btrfs_insert_inode_ref(trans, root,
  4069. name, name_len, inode->i_ino,
  4070. parent_inode->i_ino, index);
  4071. }
  4072. if (ret == 0) {
  4073. ret = btrfs_insert_dir_item(trans, root, name, name_len,
  4074. parent_inode->i_ino, &key,
  4075. btrfs_inode_type(inode), index);
  4076. BUG_ON(ret);
  4077. btrfs_i_size_write(parent_inode, parent_inode->i_size +
  4078. name_len * 2);
  4079. parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
  4080. ret = btrfs_update_inode(trans, root, parent_inode);
  4081. }
  4082. return ret;
  4083. }
  4084. static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
  4085. struct inode *dir, struct dentry *dentry,
  4086. struct inode *inode, int backref, u64 index)
  4087. {
  4088. int err = btrfs_add_link(trans, dir, inode,
  4089. dentry->d_name.name, dentry->d_name.len,
  4090. backref, index);
  4091. if (!err) {
  4092. d_instantiate(dentry, inode);
  4093. return 0;
  4094. }
  4095. if (err > 0)
  4096. err = -EEXIST;
  4097. return err;
  4098. }
  4099. static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
  4100. int mode, dev_t rdev)
  4101. {
  4102. struct btrfs_trans_handle *trans;
  4103. struct btrfs_root *root = BTRFS_I(dir)->root;
  4104. struct inode *inode = NULL;
  4105. int err;
  4106. int drop_inode = 0;
  4107. u64 objectid;
  4108. unsigned long nr = 0;
  4109. u64 index = 0;
  4110. if (!new_valid_dev(rdev))
  4111. return -EINVAL;
  4112. err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
  4113. if (err)
  4114. return err;
  4115. /*
  4116. * 2 for inode item and ref
  4117. * 2 for dir items
  4118. * 1 for xattr if selinux is on
  4119. */
  4120. trans = btrfs_start_transaction(root, 5);
  4121. if (IS_ERR(trans))
  4122. return PTR_ERR(trans);
  4123. btrfs_set_trans_block_group(trans, dir);
  4124. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  4125. dentry->d_name.len, dir->i_ino, objectid,
  4126. BTRFS_I(dir)->block_group, mode, &index);
  4127. err = PTR_ERR(inode);
  4128. if (IS_ERR(inode))
  4129. goto out_unlock;
  4130. err = btrfs_init_inode_security(trans, inode, dir);
  4131. if (err) {
  4132. drop_inode = 1;
  4133. goto out_unlock;
  4134. }
  4135. btrfs_set_trans_block_group(trans, inode);
  4136. err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
  4137. if (err)
  4138. drop_inode = 1;
  4139. else {
  4140. inode->i_op = &btrfs_special_inode_operations;
  4141. init_special_inode(inode, inode->i_mode, rdev);
  4142. btrfs_update_inode(trans, root, inode);
  4143. }
  4144. btrfs_update_inode_block_group(trans, inode);
  4145. btrfs_update_inode_block_group(trans, dir);
  4146. out_unlock:
  4147. nr = trans->blocks_used;
  4148. btrfs_end_transaction_throttle(trans, root);
  4149. btrfs_btree_balance_dirty(root, nr);
  4150. if (drop_inode) {
  4151. inode_dec_link_count(inode);
  4152. iput(inode);
  4153. }
  4154. return err;
  4155. }
  4156. static int btrfs_create(struct inode *dir, struct dentry *dentry,
  4157. int mode, struct nameidata *nd)
  4158. {
  4159. struct btrfs_trans_handle *trans;
  4160. struct btrfs_root *root = BTRFS_I(dir)->root;
  4161. struct inode *inode = NULL;
  4162. int drop_inode = 0;
  4163. int err;
  4164. unsigned long nr = 0;
  4165. u64 objectid;
  4166. u64 index = 0;
  4167. err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
  4168. if (err)
  4169. return err;
  4170. /*
  4171. * 2 for inode item and ref
  4172. * 2 for dir items
  4173. * 1 for xattr if selinux is on
  4174. */
  4175. trans = btrfs_start_transaction(root, 5);
  4176. if (IS_ERR(trans))
  4177. return PTR_ERR(trans);
  4178. btrfs_set_trans_block_group(trans, dir);
  4179. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  4180. dentry->d_name.len, dir->i_ino, objectid,
  4181. BTRFS_I(dir)->block_group, mode, &index);
  4182. err = PTR_ERR(inode);
  4183. if (IS_ERR(inode))
  4184. goto out_unlock;
  4185. err = btrfs_init_inode_security(trans, inode, dir);
  4186. if (err) {
  4187. drop_inode = 1;
  4188. goto out_unlock;
  4189. }
  4190. btrfs_set_trans_block_group(trans, inode);
  4191. err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
  4192. if (err)
  4193. drop_inode = 1;
  4194. else {
  4195. inode->i_mapping->a_ops = &btrfs_aops;
  4196. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  4197. inode->i_fop = &btrfs_file_operations;
  4198. inode->i_op = &btrfs_file_inode_operations;
  4199. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  4200. }
  4201. btrfs_update_inode_block_group(trans, inode);
  4202. btrfs_update_inode_block_group(trans, dir);
  4203. out_unlock:
  4204. nr = trans->blocks_used;
  4205. btrfs_end_transaction_throttle(trans, root);
  4206. if (drop_inode) {
  4207. inode_dec_link_count(inode);
  4208. iput(inode);
  4209. }
  4210. btrfs_btree_balance_dirty(root, nr);
  4211. return err;
  4212. }
  4213. static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
  4214. struct dentry *dentry)
  4215. {
  4216. struct btrfs_trans_handle *trans;
  4217. struct btrfs_root *root = BTRFS_I(dir)->root;
  4218. struct inode *inode = old_dentry->d_inode;
  4219. u64 index;
  4220. unsigned long nr = 0;
  4221. int err;
  4222. int drop_inode = 0;
  4223. if (inode->i_nlink == 0)
  4224. return -ENOENT;
  4225. /* do not allow sys_link's with other subvols of the same device */
  4226. if (root->objectid != BTRFS_I(inode)->root->objectid)
  4227. return -EPERM;
  4228. btrfs_inc_nlink(inode);
  4229. inode->i_ctime = CURRENT_TIME;
  4230. err = btrfs_set_inode_index(dir, &index);
  4231. if (err)
  4232. goto fail;
  4233. /*
  4234. * 2 items for inode and inode ref
  4235. * 2 items for dir items
  4236. * 1 item for parent inode
  4237. */
  4238. trans = btrfs_start_transaction(root, 5);
  4239. if (IS_ERR(trans)) {
  4240. err = PTR_ERR(trans);
  4241. goto fail;
  4242. }
  4243. btrfs_set_trans_block_group(trans, dir);
  4244. ihold(inode);
  4245. err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
  4246. if (err) {
  4247. drop_inode = 1;
  4248. } else {
  4249. struct dentry *parent = dget_parent(dentry);
  4250. btrfs_update_inode_block_group(trans, dir);
  4251. err = btrfs_update_inode(trans, root, inode);
  4252. BUG_ON(err);
  4253. btrfs_log_new_name(trans, inode, NULL, parent);
  4254. dput(parent);
  4255. }
  4256. nr = trans->blocks_used;
  4257. btrfs_end_transaction_throttle(trans, root);
  4258. fail:
  4259. if (drop_inode) {
  4260. inode_dec_link_count(inode);
  4261. iput(inode);
  4262. }
  4263. btrfs_btree_balance_dirty(root, nr);
  4264. return err;
  4265. }
  4266. static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  4267. {
  4268. struct inode *inode = NULL;
  4269. struct btrfs_trans_handle *trans;
  4270. struct btrfs_root *root = BTRFS_I(dir)->root;
  4271. int err = 0;
  4272. int drop_on_err = 0;
  4273. u64 objectid = 0;
  4274. u64 index = 0;
  4275. unsigned long nr = 1;
  4276. err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
  4277. if (err)
  4278. return err;
  4279. /*
  4280. * 2 items for inode and ref
  4281. * 2 items for dir items
  4282. * 1 for xattr if selinux is on
  4283. */
  4284. trans = btrfs_start_transaction(root, 5);
  4285. if (IS_ERR(trans))
  4286. return PTR_ERR(trans);
  4287. btrfs_set_trans_block_group(trans, dir);
  4288. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  4289. dentry->d_name.len, dir->i_ino, objectid,
  4290. BTRFS_I(dir)->block_group, S_IFDIR | mode,
  4291. &index);
  4292. if (IS_ERR(inode)) {
  4293. err = PTR_ERR(inode);
  4294. goto out_fail;
  4295. }
  4296. drop_on_err = 1;
  4297. err = btrfs_init_inode_security(trans, inode, dir);
  4298. if (err)
  4299. goto out_fail;
  4300. inode->i_op = &btrfs_dir_inode_operations;
  4301. inode->i_fop = &btrfs_dir_file_operations;
  4302. btrfs_set_trans_block_group(trans, inode);
  4303. btrfs_i_size_write(inode, 0);
  4304. err = btrfs_update_inode(trans, root, inode);
  4305. if (err)
  4306. goto out_fail;
  4307. err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
  4308. dentry->d_name.len, 0, index);
  4309. if (err)
  4310. goto out_fail;
  4311. d_instantiate(dentry, inode);
  4312. drop_on_err = 0;
  4313. btrfs_update_inode_block_group(trans, inode);
  4314. btrfs_update_inode_block_group(trans, dir);
  4315. out_fail:
  4316. nr = trans->blocks_used;
  4317. btrfs_end_transaction_throttle(trans, root);
  4318. if (drop_on_err)
  4319. iput(inode);
  4320. btrfs_btree_balance_dirty(root, nr);
  4321. return err;
  4322. }
  4323. /* helper for btfs_get_extent. Given an existing extent in the tree,
  4324. * and an extent that you want to insert, deal with overlap and insert
  4325. * the new extent into the tree.
  4326. */
  4327. static int merge_extent_mapping(struct extent_map_tree *em_tree,
  4328. struct extent_map *existing,
  4329. struct extent_map *em,
  4330. u64 map_start, u64 map_len)
  4331. {
  4332. u64 start_diff;
  4333. BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
  4334. start_diff = map_start - em->start;
  4335. em->start = map_start;
  4336. em->len = map_len;
  4337. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  4338. !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  4339. em->block_start += start_diff;
  4340. em->block_len -= start_diff;
  4341. }
  4342. return add_extent_mapping(em_tree, em);
  4343. }
  4344. static noinline int uncompress_inline(struct btrfs_path *path,
  4345. struct inode *inode, struct page *page,
  4346. size_t pg_offset, u64 extent_offset,
  4347. struct btrfs_file_extent_item *item)
  4348. {
  4349. int ret;
  4350. struct extent_buffer *leaf = path->nodes[0];
  4351. char *tmp;
  4352. size_t max_size;
  4353. unsigned long inline_size;
  4354. unsigned long ptr;
  4355. int compress_type;
  4356. WARN_ON(pg_offset != 0);
  4357. compress_type = btrfs_file_extent_compression(leaf, item);
  4358. max_size = btrfs_file_extent_ram_bytes(leaf, item);
  4359. inline_size = btrfs_file_extent_inline_item_len(leaf,
  4360. btrfs_item_nr(leaf, path->slots[0]));
  4361. tmp = kmalloc(inline_size, GFP_NOFS);
  4362. ptr = btrfs_file_extent_inline_start(item);
  4363. read_extent_buffer(leaf, tmp, ptr, inline_size);
  4364. max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
  4365. ret = btrfs_decompress(compress_type, tmp, page,
  4366. extent_offset, inline_size, max_size);
  4367. if (ret) {
  4368. char *kaddr = kmap_atomic(page, KM_USER0);
  4369. unsigned long copy_size = min_t(u64,
  4370. PAGE_CACHE_SIZE - pg_offset,
  4371. max_size - extent_offset);
  4372. memset(kaddr + pg_offset, 0, copy_size);
  4373. kunmap_atomic(kaddr, KM_USER0);
  4374. }
  4375. kfree(tmp);
  4376. return 0;
  4377. }
  4378. /*
  4379. * a bit scary, this does extent mapping from logical file offset to the disk.
  4380. * the ugly parts come from merging extents from the disk with the in-ram
  4381. * representation. This gets more complex because of the data=ordered code,
  4382. * where the in-ram extents might be locked pending data=ordered completion.
  4383. *
  4384. * This also copies inline extents directly into the page.
  4385. */
  4386. struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
  4387. size_t pg_offset, u64 start, u64 len,
  4388. int create)
  4389. {
  4390. int ret;
  4391. int err = 0;
  4392. u64 bytenr;
  4393. u64 extent_start = 0;
  4394. u64 extent_end = 0;
  4395. u64 objectid = inode->i_ino;
  4396. u32 found_type;
  4397. struct btrfs_path *path = NULL;
  4398. struct btrfs_root *root = BTRFS_I(inode)->root;
  4399. struct btrfs_file_extent_item *item;
  4400. struct extent_buffer *leaf;
  4401. struct btrfs_key found_key;
  4402. struct extent_map *em = NULL;
  4403. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  4404. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  4405. struct btrfs_trans_handle *trans = NULL;
  4406. int compress_type;
  4407. again:
  4408. read_lock(&em_tree->lock);
  4409. em = lookup_extent_mapping(em_tree, start, len);
  4410. if (em)
  4411. em->bdev = root->fs_info->fs_devices->latest_bdev;
  4412. read_unlock(&em_tree->lock);
  4413. if (em) {
  4414. if (em->start > start || em->start + em->len <= start)
  4415. free_extent_map(em);
  4416. else if (em->block_start == EXTENT_MAP_INLINE && page)
  4417. free_extent_map(em);
  4418. else
  4419. goto out;
  4420. }
  4421. em = alloc_extent_map(GFP_NOFS);
  4422. if (!em) {
  4423. err = -ENOMEM;
  4424. goto out;
  4425. }
  4426. em->bdev = root->fs_info->fs_devices->latest_bdev;
  4427. em->start = EXTENT_MAP_HOLE;
  4428. em->orig_start = EXTENT_MAP_HOLE;
  4429. em->len = (u64)-1;
  4430. em->block_len = (u64)-1;
  4431. if (!path) {
  4432. path = btrfs_alloc_path();
  4433. BUG_ON(!path);
  4434. }
  4435. ret = btrfs_lookup_file_extent(trans, root, path,
  4436. objectid, start, trans != NULL);
  4437. if (ret < 0) {
  4438. err = ret;
  4439. goto out;
  4440. }
  4441. if (ret != 0) {
  4442. if (path->slots[0] == 0)
  4443. goto not_found;
  4444. path->slots[0]--;
  4445. }
  4446. leaf = path->nodes[0];
  4447. item = btrfs_item_ptr(leaf, path->slots[0],
  4448. struct btrfs_file_extent_item);
  4449. /* are we inside the extent that was found? */
  4450. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  4451. found_type = btrfs_key_type(&found_key);
  4452. if (found_key.objectid != objectid ||
  4453. found_type != BTRFS_EXTENT_DATA_KEY) {
  4454. goto not_found;
  4455. }
  4456. found_type = btrfs_file_extent_type(leaf, item);
  4457. extent_start = found_key.offset;
  4458. compress_type = btrfs_file_extent_compression(leaf, item);
  4459. if (found_type == BTRFS_FILE_EXTENT_REG ||
  4460. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  4461. extent_end = extent_start +
  4462. btrfs_file_extent_num_bytes(leaf, item);
  4463. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  4464. size_t size;
  4465. size = btrfs_file_extent_inline_len(leaf, item);
  4466. extent_end = (extent_start + size + root->sectorsize - 1) &
  4467. ~((u64)root->sectorsize - 1);
  4468. }
  4469. if (start >= extent_end) {
  4470. path->slots[0]++;
  4471. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  4472. ret = btrfs_next_leaf(root, path);
  4473. if (ret < 0) {
  4474. err = ret;
  4475. goto out;
  4476. }
  4477. if (ret > 0)
  4478. goto not_found;
  4479. leaf = path->nodes[0];
  4480. }
  4481. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  4482. if (found_key.objectid != objectid ||
  4483. found_key.type != BTRFS_EXTENT_DATA_KEY)
  4484. goto not_found;
  4485. if (start + len <= found_key.offset)
  4486. goto not_found;
  4487. em->start = start;
  4488. em->len = found_key.offset - start;
  4489. goto not_found_em;
  4490. }
  4491. if (found_type == BTRFS_FILE_EXTENT_REG ||
  4492. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  4493. em->start = extent_start;
  4494. em->len = extent_end - extent_start;
  4495. em->orig_start = extent_start -
  4496. btrfs_file_extent_offset(leaf, item);
  4497. bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
  4498. if (bytenr == 0) {
  4499. em->block_start = EXTENT_MAP_HOLE;
  4500. goto insert;
  4501. }
  4502. if (compress_type != BTRFS_COMPRESS_NONE) {
  4503. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  4504. em->compress_type = compress_type;
  4505. em->block_start = bytenr;
  4506. em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
  4507. item);
  4508. } else {
  4509. bytenr += btrfs_file_extent_offset(leaf, item);
  4510. em->block_start = bytenr;
  4511. em->block_len = em->len;
  4512. if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
  4513. set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
  4514. }
  4515. goto insert;
  4516. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  4517. unsigned long ptr;
  4518. char *map;
  4519. size_t size;
  4520. size_t extent_offset;
  4521. size_t copy_size;
  4522. em->block_start = EXTENT_MAP_INLINE;
  4523. if (!page || create) {
  4524. em->start = extent_start;
  4525. em->len = extent_end - extent_start;
  4526. goto out;
  4527. }
  4528. size = btrfs_file_extent_inline_len(leaf, item);
  4529. extent_offset = page_offset(page) + pg_offset - extent_start;
  4530. copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
  4531. size - extent_offset);
  4532. em->start = extent_start + extent_offset;
  4533. em->len = (copy_size + root->sectorsize - 1) &
  4534. ~((u64)root->sectorsize - 1);
  4535. em->orig_start = EXTENT_MAP_INLINE;
  4536. if (compress_type) {
  4537. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  4538. em->compress_type = compress_type;
  4539. }
  4540. ptr = btrfs_file_extent_inline_start(item) + extent_offset;
  4541. if (create == 0 && !PageUptodate(page)) {
  4542. if (btrfs_file_extent_compression(leaf, item) !=
  4543. BTRFS_COMPRESS_NONE) {
  4544. ret = uncompress_inline(path, inode, page,
  4545. pg_offset,
  4546. extent_offset, item);
  4547. BUG_ON(ret);
  4548. } else {
  4549. map = kmap(page);
  4550. read_extent_buffer(leaf, map + pg_offset, ptr,
  4551. copy_size);
  4552. if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
  4553. memset(map + pg_offset + copy_size, 0,
  4554. PAGE_CACHE_SIZE - pg_offset -
  4555. copy_size);
  4556. }
  4557. kunmap(page);
  4558. }
  4559. flush_dcache_page(page);
  4560. } else if (create && PageUptodate(page)) {
  4561. WARN_ON(1);
  4562. if (!trans) {
  4563. kunmap(page);
  4564. free_extent_map(em);
  4565. em = NULL;
  4566. btrfs_release_path(root, path);
  4567. trans = btrfs_join_transaction(root, 1);
  4568. if (IS_ERR(trans))
  4569. return ERR_CAST(trans);
  4570. goto again;
  4571. }
  4572. map = kmap(page);
  4573. write_extent_buffer(leaf, map + pg_offset, ptr,
  4574. copy_size);
  4575. kunmap(page);
  4576. btrfs_mark_buffer_dirty(leaf);
  4577. }
  4578. set_extent_uptodate(io_tree, em->start,
  4579. extent_map_end(em) - 1, GFP_NOFS);
  4580. goto insert;
  4581. } else {
  4582. printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
  4583. WARN_ON(1);
  4584. }
  4585. not_found:
  4586. em->start = start;
  4587. em->len = len;
  4588. not_found_em:
  4589. em->block_start = EXTENT_MAP_HOLE;
  4590. set_bit(EXTENT_FLAG_VACANCY, &em->flags);
  4591. insert:
  4592. btrfs_release_path(root, path);
  4593. if (em->start > start || extent_map_end(em) <= start) {
  4594. printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
  4595. "[%llu %llu]\n", (unsigned long long)em->start,
  4596. (unsigned long long)em->len,
  4597. (unsigned long long)start,
  4598. (unsigned long long)len);
  4599. err = -EIO;
  4600. goto out;
  4601. }
  4602. err = 0;
  4603. write_lock(&em_tree->lock);
  4604. ret = add_extent_mapping(em_tree, em);
  4605. /* it is possible that someone inserted the extent into the tree
  4606. * while we had the lock dropped. It is also possible that
  4607. * an overlapping map exists in the tree
  4608. */
  4609. if (ret == -EEXIST) {
  4610. struct extent_map *existing;
  4611. ret = 0;
  4612. existing = lookup_extent_mapping(em_tree, start, len);
  4613. if (existing && (existing->start > start ||
  4614. existing->start + existing->len <= start)) {
  4615. free_extent_map(existing);
  4616. existing = NULL;
  4617. }
  4618. if (!existing) {
  4619. existing = lookup_extent_mapping(em_tree, em->start,
  4620. em->len);
  4621. if (existing) {
  4622. err = merge_extent_mapping(em_tree, existing,
  4623. em, start,
  4624. root->sectorsize);
  4625. free_extent_map(existing);
  4626. if (err) {
  4627. free_extent_map(em);
  4628. em = NULL;
  4629. }
  4630. } else {
  4631. err = -EIO;
  4632. free_extent_map(em);
  4633. em = NULL;
  4634. }
  4635. } else {
  4636. free_extent_map(em);
  4637. em = existing;
  4638. err = 0;
  4639. }
  4640. }
  4641. write_unlock(&em_tree->lock);
  4642. out:
  4643. trace_btrfs_get_extent(root, em);
  4644. if (path)
  4645. btrfs_free_path(path);
  4646. if (trans) {
  4647. ret = btrfs_end_transaction(trans, root);
  4648. if (!err)
  4649. err = ret;
  4650. }
  4651. if (err) {
  4652. free_extent_map(em);
  4653. return ERR_PTR(err);
  4654. }
  4655. return em;
  4656. }
  4657. struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
  4658. size_t pg_offset, u64 start, u64 len,
  4659. int create)
  4660. {
  4661. struct extent_map *em;
  4662. struct extent_map *hole_em = NULL;
  4663. u64 range_start = start;
  4664. u64 end;
  4665. u64 found;
  4666. u64 found_end;
  4667. int err = 0;
  4668. em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
  4669. if (IS_ERR(em))
  4670. return em;
  4671. if (em) {
  4672. /*
  4673. * if our em maps to a hole, there might
  4674. * actually be delalloc bytes behind it
  4675. */
  4676. if (em->block_start != EXTENT_MAP_HOLE)
  4677. return em;
  4678. else
  4679. hole_em = em;
  4680. }
  4681. /* check to see if we've wrapped (len == -1 or similar) */
  4682. end = start + len;
  4683. if (end < start)
  4684. end = (u64)-1;
  4685. else
  4686. end -= 1;
  4687. em = NULL;
  4688. /* ok, we didn't find anything, lets look for delalloc */
  4689. found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
  4690. end, len, EXTENT_DELALLOC, 1);
  4691. found_end = range_start + found;
  4692. if (found_end < range_start)
  4693. found_end = (u64)-1;
  4694. /*
  4695. * we didn't find anything useful, return
  4696. * the original results from get_extent()
  4697. */
  4698. if (range_start > end || found_end <= start) {
  4699. em = hole_em;
  4700. hole_em = NULL;
  4701. goto out;
  4702. }
  4703. /* adjust the range_start to make sure it doesn't
  4704. * go backwards from the start they passed in
  4705. */
  4706. range_start = max(start,range_start);
  4707. found = found_end - range_start;
  4708. if (found > 0) {
  4709. u64 hole_start = start;
  4710. u64 hole_len = len;
  4711. em = alloc_extent_map(GFP_NOFS);
  4712. if (!em) {
  4713. err = -ENOMEM;
  4714. goto out;
  4715. }
  4716. /*
  4717. * when btrfs_get_extent can't find anything it
  4718. * returns one huge hole
  4719. *
  4720. * make sure what it found really fits our range, and
  4721. * adjust to make sure it is based on the start from
  4722. * the caller
  4723. */
  4724. if (hole_em) {
  4725. u64 calc_end = extent_map_end(hole_em);
  4726. if (calc_end <= start || (hole_em->start > end)) {
  4727. free_extent_map(hole_em);
  4728. hole_em = NULL;
  4729. } else {
  4730. hole_start = max(hole_em->start, start);
  4731. hole_len = calc_end - hole_start;
  4732. }
  4733. }
  4734. em->bdev = NULL;
  4735. if (hole_em && range_start > hole_start) {
  4736. /* our hole starts before our delalloc, so we
  4737. * have to return just the parts of the hole
  4738. * that go until the delalloc starts
  4739. */
  4740. em->len = min(hole_len,
  4741. range_start - hole_start);
  4742. em->start = hole_start;
  4743. em->orig_start = hole_start;
  4744. /*
  4745. * don't adjust block start at all,
  4746. * it is fixed at EXTENT_MAP_HOLE
  4747. */
  4748. em->block_start = hole_em->block_start;
  4749. em->block_len = hole_len;
  4750. } else {
  4751. em->start = range_start;
  4752. em->len = found;
  4753. em->orig_start = range_start;
  4754. em->block_start = EXTENT_MAP_DELALLOC;
  4755. em->block_len = found;
  4756. }
  4757. } else if (hole_em) {
  4758. return hole_em;
  4759. }
  4760. out:
  4761. free_extent_map(hole_em);
  4762. if (err) {
  4763. free_extent_map(em);
  4764. return ERR_PTR(err);
  4765. }
  4766. return em;
  4767. }
  4768. static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
  4769. u64 start, u64 len)
  4770. {
  4771. struct btrfs_root *root = BTRFS_I(inode)->root;
  4772. struct btrfs_trans_handle *trans;
  4773. struct extent_map *em;
  4774. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  4775. struct btrfs_key ins;
  4776. u64 alloc_hint;
  4777. int ret;
  4778. btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
  4779. trans = btrfs_join_transaction(root, 0);
  4780. if (IS_ERR(trans))
  4781. return ERR_CAST(trans);
  4782. trans->block_rsv = &root->fs_info->delalloc_block_rsv;
  4783. alloc_hint = get_extent_allocation_hint(inode, start, len);
  4784. ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
  4785. alloc_hint, (u64)-1, &ins, 1);
  4786. if (ret) {
  4787. em = ERR_PTR(ret);
  4788. goto out;
  4789. }
  4790. em = alloc_extent_map(GFP_NOFS);
  4791. if (!em) {
  4792. em = ERR_PTR(-ENOMEM);
  4793. goto out;
  4794. }
  4795. em->start = start;
  4796. em->orig_start = em->start;
  4797. em->len = ins.offset;
  4798. em->block_start = ins.objectid;
  4799. em->block_len = ins.offset;
  4800. em->bdev = root->fs_info->fs_devices->latest_bdev;
  4801. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  4802. while (1) {
  4803. write_lock(&em_tree->lock);
  4804. ret = add_extent_mapping(em_tree, em);
  4805. write_unlock(&em_tree->lock);
  4806. if (ret != -EEXIST)
  4807. break;
  4808. btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0);
  4809. }
  4810. ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
  4811. ins.offset, ins.offset, 0);
  4812. if (ret) {
  4813. btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
  4814. em = ERR_PTR(ret);
  4815. }
  4816. out:
  4817. btrfs_end_transaction(trans, root);
  4818. return em;
  4819. }
  4820. /*
  4821. * returns 1 when the nocow is safe, < 1 on error, 0 if the
  4822. * block must be cow'd
  4823. */
  4824. static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
  4825. struct inode *inode, u64 offset, u64 len)
  4826. {
  4827. struct btrfs_path *path;
  4828. int ret;
  4829. struct extent_buffer *leaf;
  4830. struct btrfs_root *root = BTRFS_I(inode)->root;
  4831. struct btrfs_file_extent_item *fi;
  4832. struct btrfs_key key;
  4833. u64 disk_bytenr;
  4834. u64 backref_offset;
  4835. u64 extent_end;
  4836. u64 num_bytes;
  4837. int slot;
  4838. int found_type;
  4839. path = btrfs_alloc_path();
  4840. if (!path)
  4841. return -ENOMEM;
  4842. ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
  4843. offset, 0);
  4844. if (ret < 0)
  4845. goto out;
  4846. slot = path->slots[0];
  4847. if (ret == 1) {
  4848. if (slot == 0) {
  4849. /* can't find the item, must cow */
  4850. ret = 0;
  4851. goto out;
  4852. }
  4853. slot--;
  4854. }
  4855. ret = 0;
  4856. leaf = path->nodes[0];
  4857. btrfs_item_key_to_cpu(leaf, &key, slot);
  4858. if (key.objectid != inode->i_ino ||
  4859. key.type != BTRFS_EXTENT_DATA_KEY) {
  4860. /* not our file or wrong item type, must cow */
  4861. goto out;
  4862. }
  4863. if (key.offset > offset) {
  4864. /* Wrong offset, must cow */
  4865. goto out;
  4866. }
  4867. fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  4868. found_type = btrfs_file_extent_type(leaf, fi);
  4869. if (found_type != BTRFS_FILE_EXTENT_REG &&
  4870. found_type != BTRFS_FILE_EXTENT_PREALLOC) {
  4871. /* not a regular extent, must cow */
  4872. goto out;
  4873. }
  4874. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  4875. backref_offset = btrfs_file_extent_offset(leaf, fi);
  4876. extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
  4877. if (extent_end < offset + len) {
  4878. /* extent doesn't include our full range, must cow */
  4879. goto out;
  4880. }
  4881. if (btrfs_extent_readonly(root, disk_bytenr))
  4882. goto out;
  4883. /*
  4884. * look for other files referencing this extent, if we
  4885. * find any we must cow
  4886. */
  4887. if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
  4888. key.offset - backref_offset, disk_bytenr))
  4889. goto out;
  4890. /*
  4891. * adjust disk_bytenr and num_bytes to cover just the bytes
  4892. * in this extent we are about to write. If there
  4893. * are any csums in that range we have to cow in order
  4894. * to keep the csums correct
  4895. */
  4896. disk_bytenr += backref_offset;
  4897. disk_bytenr += offset - key.offset;
  4898. num_bytes = min(offset + len, extent_end) - offset;
  4899. if (csum_exist_in_range(root, disk_bytenr, num_bytes))
  4900. goto out;
  4901. /*
  4902. * all of the above have passed, it is safe to overwrite this extent
  4903. * without cow
  4904. */
  4905. ret = 1;
  4906. out:
  4907. btrfs_free_path(path);
  4908. return ret;
  4909. }
  4910. static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
  4911. struct buffer_head *bh_result, int create)
  4912. {
  4913. struct extent_map *em;
  4914. struct btrfs_root *root = BTRFS_I(inode)->root;
  4915. u64 start = iblock << inode->i_blkbits;
  4916. u64 len = bh_result->b_size;
  4917. struct btrfs_trans_handle *trans;
  4918. em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
  4919. if (IS_ERR(em))
  4920. return PTR_ERR(em);
  4921. /*
  4922. * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
  4923. * io. INLINE is special, and we could probably kludge it in here, but
  4924. * it's still buffered so for safety lets just fall back to the generic
  4925. * buffered path.
  4926. *
  4927. * For COMPRESSED we _have_ to read the entire extent in so we can
  4928. * decompress it, so there will be buffering required no matter what we
  4929. * do, so go ahead and fallback to buffered.
  4930. *
  4931. * We return -ENOTBLK because thats what makes DIO go ahead and go back
  4932. * to buffered IO. Don't blame me, this is the price we pay for using
  4933. * the generic code.
  4934. */
  4935. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
  4936. em->block_start == EXTENT_MAP_INLINE) {
  4937. free_extent_map(em);
  4938. return -ENOTBLK;
  4939. }
  4940. /* Just a good old fashioned hole, return */
  4941. if (!create && (em->block_start == EXTENT_MAP_HOLE ||
  4942. test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
  4943. free_extent_map(em);
  4944. /* DIO will do one hole at a time, so just unlock a sector */
  4945. unlock_extent(&BTRFS_I(inode)->io_tree, start,
  4946. start + root->sectorsize - 1, GFP_NOFS);
  4947. return 0;
  4948. }
  4949. /*
  4950. * We don't allocate a new extent in the following cases
  4951. *
  4952. * 1) The inode is marked as NODATACOW. In this case we'll just use the
  4953. * existing extent.
  4954. * 2) The extent is marked as PREALLOC. We're good to go here and can
  4955. * just use the extent.
  4956. *
  4957. */
  4958. if (!create) {
  4959. len = em->len - (start - em->start);
  4960. goto map;
  4961. }
  4962. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
  4963. ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
  4964. em->block_start != EXTENT_MAP_HOLE)) {
  4965. int type;
  4966. int ret;
  4967. u64 block_start;
  4968. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  4969. type = BTRFS_ORDERED_PREALLOC;
  4970. else
  4971. type = BTRFS_ORDERED_NOCOW;
  4972. len = min(len, em->len - (start - em->start));
  4973. block_start = em->block_start + (start - em->start);
  4974. /*
  4975. * we're not going to log anything, but we do need
  4976. * to make sure the current transaction stays open
  4977. * while we look for nocow cross refs
  4978. */
  4979. trans = btrfs_join_transaction(root, 0);
  4980. if (IS_ERR(trans))
  4981. goto must_cow;
  4982. if (can_nocow_odirect(trans, inode, start, len) == 1) {
  4983. ret = btrfs_add_ordered_extent_dio(inode, start,
  4984. block_start, len, len, type);
  4985. btrfs_end_transaction(trans, root);
  4986. if (ret) {
  4987. free_extent_map(em);
  4988. return ret;
  4989. }
  4990. goto unlock;
  4991. }
  4992. btrfs_end_transaction(trans, root);
  4993. }
  4994. must_cow:
  4995. /*
  4996. * this will cow the extent, reset the len in case we changed
  4997. * it above
  4998. */
  4999. len = bh_result->b_size;
  5000. free_extent_map(em);
  5001. em = btrfs_new_extent_direct(inode, start, len);
  5002. if (IS_ERR(em))
  5003. return PTR_ERR(em);
  5004. len = min(len, em->len - (start - em->start));
  5005. unlock:
  5006. clear_extent_bit(&BTRFS_I(inode)->io_tree, start, start + len - 1,
  5007. EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DIRTY, 1,
  5008. 0, NULL, GFP_NOFS);
  5009. map:
  5010. bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
  5011. inode->i_blkbits;
  5012. bh_result->b_size = len;
  5013. bh_result->b_bdev = em->bdev;
  5014. set_buffer_mapped(bh_result);
  5015. if (create && !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  5016. set_buffer_new(bh_result);
  5017. free_extent_map(em);
  5018. return 0;
  5019. }
  5020. struct btrfs_dio_private {
  5021. struct inode *inode;
  5022. u64 logical_offset;
  5023. u64 disk_bytenr;
  5024. u64 bytes;
  5025. u32 *csums;
  5026. void *private;
  5027. /* number of bios pending for this dio */
  5028. atomic_t pending_bios;
  5029. /* IO errors */
  5030. int errors;
  5031. struct bio *orig_bio;
  5032. };
  5033. static void btrfs_endio_direct_read(struct bio *bio, int err)
  5034. {
  5035. struct btrfs_dio_private *dip = bio->bi_private;
  5036. struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
  5037. struct bio_vec *bvec = bio->bi_io_vec;
  5038. struct inode *inode = dip->inode;
  5039. struct btrfs_root *root = BTRFS_I(inode)->root;
  5040. u64 start;
  5041. u32 *private = dip->csums;
  5042. start = dip->logical_offset;
  5043. do {
  5044. if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
  5045. struct page *page = bvec->bv_page;
  5046. char *kaddr;
  5047. u32 csum = ~(u32)0;
  5048. unsigned long flags;
  5049. local_irq_save(flags);
  5050. kaddr = kmap_atomic(page, KM_IRQ0);
  5051. csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
  5052. csum, bvec->bv_len);
  5053. btrfs_csum_final(csum, (char *)&csum);
  5054. kunmap_atomic(kaddr, KM_IRQ0);
  5055. local_irq_restore(flags);
  5056. flush_dcache_page(bvec->bv_page);
  5057. if (csum != *private) {
  5058. printk(KERN_ERR "btrfs csum failed ino %lu off"
  5059. " %llu csum %u private %u\n",
  5060. inode->i_ino, (unsigned long long)start,
  5061. csum, *private);
  5062. err = -EIO;
  5063. }
  5064. }
  5065. start += bvec->bv_len;
  5066. private++;
  5067. bvec++;
  5068. } while (bvec <= bvec_end);
  5069. unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
  5070. dip->logical_offset + dip->bytes - 1, GFP_NOFS);
  5071. bio->bi_private = dip->private;
  5072. kfree(dip->csums);
  5073. kfree(dip);
  5074. /* If we had a csum failure make sure to clear the uptodate flag */
  5075. if (err)
  5076. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  5077. dio_end_io(bio, err);
  5078. }
  5079. static void btrfs_endio_direct_write(struct bio *bio, int err)
  5080. {
  5081. struct btrfs_dio_private *dip = bio->bi_private;
  5082. struct inode *inode = dip->inode;
  5083. struct btrfs_root *root = BTRFS_I(inode)->root;
  5084. struct btrfs_trans_handle *trans;
  5085. struct btrfs_ordered_extent *ordered = NULL;
  5086. struct extent_state *cached_state = NULL;
  5087. u64 ordered_offset = dip->logical_offset;
  5088. u64 ordered_bytes = dip->bytes;
  5089. int ret;
  5090. if (err)
  5091. goto out_done;
  5092. again:
  5093. ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
  5094. &ordered_offset,
  5095. ordered_bytes);
  5096. if (!ret)
  5097. goto out_test;
  5098. BUG_ON(!ordered);
  5099. trans = btrfs_join_transaction(root, 1);
  5100. if (IS_ERR(trans)) {
  5101. err = -ENOMEM;
  5102. goto out;
  5103. }
  5104. trans->block_rsv = &root->fs_info->delalloc_block_rsv;
  5105. if (test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) {
  5106. ret = btrfs_ordered_update_i_size(inode, 0, ordered);
  5107. if (!ret)
  5108. ret = btrfs_update_inode(trans, root, inode);
  5109. err = ret;
  5110. goto out;
  5111. }
  5112. lock_extent_bits(&BTRFS_I(inode)->io_tree, ordered->file_offset,
  5113. ordered->file_offset + ordered->len - 1, 0,
  5114. &cached_state, GFP_NOFS);
  5115. if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) {
  5116. ret = btrfs_mark_extent_written(trans, inode,
  5117. ordered->file_offset,
  5118. ordered->file_offset +
  5119. ordered->len);
  5120. if (ret) {
  5121. err = ret;
  5122. goto out_unlock;
  5123. }
  5124. } else {
  5125. ret = insert_reserved_file_extent(trans, inode,
  5126. ordered->file_offset,
  5127. ordered->start,
  5128. ordered->disk_len,
  5129. ordered->len,
  5130. ordered->len,
  5131. 0, 0, 0,
  5132. BTRFS_FILE_EXTENT_REG);
  5133. unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
  5134. ordered->file_offset, ordered->len);
  5135. if (ret) {
  5136. err = ret;
  5137. WARN_ON(1);
  5138. goto out_unlock;
  5139. }
  5140. }
  5141. add_pending_csums(trans, inode, ordered->file_offset, &ordered->list);
  5142. btrfs_ordered_update_i_size(inode, 0, ordered);
  5143. btrfs_update_inode(trans, root, inode);
  5144. out_unlock:
  5145. unlock_extent_cached(&BTRFS_I(inode)->io_tree, ordered->file_offset,
  5146. ordered->file_offset + ordered->len - 1,
  5147. &cached_state, GFP_NOFS);
  5148. out:
  5149. btrfs_delalloc_release_metadata(inode, ordered->len);
  5150. btrfs_end_transaction(trans, root);
  5151. ordered_offset = ordered->file_offset + ordered->len;
  5152. btrfs_put_ordered_extent(ordered);
  5153. btrfs_put_ordered_extent(ordered);
  5154. out_test:
  5155. /*
  5156. * our bio might span multiple ordered extents. If we haven't
  5157. * completed the accounting for the whole dio, go back and try again
  5158. */
  5159. if (ordered_offset < dip->logical_offset + dip->bytes) {
  5160. ordered_bytes = dip->logical_offset + dip->bytes -
  5161. ordered_offset;
  5162. goto again;
  5163. }
  5164. out_done:
  5165. bio->bi_private = dip->private;
  5166. kfree(dip->csums);
  5167. kfree(dip);
  5168. /* If we had an error make sure to clear the uptodate flag */
  5169. if (err)
  5170. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  5171. dio_end_io(bio, err);
  5172. }
  5173. static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
  5174. struct bio *bio, int mirror_num,
  5175. unsigned long bio_flags, u64 offset)
  5176. {
  5177. int ret;
  5178. struct btrfs_root *root = BTRFS_I(inode)->root;
  5179. ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
  5180. BUG_ON(ret);
  5181. return 0;
  5182. }
  5183. static void btrfs_end_dio_bio(struct bio *bio, int err)
  5184. {
  5185. struct btrfs_dio_private *dip = bio->bi_private;
  5186. if (err) {
  5187. printk(KERN_ERR "btrfs direct IO failed ino %lu rw %lu "
  5188. "sector %#Lx len %u err no %d\n",
  5189. dip->inode->i_ino, bio->bi_rw,
  5190. (unsigned long long)bio->bi_sector, bio->bi_size, err);
  5191. dip->errors = 1;
  5192. /*
  5193. * before atomic variable goto zero, we must make sure
  5194. * dip->errors is perceived to be set.
  5195. */
  5196. smp_mb__before_atomic_dec();
  5197. }
  5198. /* if there are more bios still pending for this dio, just exit */
  5199. if (!atomic_dec_and_test(&dip->pending_bios))
  5200. goto out;
  5201. if (dip->errors)
  5202. bio_io_error(dip->orig_bio);
  5203. else {
  5204. set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
  5205. bio_endio(dip->orig_bio, 0);
  5206. }
  5207. out:
  5208. bio_put(bio);
  5209. }
  5210. static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
  5211. u64 first_sector, gfp_t gfp_flags)
  5212. {
  5213. int nr_vecs = bio_get_nr_vecs(bdev);
  5214. return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
  5215. }
  5216. static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
  5217. int rw, u64 file_offset, int skip_sum,
  5218. u32 *csums)
  5219. {
  5220. int write = rw & REQ_WRITE;
  5221. struct btrfs_root *root = BTRFS_I(inode)->root;
  5222. int ret;
  5223. bio_get(bio);
  5224. ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
  5225. if (ret)
  5226. goto err;
  5227. if (write && !skip_sum) {
  5228. ret = btrfs_wq_submit_bio(root->fs_info,
  5229. inode, rw, bio, 0, 0,
  5230. file_offset,
  5231. __btrfs_submit_bio_start_direct_io,
  5232. __btrfs_submit_bio_done);
  5233. goto err;
  5234. } else if (!skip_sum)
  5235. btrfs_lookup_bio_sums_dio(root, inode, bio,
  5236. file_offset, csums);
  5237. ret = btrfs_map_bio(root, rw, bio, 0, 1);
  5238. err:
  5239. bio_put(bio);
  5240. return ret;
  5241. }
  5242. static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
  5243. int skip_sum)
  5244. {
  5245. struct inode *inode = dip->inode;
  5246. struct btrfs_root *root = BTRFS_I(inode)->root;
  5247. struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
  5248. struct bio *bio;
  5249. struct bio *orig_bio = dip->orig_bio;
  5250. struct bio_vec *bvec = orig_bio->bi_io_vec;
  5251. u64 start_sector = orig_bio->bi_sector;
  5252. u64 file_offset = dip->logical_offset;
  5253. u64 submit_len = 0;
  5254. u64 map_length;
  5255. int nr_pages = 0;
  5256. u32 *csums = dip->csums;
  5257. int ret = 0;
  5258. int write = rw & REQ_WRITE;
  5259. bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
  5260. if (!bio)
  5261. return -ENOMEM;
  5262. bio->bi_private = dip;
  5263. bio->bi_end_io = btrfs_end_dio_bio;
  5264. atomic_inc(&dip->pending_bios);
  5265. map_length = orig_bio->bi_size;
  5266. ret = btrfs_map_block(map_tree, READ, start_sector << 9,
  5267. &map_length, NULL, 0);
  5268. if (ret) {
  5269. bio_put(bio);
  5270. return -EIO;
  5271. }
  5272. while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
  5273. if (unlikely(map_length < submit_len + bvec->bv_len ||
  5274. bio_add_page(bio, bvec->bv_page, bvec->bv_len,
  5275. bvec->bv_offset) < bvec->bv_len)) {
  5276. /*
  5277. * inc the count before we submit the bio so
  5278. * we know the end IO handler won't happen before
  5279. * we inc the count. Otherwise, the dip might get freed
  5280. * before we're done setting it up
  5281. */
  5282. atomic_inc(&dip->pending_bios);
  5283. ret = __btrfs_submit_dio_bio(bio, inode, rw,
  5284. file_offset, skip_sum,
  5285. csums);
  5286. if (ret) {
  5287. bio_put(bio);
  5288. atomic_dec(&dip->pending_bios);
  5289. goto out_err;
  5290. }
  5291. /* Write's use the ordered csums */
  5292. if (!write && !skip_sum)
  5293. csums = csums + nr_pages;
  5294. start_sector += submit_len >> 9;
  5295. file_offset += submit_len;
  5296. submit_len = 0;
  5297. nr_pages = 0;
  5298. bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
  5299. start_sector, GFP_NOFS);
  5300. if (!bio)
  5301. goto out_err;
  5302. bio->bi_private = dip;
  5303. bio->bi_end_io = btrfs_end_dio_bio;
  5304. map_length = orig_bio->bi_size;
  5305. ret = btrfs_map_block(map_tree, READ, start_sector << 9,
  5306. &map_length, NULL, 0);
  5307. if (ret) {
  5308. bio_put(bio);
  5309. goto out_err;
  5310. }
  5311. } else {
  5312. submit_len += bvec->bv_len;
  5313. nr_pages ++;
  5314. bvec++;
  5315. }
  5316. }
  5317. ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
  5318. csums);
  5319. if (!ret)
  5320. return 0;
  5321. bio_put(bio);
  5322. out_err:
  5323. dip->errors = 1;
  5324. /*
  5325. * before atomic variable goto zero, we must
  5326. * make sure dip->errors is perceived to be set.
  5327. */
  5328. smp_mb__before_atomic_dec();
  5329. if (atomic_dec_and_test(&dip->pending_bios))
  5330. bio_io_error(dip->orig_bio);
  5331. /* bio_end_io() will handle error, so we needn't return it */
  5332. return 0;
  5333. }
  5334. static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
  5335. loff_t file_offset)
  5336. {
  5337. struct btrfs_root *root = BTRFS_I(inode)->root;
  5338. struct btrfs_dio_private *dip;
  5339. struct bio_vec *bvec = bio->bi_io_vec;
  5340. int skip_sum;
  5341. int write = rw & REQ_WRITE;
  5342. int ret = 0;
  5343. skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  5344. dip = kmalloc(sizeof(*dip), GFP_NOFS);
  5345. if (!dip) {
  5346. ret = -ENOMEM;
  5347. goto free_ordered;
  5348. }
  5349. dip->csums = NULL;
  5350. /* Write's use the ordered csum stuff, so we don't need dip->csums */
  5351. if (!write && !skip_sum) {
  5352. dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS);
  5353. if (!dip->csums) {
  5354. kfree(dip);
  5355. ret = -ENOMEM;
  5356. goto free_ordered;
  5357. }
  5358. }
  5359. dip->private = bio->bi_private;
  5360. dip->inode = inode;
  5361. dip->logical_offset = file_offset;
  5362. dip->bytes = 0;
  5363. do {
  5364. dip->bytes += bvec->bv_len;
  5365. bvec++;
  5366. } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
  5367. dip->disk_bytenr = (u64)bio->bi_sector << 9;
  5368. bio->bi_private = dip;
  5369. dip->errors = 0;
  5370. dip->orig_bio = bio;
  5371. atomic_set(&dip->pending_bios, 0);
  5372. if (write)
  5373. bio->bi_end_io = btrfs_endio_direct_write;
  5374. else
  5375. bio->bi_end_io = btrfs_endio_direct_read;
  5376. ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
  5377. if (!ret)
  5378. return;
  5379. free_ordered:
  5380. /*
  5381. * If this is a write, we need to clean up the reserved space and kill
  5382. * the ordered extent.
  5383. */
  5384. if (write) {
  5385. struct btrfs_ordered_extent *ordered;
  5386. ordered = btrfs_lookup_ordered_extent(inode, file_offset);
  5387. if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
  5388. !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
  5389. btrfs_free_reserved_extent(root, ordered->start,
  5390. ordered->disk_len);
  5391. btrfs_put_ordered_extent(ordered);
  5392. btrfs_put_ordered_extent(ordered);
  5393. }
  5394. bio_endio(bio, ret);
  5395. }
  5396. static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
  5397. const struct iovec *iov, loff_t offset,
  5398. unsigned long nr_segs)
  5399. {
  5400. int seg;
  5401. size_t size;
  5402. unsigned long addr;
  5403. unsigned blocksize_mask = root->sectorsize - 1;
  5404. ssize_t retval = -EINVAL;
  5405. loff_t end = offset;
  5406. if (offset & blocksize_mask)
  5407. goto out;
  5408. /* Check the memory alignment. Blocks cannot straddle pages */
  5409. for (seg = 0; seg < nr_segs; seg++) {
  5410. addr = (unsigned long)iov[seg].iov_base;
  5411. size = iov[seg].iov_len;
  5412. end += size;
  5413. if ((addr & blocksize_mask) || (size & blocksize_mask))
  5414. goto out;
  5415. }
  5416. retval = 0;
  5417. out:
  5418. return retval;
  5419. }
  5420. static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
  5421. const struct iovec *iov, loff_t offset,
  5422. unsigned long nr_segs)
  5423. {
  5424. struct file *file = iocb->ki_filp;
  5425. struct inode *inode = file->f_mapping->host;
  5426. struct btrfs_ordered_extent *ordered;
  5427. struct extent_state *cached_state = NULL;
  5428. u64 lockstart, lockend;
  5429. ssize_t ret;
  5430. int writing = rw & WRITE;
  5431. int write_bits = 0;
  5432. size_t count = iov_length(iov, nr_segs);
  5433. if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
  5434. offset, nr_segs)) {
  5435. return 0;
  5436. }
  5437. lockstart = offset;
  5438. lockend = offset + count - 1;
  5439. if (writing) {
  5440. ret = btrfs_delalloc_reserve_space(inode, count);
  5441. if (ret)
  5442. goto out;
  5443. }
  5444. while (1) {
  5445. lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  5446. 0, &cached_state, GFP_NOFS);
  5447. /*
  5448. * We're concerned with the entire range that we're going to be
  5449. * doing DIO to, so we need to make sure theres no ordered
  5450. * extents in this range.
  5451. */
  5452. ordered = btrfs_lookup_ordered_range(inode, lockstart,
  5453. lockend - lockstart + 1);
  5454. if (!ordered)
  5455. break;
  5456. unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  5457. &cached_state, GFP_NOFS);
  5458. btrfs_start_ordered_extent(inode, ordered, 1);
  5459. btrfs_put_ordered_extent(ordered);
  5460. cond_resched();
  5461. }
  5462. /*
  5463. * we don't use btrfs_set_extent_delalloc because we don't want
  5464. * the dirty or uptodate bits
  5465. */
  5466. if (writing) {
  5467. write_bits = EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING;
  5468. ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  5469. EXTENT_DELALLOC, 0, NULL, &cached_state,
  5470. GFP_NOFS);
  5471. if (ret) {
  5472. clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
  5473. lockend, EXTENT_LOCKED | write_bits,
  5474. 1, 0, &cached_state, GFP_NOFS);
  5475. goto out;
  5476. }
  5477. }
  5478. free_extent_state(cached_state);
  5479. cached_state = NULL;
  5480. ret = __blockdev_direct_IO(rw, iocb, inode,
  5481. BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
  5482. iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
  5483. btrfs_submit_direct, 0);
  5484. if (ret < 0 && ret != -EIOCBQUEUED) {
  5485. clear_extent_bit(&BTRFS_I(inode)->io_tree, offset,
  5486. offset + iov_length(iov, nr_segs) - 1,
  5487. EXTENT_LOCKED | write_bits, 1, 0,
  5488. &cached_state, GFP_NOFS);
  5489. } else if (ret >= 0 && ret < iov_length(iov, nr_segs)) {
  5490. /*
  5491. * We're falling back to buffered, unlock the section we didn't
  5492. * do IO on.
  5493. */
  5494. clear_extent_bit(&BTRFS_I(inode)->io_tree, offset + ret,
  5495. offset + iov_length(iov, nr_segs) - 1,
  5496. EXTENT_LOCKED | write_bits, 1, 0,
  5497. &cached_state, GFP_NOFS);
  5498. }
  5499. out:
  5500. free_extent_state(cached_state);
  5501. return ret;
  5502. }
  5503. static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  5504. __u64 start, __u64 len)
  5505. {
  5506. return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
  5507. }
  5508. int btrfs_readpage(struct file *file, struct page *page)
  5509. {
  5510. struct extent_io_tree *tree;
  5511. tree = &BTRFS_I(page->mapping->host)->io_tree;
  5512. return extent_read_full_page(tree, page, btrfs_get_extent);
  5513. }
  5514. static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
  5515. {
  5516. struct extent_io_tree *tree;
  5517. if (current->flags & PF_MEMALLOC) {
  5518. redirty_page_for_writepage(wbc, page);
  5519. unlock_page(page);
  5520. return 0;
  5521. }
  5522. tree = &BTRFS_I(page->mapping->host)->io_tree;
  5523. return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
  5524. }
  5525. int btrfs_writepages(struct address_space *mapping,
  5526. struct writeback_control *wbc)
  5527. {
  5528. struct extent_io_tree *tree;
  5529. tree = &BTRFS_I(mapping->host)->io_tree;
  5530. return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
  5531. }
  5532. static int
  5533. btrfs_readpages(struct file *file, struct address_space *mapping,
  5534. struct list_head *pages, unsigned nr_pages)
  5535. {
  5536. struct extent_io_tree *tree;
  5537. tree = &BTRFS_I(mapping->host)->io_tree;
  5538. return extent_readpages(tree, mapping, pages, nr_pages,
  5539. btrfs_get_extent);
  5540. }
  5541. static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  5542. {
  5543. struct extent_io_tree *tree;
  5544. struct extent_map_tree *map;
  5545. int ret;
  5546. tree = &BTRFS_I(page->mapping->host)->io_tree;
  5547. map = &BTRFS_I(page->mapping->host)->extent_tree;
  5548. ret = try_release_extent_mapping(map, tree, page, gfp_flags);
  5549. if (ret == 1) {
  5550. ClearPagePrivate(page);
  5551. set_page_private(page, 0);
  5552. page_cache_release(page);
  5553. }
  5554. return ret;
  5555. }
  5556. static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  5557. {
  5558. if (PageWriteback(page) || PageDirty(page))
  5559. return 0;
  5560. return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
  5561. }
  5562. static void btrfs_invalidatepage(struct page *page, unsigned long offset)
  5563. {
  5564. struct extent_io_tree *tree;
  5565. struct btrfs_ordered_extent *ordered;
  5566. struct extent_state *cached_state = NULL;
  5567. u64 page_start = page_offset(page);
  5568. u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
  5569. /*
  5570. * we have the page locked, so new writeback can't start,
  5571. * and the dirty bit won't be cleared while we are here.
  5572. *
  5573. * Wait for IO on this page so that we can safely clear
  5574. * the PagePrivate2 bit and do ordered accounting
  5575. */
  5576. wait_on_page_writeback(page);
  5577. tree = &BTRFS_I(page->mapping->host)->io_tree;
  5578. if (offset) {
  5579. btrfs_releasepage(page, GFP_NOFS);
  5580. return;
  5581. }
  5582. lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
  5583. GFP_NOFS);
  5584. ordered = btrfs_lookup_ordered_extent(page->mapping->host,
  5585. page_offset(page));
  5586. if (ordered) {
  5587. /*
  5588. * IO on this page will never be started, so we need
  5589. * to account for any ordered extents now
  5590. */
  5591. clear_extent_bit(tree, page_start, page_end,
  5592. EXTENT_DIRTY | EXTENT_DELALLOC |
  5593. EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
  5594. &cached_state, GFP_NOFS);
  5595. /*
  5596. * whoever cleared the private bit is responsible
  5597. * for the finish_ordered_io
  5598. */
  5599. if (TestClearPagePrivate2(page)) {
  5600. btrfs_finish_ordered_io(page->mapping->host,
  5601. page_start, page_end);
  5602. }
  5603. btrfs_put_ordered_extent(ordered);
  5604. cached_state = NULL;
  5605. lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
  5606. GFP_NOFS);
  5607. }
  5608. clear_extent_bit(tree, page_start, page_end,
  5609. EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
  5610. EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS);
  5611. __btrfs_releasepage(page, GFP_NOFS);
  5612. ClearPageChecked(page);
  5613. if (PagePrivate(page)) {
  5614. ClearPagePrivate(page);
  5615. set_page_private(page, 0);
  5616. page_cache_release(page);
  5617. }
  5618. }
  5619. /*
  5620. * btrfs_page_mkwrite() is not allowed to change the file size as it gets
  5621. * called from a page fault handler when a page is first dirtied. Hence we must
  5622. * be careful to check for EOF conditions here. We set the page up correctly
  5623. * for a written page which means we get ENOSPC checking when writing into
  5624. * holes and correct delalloc and unwritten extent mapping on filesystems that
  5625. * support these features.
  5626. *
  5627. * We are not allowed to take the i_mutex here so we have to play games to
  5628. * protect against truncate races as the page could now be beyond EOF. Because
  5629. * vmtruncate() writes the inode size before removing pages, once we have the
  5630. * page lock we can determine safely if the page is beyond EOF. If it is not
  5631. * beyond EOF, then the page is guaranteed safe against truncation until we
  5632. * unlock the page.
  5633. */
  5634. int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  5635. {
  5636. struct page *page = vmf->page;
  5637. struct inode *inode = fdentry(vma->vm_file)->d_inode;
  5638. struct btrfs_root *root = BTRFS_I(inode)->root;
  5639. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  5640. struct btrfs_ordered_extent *ordered;
  5641. struct extent_state *cached_state = NULL;
  5642. char *kaddr;
  5643. unsigned long zero_start;
  5644. loff_t size;
  5645. int ret;
  5646. u64 page_start;
  5647. u64 page_end;
  5648. ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
  5649. if (ret) {
  5650. if (ret == -ENOMEM)
  5651. ret = VM_FAULT_OOM;
  5652. else /* -ENOSPC, -EIO, etc */
  5653. ret = VM_FAULT_SIGBUS;
  5654. goto out;
  5655. }
  5656. ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
  5657. again:
  5658. lock_page(page);
  5659. size = i_size_read(inode);
  5660. page_start = page_offset(page);
  5661. page_end = page_start + PAGE_CACHE_SIZE - 1;
  5662. if ((page->mapping != inode->i_mapping) ||
  5663. (page_start >= size)) {
  5664. /* page got truncated out from underneath us */
  5665. goto out_unlock;
  5666. }
  5667. wait_on_page_writeback(page);
  5668. lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
  5669. GFP_NOFS);
  5670. set_page_extent_mapped(page);
  5671. /*
  5672. * we can't set the delalloc bits if there are pending ordered
  5673. * extents. Drop our locks and wait for them to finish
  5674. */
  5675. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  5676. if (ordered) {
  5677. unlock_extent_cached(io_tree, page_start, page_end,
  5678. &cached_state, GFP_NOFS);
  5679. unlock_page(page);
  5680. btrfs_start_ordered_extent(inode, ordered, 1);
  5681. btrfs_put_ordered_extent(ordered);
  5682. goto again;
  5683. }
  5684. /*
  5685. * XXX - page_mkwrite gets called every time the page is dirtied, even
  5686. * if it was already dirty, so for space accounting reasons we need to
  5687. * clear any delalloc bits for the range we are fixing to save. There
  5688. * is probably a better way to do this, but for now keep consistent with
  5689. * prepare_pages in the normal write path.
  5690. */
  5691. clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
  5692. EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
  5693. 0, 0, &cached_state, GFP_NOFS);
  5694. ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
  5695. &cached_state);
  5696. if (ret) {
  5697. unlock_extent_cached(io_tree, page_start, page_end,
  5698. &cached_state, GFP_NOFS);
  5699. ret = VM_FAULT_SIGBUS;
  5700. goto out_unlock;
  5701. }
  5702. ret = 0;
  5703. /* page is wholly or partially inside EOF */
  5704. if (page_start + PAGE_CACHE_SIZE > size)
  5705. zero_start = size & ~PAGE_CACHE_MASK;
  5706. else
  5707. zero_start = PAGE_CACHE_SIZE;
  5708. if (zero_start != PAGE_CACHE_SIZE) {
  5709. kaddr = kmap(page);
  5710. memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
  5711. flush_dcache_page(page);
  5712. kunmap(page);
  5713. }
  5714. ClearPageChecked(page);
  5715. set_page_dirty(page);
  5716. SetPageUptodate(page);
  5717. BTRFS_I(inode)->last_trans = root->fs_info->generation;
  5718. BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
  5719. unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
  5720. out_unlock:
  5721. if (!ret)
  5722. return VM_FAULT_LOCKED;
  5723. unlock_page(page);
  5724. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  5725. out:
  5726. return ret;
  5727. }
  5728. static int btrfs_truncate(struct inode *inode)
  5729. {
  5730. struct btrfs_root *root = BTRFS_I(inode)->root;
  5731. int ret;
  5732. int err = 0;
  5733. struct btrfs_trans_handle *trans;
  5734. unsigned long nr;
  5735. u64 mask = root->sectorsize - 1;
  5736. ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
  5737. if (ret)
  5738. return ret;
  5739. btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
  5740. btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
  5741. trans = btrfs_start_transaction(root, 5);
  5742. if (IS_ERR(trans))
  5743. return PTR_ERR(trans);
  5744. btrfs_set_trans_block_group(trans, inode);
  5745. ret = btrfs_orphan_add(trans, inode);
  5746. if (ret) {
  5747. btrfs_end_transaction(trans, root);
  5748. return ret;
  5749. }
  5750. nr = trans->blocks_used;
  5751. btrfs_end_transaction(trans, root);
  5752. btrfs_btree_balance_dirty(root, nr);
  5753. /* Now start a transaction for the truncate */
  5754. trans = btrfs_start_transaction(root, 0);
  5755. if (IS_ERR(trans))
  5756. return PTR_ERR(trans);
  5757. btrfs_set_trans_block_group(trans, inode);
  5758. trans->block_rsv = root->orphan_block_rsv;
  5759. /*
  5760. * setattr is responsible for setting the ordered_data_close flag,
  5761. * but that is only tested during the last file release. That
  5762. * could happen well after the next commit, leaving a great big
  5763. * window where new writes may get lost if someone chooses to write
  5764. * to this file after truncating to zero
  5765. *
  5766. * The inode doesn't have any dirty data here, and so if we commit
  5767. * this is a noop. If someone immediately starts writing to the inode
  5768. * it is very likely we'll catch some of their writes in this
  5769. * transaction, and the commit will find this file on the ordered
  5770. * data list with good things to send down.
  5771. *
  5772. * This is a best effort solution, there is still a window where
  5773. * using truncate to replace the contents of the file will
  5774. * end up with a zero length file after a crash.
  5775. */
  5776. if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
  5777. btrfs_add_ordered_operation(trans, root, inode);
  5778. while (1) {
  5779. if (!trans) {
  5780. trans = btrfs_start_transaction(root, 0);
  5781. if (IS_ERR(trans))
  5782. return PTR_ERR(trans);
  5783. btrfs_set_trans_block_group(trans, inode);
  5784. trans->block_rsv = root->orphan_block_rsv;
  5785. }
  5786. ret = btrfs_block_rsv_check(trans, root,
  5787. root->orphan_block_rsv, 0, 5);
  5788. if (ret == -EAGAIN) {
  5789. ret = btrfs_commit_transaction(trans, root);
  5790. if (ret)
  5791. return ret;
  5792. trans = NULL;
  5793. continue;
  5794. } else if (ret) {
  5795. err = ret;
  5796. break;
  5797. }
  5798. ret = btrfs_truncate_inode_items(trans, root, inode,
  5799. inode->i_size,
  5800. BTRFS_EXTENT_DATA_KEY);
  5801. if (ret != -EAGAIN) {
  5802. err = ret;
  5803. break;
  5804. }
  5805. ret = btrfs_update_inode(trans, root, inode);
  5806. if (ret) {
  5807. err = ret;
  5808. break;
  5809. }
  5810. nr = trans->blocks_used;
  5811. btrfs_end_transaction(trans, root);
  5812. trans = NULL;
  5813. btrfs_btree_balance_dirty(root, nr);
  5814. }
  5815. if (ret == 0 && inode->i_nlink > 0) {
  5816. ret = btrfs_orphan_del(trans, inode);
  5817. if (ret)
  5818. err = ret;
  5819. } else if (ret && inode->i_nlink > 0) {
  5820. /*
  5821. * Failed to do the truncate, remove us from the in memory
  5822. * orphan list.
  5823. */
  5824. ret = btrfs_orphan_del(NULL, inode);
  5825. }
  5826. ret = btrfs_update_inode(trans, root, inode);
  5827. if (ret && !err)
  5828. err = ret;
  5829. nr = trans->blocks_used;
  5830. ret = btrfs_end_transaction_throttle(trans, root);
  5831. if (ret && !err)
  5832. err = ret;
  5833. btrfs_btree_balance_dirty(root, nr);
  5834. return err;
  5835. }
  5836. /*
  5837. * create a new subvolume directory/inode (helper for the ioctl).
  5838. */
  5839. int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
  5840. struct btrfs_root *new_root,
  5841. u64 new_dirid, u64 alloc_hint)
  5842. {
  5843. struct inode *inode;
  5844. int err;
  5845. u64 index = 0;
  5846. inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
  5847. new_dirid, alloc_hint, S_IFDIR | 0700, &index);
  5848. if (IS_ERR(inode))
  5849. return PTR_ERR(inode);
  5850. inode->i_op = &btrfs_dir_inode_operations;
  5851. inode->i_fop = &btrfs_dir_file_operations;
  5852. inode->i_nlink = 1;
  5853. btrfs_i_size_write(inode, 0);
  5854. err = btrfs_update_inode(trans, new_root, inode);
  5855. BUG_ON(err);
  5856. iput(inode);
  5857. return 0;
  5858. }
  5859. /* helper function for file defrag and space balancing. This
  5860. * forces readahead on a given range of bytes in an inode
  5861. */
  5862. unsigned long btrfs_force_ra(struct address_space *mapping,
  5863. struct file_ra_state *ra, struct file *file,
  5864. pgoff_t offset, pgoff_t last_index)
  5865. {
  5866. pgoff_t req_size = last_index - offset + 1;
  5867. page_cache_sync_readahead(mapping, ra, file, offset, req_size);
  5868. return offset + req_size;
  5869. }
  5870. struct inode *btrfs_alloc_inode(struct super_block *sb)
  5871. {
  5872. struct btrfs_inode *ei;
  5873. struct inode *inode;
  5874. ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
  5875. if (!ei)
  5876. return NULL;
  5877. ei->root = NULL;
  5878. ei->space_info = NULL;
  5879. ei->generation = 0;
  5880. ei->sequence = 0;
  5881. ei->last_trans = 0;
  5882. ei->last_sub_trans = 0;
  5883. ei->logged_trans = 0;
  5884. ei->delalloc_bytes = 0;
  5885. ei->reserved_bytes = 0;
  5886. ei->disk_i_size = 0;
  5887. ei->flags = 0;
  5888. ei->index_cnt = (u64)-1;
  5889. ei->last_unlink_trans = 0;
  5890. atomic_set(&ei->outstanding_extents, 0);
  5891. atomic_set(&ei->reserved_extents, 0);
  5892. ei->ordered_data_close = 0;
  5893. ei->orphan_meta_reserved = 0;
  5894. ei->dummy_inode = 0;
  5895. ei->force_compress = BTRFS_COMPRESS_NONE;
  5896. inode = &ei->vfs_inode;
  5897. extent_map_tree_init(&ei->extent_tree, GFP_NOFS);
  5898. extent_io_tree_init(&ei->io_tree, &inode->i_data, GFP_NOFS);
  5899. extent_io_tree_init(&ei->io_failure_tree, &inode->i_data, GFP_NOFS);
  5900. mutex_init(&ei->log_mutex);
  5901. btrfs_ordered_inode_tree_init(&ei->ordered_tree);
  5902. INIT_LIST_HEAD(&ei->i_orphan);
  5903. INIT_LIST_HEAD(&ei->delalloc_inodes);
  5904. INIT_LIST_HEAD(&ei->ordered_operations);
  5905. RB_CLEAR_NODE(&ei->rb_node);
  5906. return inode;
  5907. }
  5908. static void btrfs_i_callback(struct rcu_head *head)
  5909. {
  5910. struct inode *inode = container_of(head, struct inode, i_rcu);
  5911. INIT_LIST_HEAD(&inode->i_dentry);
  5912. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  5913. }
  5914. void btrfs_destroy_inode(struct inode *inode)
  5915. {
  5916. struct btrfs_ordered_extent *ordered;
  5917. struct btrfs_root *root = BTRFS_I(inode)->root;
  5918. WARN_ON(!list_empty(&inode->i_dentry));
  5919. WARN_ON(inode->i_data.nrpages);
  5920. WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents));
  5921. WARN_ON(atomic_read(&BTRFS_I(inode)->reserved_extents));
  5922. /*
  5923. * This can happen where we create an inode, but somebody else also
  5924. * created the same inode and we need to destroy the one we already
  5925. * created.
  5926. */
  5927. if (!root)
  5928. goto free;
  5929. /*
  5930. * Make sure we're properly removed from the ordered operation
  5931. * lists.
  5932. */
  5933. smp_mb();
  5934. if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
  5935. spin_lock(&root->fs_info->ordered_extent_lock);
  5936. list_del_init(&BTRFS_I(inode)->ordered_operations);
  5937. spin_unlock(&root->fs_info->ordered_extent_lock);
  5938. }
  5939. if (root == root->fs_info->tree_root) {
  5940. struct btrfs_block_group_cache *block_group;
  5941. block_group = btrfs_lookup_block_group(root->fs_info,
  5942. BTRFS_I(inode)->block_group);
  5943. if (block_group && block_group->inode == inode) {
  5944. spin_lock(&block_group->lock);
  5945. block_group->inode = NULL;
  5946. spin_unlock(&block_group->lock);
  5947. btrfs_put_block_group(block_group);
  5948. } else if (block_group) {
  5949. btrfs_put_block_group(block_group);
  5950. }
  5951. }
  5952. spin_lock(&root->orphan_lock);
  5953. if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
  5954. printk(KERN_INFO "BTRFS: inode %lu still on the orphan list\n",
  5955. inode->i_ino);
  5956. list_del_init(&BTRFS_I(inode)->i_orphan);
  5957. }
  5958. spin_unlock(&root->orphan_lock);
  5959. while (1) {
  5960. ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
  5961. if (!ordered)
  5962. break;
  5963. else {
  5964. printk(KERN_ERR "btrfs found ordered "
  5965. "extent %llu %llu on inode cleanup\n",
  5966. (unsigned long long)ordered->file_offset,
  5967. (unsigned long long)ordered->len);
  5968. btrfs_remove_ordered_extent(inode, ordered);
  5969. btrfs_put_ordered_extent(ordered);
  5970. btrfs_put_ordered_extent(ordered);
  5971. }
  5972. }
  5973. inode_tree_del(inode);
  5974. btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
  5975. free:
  5976. call_rcu(&inode->i_rcu, btrfs_i_callback);
  5977. }
  5978. int btrfs_drop_inode(struct inode *inode)
  5979. {
  5980. struct btrfs_root *root = BTRFS_I(inode)->root;
  5981. if (btrfs_root_refs(&root->root_item) == 0 &&
  5982. root != root->fs_info->tree_root)
  5983. return 1;
  5984. else
  5985. return generic_drop_inode(inode);
  5986. }
  5987. static void init_once(void *foo)
  5988. {
  5989. struct btrfs_inode *ei = (struct btrfs_inode *) foo;
  5990. inode_init_once(&ei->vfs_inode);
  5991. }
  5992. void btrfs_destroy_cachep(void)
  5993. {
  5994. if (btrfs_inode_cachep)
  5995. kmem_cache_destroy(btrfs_inode_cachep);
  5996. if (btrfs_trans_handle_cachep)
  5997. kmem_cache_destroy(btrfs_trans_handle_cachep);
  5998. if (btrfs_transaction_cachep)
  5999. kmem_cache_destroy(btrfs_transaction_cachep);
  6000. if (btrfs_path_cachep)
  6001. kmem_cache_destroy(btrfs_path_cachep);
  6002. if (btrfs_free_space_cachep)
  6003. kmem_cache_destroy(btrfs_free_space_cachep);
  6004. }
  6005. int btrfs_init_cachep(void)
  6006. {
  6007. btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
  6008. sizeof(struct btrfs_inode), 0,
  6009. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
  6010. if (!btrfs_inode_cachep)
  6011. goto fail;
  6012. btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
  6013. sizeof(struct btrfs_trans_handle), 0,
  6014. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  6015. if (!btrfs_trans_handle_cachep)
  6016. goto fail;
  6017. btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
  6018. sizeof(struct btrfs_transaction), 0,
  6019. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  6020. if (!btrfs_transaction_cachep)
  6021. goto fail;
  6022. btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
  6023. sizeof(struct btrfs_path), 0,
  6024. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  6025. if (!btrfs_path_cachep)
  6026. goto fail;
  6027. btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space_cache",
  6028. sizeof(struct btrfs_free_space), 0,
  6029. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  6030. if (!btrfs_free_space_cachep)
  6031. goto fail;
  6032. return 0;
  6033. fail:
  6034. btrfs_destroy_cachep();
  6035. return -ENOMEM;
  6036. }
  6037. static int btrfs_getattr(struct vfsmount *mnt,
  6038. struct dentry *dentry, struct kstat *stat)
  6039. {
  6040. struct inode *inode = dentry->d_inode;
  6041. generic_fillattr(inode, stat);
  6042. stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
  6043. stat->blksize = PAGE_CACHE_SIZE;
  6044. stat->blocks = (inode_get_bytes(inode) +
  6045. BTRFS_I(inode)->delalloc_bytes) >> 9;
  6046. return 0;
  6047. }
  6048. static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  6049. struct inode *new_dir, struct dentry *new_dentry)
  6050. {
  6051. struct btrfs_trans_handle *trans;
  6052. struct btrfs_root *root = BTRFS_I(old_dir)->root;
  6053. struct btrfs_root *dest = BTRFS_I(new_dir)->root;
  6054. struct inode *new_inode = new_dentry->d_inode;
  6055. struct inode *old_inode = old_dentry->d_inode;
  6056. struct timespec ctime = CURRENT_TIME;
  6057. u64 index = 0;
  6058. u64 root_objectid;
  6059. int ret;
  6060. if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
  6061. return -EPERM;
  6062. /* we only allow rename subvolume link between subvolumes */
  6063. if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
  6064. return -EXDEV;
  6065. if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
  6066. (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID))
  6067. return -ENOTEMPTY;
  6068. if (S_ISDIR(old_inode->i_mode) && new_inode &&
  6069. new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
  6070. return -ENOTEMPTY;
  6071. /*
  6072. * we're using rename to replace one file with another.
  6073. * and the replacement file is large. Start IO on it now so
  6074. * we don't add too much work to the end of the transaction
  6075. */
  6076. if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
  6077. old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
  6078. filemap_flush(old_inode->i_mapping);
  6079. /* close the racy window with snapshot create/destroy ioctl */
  6080. if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
  6081. down_read(&root->fs_info->subvol_sem);
  6082. /*
  6083. * We want to reserve the absolute worst case amount of items. So if
  6084. * both inodes are subvols and we need to unlink them then that would
  6085. * require 4 item modifications, but if they are both normal inodes it
  6086. * would require 5 item modifications, so we'll assume their normal
  6087. * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
  6088. * should cover the worst case number of items we'll modify.
  6089. */
  6090. trans = btrfs_start_transaction(root, 20);
  6091. if (IS_ERR(trans))
  6092. return PTR_ERR(trans);
  6093. btrfs_set_trans_block_group(trans, new_dir);
  6094. if (dest != root)
  6095. btrfs_record_root_in_trans(trans, dest);
  6096. ret = btrfs_set_inode_index(new_dir, &index);
  6097. if (ret)
  6098. goto out_fail;
  6099. if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  6100. /* force full log commit if subvolume involved. */
  6101. root->fs_info->last_trans_log_full_commit = trans->transid;
  6102. } else {
  6103. ret = btrfs_insert_inode_ref(trans, dest,
  6104. new_dentry->d_name.name,
  6105. new_dentry->d_name.len,
  6106. old_inode->i_ino,
  6107. new_dir->i_ino, index);
  6108. if (ret)
  6109. goto out_fail;
  6110. /*
  6111. * this is an ugly little race, but the rename is required
  6112. * to make sure that if we crash, the inode is either at the
  6113. * old name or the new one. pinning the log transaction lets
  6114. * us make sure we don't allow a log commit to come in after
  6115. * we unlink the name but before we add the new name back in.
  6116. */
  6117. btrfs_pin_log_trans(root);
  6118. }
  6119. /*
  6120. * make sure the inode gets flushed if it is replacing
  6121. * something.
  6122. */
  6123. if (new_inode && new_inode->i_size &&
  6124. old_inode && S_ISREG(old_inode->i_mode)) {
  6125. btrfs_add_ordered_operation(trans, root, old_inode);
  6126. }
  6127. old_dir->i_ctime = old_dir->i_mtime = ctime;
  6128. new_dir->i_ctime = new_dir->i_mtime = ctime;
  6129. old_inode->i_ctime = ctime;
  6130. if (old_dentry->d_parent != new_dentry->d_parent)
  6131. btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
  6132. if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  6133. root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
  6134. ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
  6135. old_dentry->d_name.name,
  6136. old_dentry->d_name.len);
  6137. } else {
  6138. btrfs_inc_nlink(old_dentry->d_inode);
  6139. ret = btrfs_unlink_inode(trans, root, old_dir,
  6140. old_dentry->d_inode,
  6141. old_dentry->d_name.name,
  6142. old_dentry->d_name.len);
  6143. }
  6144. BUG_ON(ret);
  6145. if (new_inode) {
  6146. new_inode->i_ctime = CURRENT_TIME;
  6147. if (unlikely(new_inode->i_ino ==
  6148. BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
  6149. root_objectid = BTRFS_I(new_inode)->location.objectid;
  6150. ret = btrfs_unlink_subvol(trans, dest, new_dir,
  6151. root_objectid,
  6152. new_dentry->d_name.name,
  6153. new_dentry->d_name.len);
  6154. BUG_ON(new_inode->i_nlink == 0);
  6155. } else {
  6156. ret = btrfs_unlink_inode(trans, dest, new_dir,
  6157. new_dentry->d_inode,
  6158. new_dentry->d_name.name,
  6159. new_dentry->d_name.len);
  6160. }
  6161. BUG_ON(ret);
  6162. if (new_inode->i_nlink == 0) {
  6163. ret = btrfs_orphan_add(trans, new_dentry->d_inode);
  6164. BUG_ON(ret);
  6165. }
  6166. }
  6167. ret = btrfs_add_link(trans, new_dir, old_inode,
  6168. new_dentry->d_name.name,
  6169. new_dentry->d_name.len, 0, index);
  6170. BUG_ON(ret);
  6171. if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
  6172. struct dentry *parent = dget_parent(new_dentry);
  6173. btrfs_log_new_name(trans, old_inode, old_dir, parent);
  6174. dput(parent);
  6175. btrfs_end_log_trans(root);
  6176. }
  6177. out_fail:
  6178. btrfs_end_transaction_throttle(trans, root);
  6179. if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
  6180. up_read(&root->fs_info->subvol_sem);
  6181. return ret;
  6182. }
  6183. /*
  6184. * some fairly slow code that needs optimization. This walks the list
  6185. * of all the inodes with pending delalloc and forces them to disk.
  6186. */
  6187. int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
  6188. {
  6189. struct list_head *head = &root->fs_info->delalloc_inodes;
  6190. struct btrfs_inode *binode;
  6191. struct inode *inode;
  6192. if (root->fs_info->sb->s_flags & MS_RDONLY)
  6193. return -EROFS;
  6194. spin_lock(&root->fs_info->delalloc_lock);
  6195. while (!list_empty(head)) {
  6196. binode = list_entry(head->next, struct btrfs_inode,
  6197. delalloc_inodes);
  6198. inode = igrab(&binode->vfs_inode);
  6199. if (!inode)
  6200. list_del_init(&binode->delalloc_inodes);
  6201. spin_unlock(&root->fs_info->delalloc_lock);
  6202. if (inode) {
  6203. filemap_flush(inode->i_mapping);
  6204. if (delay_iput)
  6205. btrfs_add_delayed_iput(inode);
  6206. else
  6207. iput(inode);
  6208. }
  6209. cond_resched();
  6210. spin_lock(&root->fs_info->delalloc_lock);
  6211. }
  6212. spin_unlock(&root->fs_info->delalloc_lock);
  6213. /* the filemap_flush will queue IO into the worker threads, but
  6214. * we have to make sure the IO is actually started and that
  6215. * ordered extents get created before we return
  6216. */
  6217. atomic_inc(&root->fs_info->async_submit_draining);
  6218. while (atomic_read(&root->fs_info->nr_async_submits) ||
  6219. atomic_read(&root->fs_info->async_delalloc_pages)) {
  6220. wait_event(root->fs_info->async_submit_wait,
  6221. (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
  6222. atomic_read(&root->fs_info->async_delalloc_pages) == 0));
  6223. }
  6224. atomic_dec(&root->fs_info->async_submit_draining);
  6225. return 0;
  6226. }
  6227. int btrfs_start_one_delalloc_inode(struct btrfs_root *root, int delay_iput,
  6228. int sync)
  6229. {
  6230. struct btrfs_inode *binode;
  6231. struct inode *inode = NULL;
  6232. spin_lock(&root->fs_info->delalloc_lock);
  6233. while (!list_empty(&root->fs_info->delalloc_inodes)) {
  6234. binode = list_entry(root->fs_info->delalloc_inodes.next,
  6235. struct btrfs_inode, delalloc_inodes);
  6236. inode = igrab(&binode->vfs_inode);
  6237. if (inode) {
  6238. list_move_tail(&binode->delalloc_inodes,
  6239. &root->fs_info->delalloc_inodes);
  6240. break;
  6241. }
  6242. list_del_init(&binode->delalloc_inodes);
  6243. cond_resched_lock(&root->fs_info->delalloc_lock);
  6244. }
  6245. spin_unlock(&root->fs_info->delalloc_lock);
  6246. if (inode) {
  6247. if (sync) {
  6248. filemap_write_and_wait(inode->i_mapping);
  6249. /*
  6250. * We have to do this because compression doesn't
  6251. * actually set PG_writeback until it submits the pages
  6252. * for IO, which happens in an async thread, so we could
  6253. * race and not actually wait for any writeback pages
  6254. * because they've not been submitted yet. Technically
  6255. * this could still be the case for the ordered stuff
  6256. * since the async thread may not have started to do its
  6257. * work yet. If this becomes the case then we need to
  6258. * figure out a way to make sure that in writepage we
  6259. * wait for any async pages to be submitted before
  6260. * returning so that fdatawait does what its supposed to
  6261. * do.
  6262. */
  6263. btrfs_wait_ordered_range(inode, 0, (u64)-1);
  6264. } else {
  6265. filemap_flush(inode->i_mapping);
  6266. }
  6267. if (delay_iput)
  6268. btrfs_add_delayed_iput(inode);
  6269. else
  6270. iput(inode);
  6271. return 1;
  6272. }
  6273. return 0;
  6274. }
  6275. static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
  6276. const char *symname)
  6277. {
  6278. struct btrfs_trans_handle *trans;
  6279. struct btrfs_root *root = BTRFS_I(dir)->root;
  6280. struct btrfs_path *path;
  6281. struct btrfs_key key;
  6282. struct inode *inode = NULL;
  6283. int err;
  6284. int drop_inode = 0;
  6285. u64 objectid;
  6286. u64 index = 0 ;
  6287. int name_len;
  6288. int datasize;
  6289. unsigned long ptr;
  6290. struct btrfs_file_extent_item *ei;
  6291. struct extent_buffer *leaf;
  6292. unsigned long nr = 0;
  6293. name_len = strlen(symname) + 1;
  6294. if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
  6295. return -ENAMETOOLONG;
  6296. err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
  6297. if (err)
  6298. return err;
  6299. /*
  6300. * 2 items for inode item and ref
  6301. * 2 items for dir items
  6302. * 1 item for xattr if selinux is on
  6303. */
  6304. trans = btrfs_start_transaction(root, 5);
  6305. if (IS_ERR(trans))
  6306. return PTR_ERR(trans);
  6307. btrfs_set_trans_block_group(trans, dir);
  6308. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  6309. dentry->d_name.len, dir->i_ino, objectid,
  6310. BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
  6311. &index);
  6312. err = PTR_ERR(inode);
  6313. if (IS_ERR(inode))
  6314. goto out_unlock;
  6315. err = btrfs_init_inode_security(trans, inode, dir);
  6316. if (err) {
  6317. drop_inode = 1;
  6318. goto out_unlock;
  6319. }
  6320. btrfs_set_trans_block_group(trans, inode);
  6321. err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
  6322. if (err)
  6323. drop_inode = 1;
  6324. else {
  6325. inode->i_mapping->a_ops = &btrfs_aops;
  6326. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  6327. inode->i_fop = &btrfs_file_operations;
  6328. inode->i_op = &btrfs_file_inode_operations;
  6329. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  6330. }
  6331. btrfs_update_inode_block_group(trans, inode);
  6332. btrfs_update_inode_block_group(trans, dir);
  6333. if (drop_inode)
  6334. goto out_unlock;
  6335. path = btrfs_alloc_path();
  6336. BUG_ON(!path);
  6337. key.objectid = inode->i_ino;
  6338. key.offset = 0;
  6339. btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
  6340. datasize = btrfs_file_extent_calc_inline_size(name_len);
  6341. err = btrfs_insert_empty_item(trans, root, path, &key,
  6342. datasize);
  6343. if (err) {
  6344. drop_inode = 1;
  6345. goto out_unlock;
  6346. }
  6347. leaf = path->nodes[0];
  6348. ei = btrfs_item_ptr(leaf, path->slots[0],
  6349. struct btrfs_file_extent_item);
  6350. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  6351. btrfs_set_file_extent_type(leaf, ei,
  6352. BTRFS_FILE_EXTENT_INLINE);
  6353. btrfs_set_file_extent_encryption(leaf, ei, 0);
  6354. btrfs_set_file_extent_compression(leaf, ei, 0);
  6355. btrfs_set_file_extent_other_encoding(leaf, ei, 0);
  6356. btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
  6357. ptr = btrfs_file_extent_inline_start(ei);
  6358. write_extent_buffer(leaf, symname, ptr, name_len);
  6359. btrfs_mark_buffer_dirty(leaf);
  6360. btrfs_free_path(path);
  6361. inode->i_op = &btrfs_symlink_inode_operations;
  6362. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  6363. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  6364. inode_set_bytes(inode, name_len);
  6365. btrfs_i_size_write(inode, name_len - 1);
  6366. err = btrfs_update_inode(trans, root, inode);
  6367. if (err)
  6368. drop_inode = 1;
  6369. out_unlock:
  6370. nr = trans->blocks_used;
  6371. btrfs_end_transaction_throttle(trans, root);
  6372. if (drop_inode) {
  6373. inode_dec_link_count(inode);
  6374. iput(inode);
  6375. }
  6376. btrfs_btree_balance_dirty(root, nr);
  6377. return err;
  6378. }
  6379. static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
  6380. u64 start, u64 num_bytes, u64 min_size,
  6381. loff_t actual_len, u64 *alloc_hint,
  6382. struct btrfs_trans_handle *trans)
  6383. {
  6384. struct btrfs_root *root = BTRFS_I(inode)->root;
  6385. struct btrfs_key ins;
  6386. u64 cur_offset = start;
  6387. u64 i_size;
  6388. int ret = 0;
  6389. bool own_trans = true;
  6390. if (trans)
  6391. own_trans = false;
  6392. while (num_bytes > 0) {
  6393. if (own_trans) {
  6394. trans = btrfs_start_transaction(root, 3);
  6395. if (IS_ERR(trans)) {
  6396. ret = PTR_ERR(trans);
  6397. break;
  6398. }
  6399. }
  6400. ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
  6401. 0, *alloc_hint, (u64)-1, &ins, 1);
  6402. if (ret) {
  6403. if (own_trans)
  6404. btrfs_end_transaction(trans, root);
  6405. break;
  6406. }
  6407. ret = insert_reserved_file_extent(trans, inode,
  6408. cur_offset, ins.objectid,
  6409. ins.offset, ins.offset,
  6410. ins.offset, 0, 0, 0,
  6411. BTRFS_FILE_EXTENT_PREALLOC);
  6412. BUG_ON(ret);
  6413. btrfs_drop_extent_cache(inode, cur_offset,
  6414. cur_offset + ins.offset -1, 0);
  6415. num_bytes -= ins.offset;
  6416. cur_offset += ins.offset;
  6417. *alloc_hint = ins.objectid + ins.offset;
  6418. inode->i_ctime = CURRENT_TIME;
  6419. BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
  6420. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  6421. (actual_len > inode->i_size) &&
  6422. (cur_offset > inode->i_size)) {
  6423. if (cur_offset > actual_len)
  6424. i_size = actual_len;
  6425. else
  6426. i_size = cur_offset;
  6427. i_size_write(inode, i_size);
  6428. btrfs_ordered_update_i_size(inode, i_size, NULL);
  6429. }
  6430. ret = btrfs_update_inode(trans, root, inode);
  6431. BUG_ON(ret);
  6432. if (own_trans)
  6433. btrfs_end_transaction(trans, root);
  6434. }
  6435. return ret;
  6436. }
  6437. int btrfs_prealloc_file_range(struct inode *inode, int mode,
  6438. u64 start, u64 num_bytes, u64 min_size,
  6439. loff_t actual_len, u64 *alloc_hint)
  6440. {
  6441. return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
  6442. min_size, actual_len, alloc_hint,
  6443. NULL);
  6444. }
  6445. int btrfs_prealloc_file_range_trans(struct inode *inode,
  6446. struct btrfs_trans_handle *trans, int mode,
  6447. u64 start, u64 num_bytes, u64 min_size,
  6448. loff_t actual_len, u64 *alloc_hint)
  6449. {
  6450. return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
  6451. min_size, actual_len, alloc_hint, trans);
  6452. }
  6453. static int btrfs_set_page_dirty(struct page *page)
  6454. {
  6455. return __set_page_dirty_nobuffers(page);
  6456. }
  6457. static int btrfs_permission(struct inode *inode, int mask, unsigned int flags)
  6458. {
  6459. struct btrfs_root *root = BTRFS_I(inode)->root;
  6460. if (btrfs_root_readonly(root) && (mask & MAY_WRITE))
  6461. return -EROFS;
  6462. if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
  6463. return -EACCES;
  6464. return generic_permission(inode, mask, flags, btrfs_check_acl);
  6465. }
  6466. static const struct inode_operations btrfs_dir_inode_operations = {
  6467. .getattr = btrfs_getattr,
  6468. .lookup = btrfs_lookup,
  6469. .create = btrfs_create,
  6470. .unlink = btrfs_unlink,
  6471. .link = btrfs_link,
  6472. .mkdir = btrfs_mkdir,
  6473. .rmdir = btrfs_rmdir,
  6474. .rename = btrfs_rename,
  6475. .symlink = btrfs_symlink,
  6476. .setattr = btrfs_setattr,
  6477. .mknod = btrfs_mknod,
  6478. .setxattr = btrfs_setxattr,
  6479. .getxattr = btrfs_getxattr,
  6480. .listxattr = btrfs_listxattr,
  6481. .removexattr = btrfs_removexattr,
  6482. .permission = btrfs_permission,
  6483. };
  6484. static const struct inode_operations btrfs_dir_ro_inode_operations = {
  6485. .lookup = btrfs_lookup,
  6486. .permission = btrfs_permission,
  6487. };
  6488. static const struct file_operations btrfs_dir_file_operations = {
  6489. .llseek = generic_file_llseek,
  6490. .read = generic_read_dir,
  6491. .readdir = btrfs_real_readdir,
  6492. .unlocked_ioctl = btrfs_ioctl,
  6493. #ifdef CONFIG_COMPAT
  6494. .compat_ioctl = btrfs_ioctl,
  6495. #endif
  6496. .release = btrfs_release_file,
  6497. .fsync = btrfs_sync_file,
  6498. };
  6499. static struct extent_io_ops btrfs_extent_io_ops = {
  6500. .fill_delalloc = run_delalloc_range,
  6501. .submit_bio_hook = btrfs_submit_bio_hook,
  6502. .merge_bio_hook = btrfs_merge_bio_hook,
  6503. .readpage_end_io_hook = btrfs_readpage_end_io_hook,
  6504. .writepage_end_io_hook = btrfs_writepage_end_io_hook,
  6505. .writepage_start_hook = btrfs_writepage_start_hook,
  6506. .readpage_io_failed_hook = btrfs_io_failed_hook,
  6507. .set_bit_hook = btrfs_set_bit_hook,
  6508. .clear_bit_hook = btrfs_clear_bit_hook,
  6509. .merge_extent_hook = btrfs_merge_extent_hook,
  6510. .split_extent_hook = btrfs_split_extent_hook,
  6511. };
  6512. /*
  6513. * btrfs doesn't support the bmap operation because swapfiles
  6514. * use bmap to make a mapping of extents in the file. They assume
  6515. * these extents won't change over the life of the file and they
  6516. * use the bmap result to do IO directly to the drive.
  6517. *
  6518. * the btrfs bmap call would return logical addresses that aren't
  6519. * suitable for IO and they also will change frequently as COW
  6520. * operations happen. So, swapfile + btrfs == corruption.
  6521. *
  6522. * For now we're avoiding this by dropping bmap.
  6523. */
  6524. static const struct address_space_operations btrfs_aops = {
  6525. .readpage = btrfs_readpage,
  6526. .writepage = btrfs_writepage,
  6527. .writepages = btrfs_writepages,
  6528. .readpages = btrfs_readpages,
  6529. .sync_page = block_sync_page,
  6530. .direct_IO = btrfs_direct_IO,
  6531. .invalidatepage = btrfs_invalidatepage,
  6532. .releasepage = btrfs_releasepage,
  6533. .set_page_dirty = btrfs_set_page_dirty,
  6534. .error_remove_page = generic_error_remove_page,
  6535. };
  6536. static const struct address_space_operations btrfs_symlink_aops = {
  6537. .readpage = btrfs_readpage,
  6538. .writepage = btrfs_writepage,
  6539. .invalidatepage = btrfs_invalidatepage,
  6540. .releasepage = btrfs_releasepage,
  6541. };
  6542. static const struct inode_operations btrfs_file_inode_operations = {
  6543. .getattr = btrfs_getattr,
  6544. .setattr = btrfs_setattr,
  6545. .setxattr = btrfs_setxattr,
  6546. .getxattr = btrfs_getxattr,
  6547. .listxattr = btrfs_listxattr,
  6548. .removexattr = btrfs_removexattr,
  6549. .permission = btrfs_permission,
  6550. .fiemap = btrfs_fiemap,
  6551. };
  6552. static const struct inode_operations btrfs_special_inode_operations = {
  6553. .getattr = btrfs_getattr,
  6554. .setattr = btrfs_setattr,
  6555. .permission = btrfs_permission,
  6556. .setxattr = btrfs_setxattr,
  6557. .getxattr = btrfs_getxattr,
  6558. .listxattr = btrfs_listxattr,
  6559. .removexattr = btrfs_removexattr,
  6560. };
  6561. static const struct inode_operations btrfs_symlink_inode_operations = {
  6562. .readlink = generic_readlink,
  6563. .follow_link = page_follow_link_light,
  6564. .put_link = page_put_link,
  6565. .getattr = btrfs_getattr,
  6566. .permission = btrfs_permission,
  6567. .setxattr = btrfs_setxattr,
  6568. .getxattr = btrfs_getxattr,
  6569. .listxattr = btrfs_listxattr,
  6570. .removexattr = btrfs_removexattr,
  6571. };
  6572. const struct dentry_operations btrfs_dentry_operations = {
  6573. .d_delete = btrfs_dentry_delete,
  6574. };