inode.c 197 KB

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