inode.c 200 KB

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