inode.c 197 KB

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